outerweb/localization

此包为您的Laravel应用添加多语言支持。

v1.0.1 2024-03-12 20:46 UTC

This package is auto-updated.

Last update: 2024-09-04 10:48:29 UTC


README

Latest Version on Packagist Total Downloads

此包为您的Laravel应用添加多语言支持

  • 多个地区配置
  • 本地化路由
  • 可翻译的路由段
  • 自动检测用户地区

安装

您可以通过composer安装此包

composer require outerweb/localization

Outerweb\Localization\Http\Middleware\SetLocale 中间件添加到 app/Http/Kernel.php 中的 web 中间件组:这将通过以下步骤自动设置每个请求的地区

  1. 检查URL中是否设置了地区(例如 http://example.com/en
  2. 检查cookie中是否设置了地区
  3. 检查用户浏览器首选语言中是否有匹配的地区
  4. 使用备用地区
protected $middlewareGroups = [
    'web' => [
        // ...
        \Outerweb\Localization\Http\Middleware\SetLocale::class,
    ],
];

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="localization-config"

这是发布后的配置文件内容

return [
    /**
     * The cookies that this package will use internally..
     * If your app already uses some other cookie name,
     * you can change it here to make it more uniform.
     */
    'cookies' => [
        'locale' => 'locale',
    ],

    /**
     * If you prefer to define this in config/app.php,
     * leave this as null. It will then fallback to
     * the app.fallback_locale config value.
     */
    'fallback_locale' => null,

    /**
     * If you prefer to define this in config/app.php,
     * leave this as null. It will then fallback to
     * the app.supported_locales config value.
     */
    'supported_locales' => null,

    /**
     * The name of the translations file in the
     * lang directory. (default: routes.php)
     */
    'translations_file_name' => 'routes',
];

使用方法

定义路由

按照以下方式定义您想要本地化的路由

Route::localized(function () {
    Route::get('/', function () {
        return view('welcome');
    })
        ->name('home');
});

这将为您在 supported_locales 配置值中定义的每个地区生成主页路由。

例如,如果您已将 ennl 定义为支持地区,则以下路由将被生成

  • http://example.com/en(路由名称:en.home
  • http://example.com/nl(路由名称:nl.home

您还可以定义一个备用路由,该路由将重定向到本地化路由

Route::fallback(function () {
    return redirect()->localizedRoute('home');
});

翻译路由段

您可以通过将其添加到配置的翻译文件中,将每个路由段翻译为 lang 目录。

例如,如果您定义了一个路由 /about-us 并支持地区 ennl,则可以将其添加到配置的翻译文件中

lang/en/routes.php

return [
    'about-us' => 'about-us',
];

lang/nl/routes.php

return [
    'about-us' => 'over-ons',
];

这将生成以下路由

  • http://example.com/en/about-us(路由名称:en.about-us
  • http://example.com/nl/over-ons(路由名称:nl.about-us

生成本地化URL

当使用本地化路由时,您不能使用 route() 助手生成URL。相反,您可以使用 localizedRoute() 助手

localizedRoute('home'); // http://example.com/en (route name: en.home)
localizedRoute('blog.show', ['blog' => 'my-blog-post']); // http://example.com/en/blog/my-blog-post (route name: en.blog.show)
localizedRoute('home', [], 'nl'); // http://example.com/nl (route name: nl.home)

如上所示,此助手接受与 route() 助手相同的参数。

获取当前路由的本地化路由

您可以通过以下方式获取当前路由的本地化路由

localization()->localizedRoutesForCurrentRoute();

例如,如果您在 'about-us' 页面上,这将返回以下数组

[
    'en' => 'http://example.com/en/about-us',
    'nl' => 'http://example.com/nl/over-ons',
]

这如果您想生成一个语言切换器,可能很有用。

获取特定路由的本地化路由

您可以通过以下方式获取特定路由的本地化路由

localization()->localizedRoutesForRoute('home');

在这个例子中,结果将是

[
    'en' => 'http://example.com/en',
    'nl' => 'http://example.com/nl',
]

这如果您想告诉谷歌页面的其他本地化版本,可能很有用。(有关更多信息,请参阅此文章

变更日志

请参阅CHANGELOG 了解最近更改的内容。

致谢

许可

MIT许可证(MIT)。请参阅许可证文件获取更多信息。