outerweb/localization 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

outerweb/localization

Composer 安装命令:

composer require outerweb/localization

包简介

This package adds multi-language support to your Laravel application.

README 文档

README

Latest Version on Packagist Total Downloads

This package adds multi-language support to your Laravel application:

  • Multiple locales configuration
  • Localized routes
  • Translatable route segments
  • Automatic user locale detection

Installation

You can install the package via composer:

composer require outerweb/localization

Add the Outerweb\Localization\Http\Middleware\SetLocale middleware to the web middleware group in app/Http/Kernel.php: This will automatically set the locale for each request by going through the following steps:

  1. Check if the locale is set in the URL (e.g. http://example.com/en)
  2. Check if the locale is set in a cookie
  3. Check for a matching locale in the preferred languages of the user's browser
  4. Use the fallback locale
protected $middlewareGroups = [
    'web' => [
        // ...
        \Outerweb\Localization\Http\Middleware\SetLocale::class,
    ],
];

You can publish the config file with:

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

This is the contents of the published config file:

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',
];

Usage

Defining routes

Define the routes you want to localize like this:

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

This will then generate the home route for each locale you have defined in the supported_locales config value.

For example, if you have defined en and nl as supported locales, the following routes will be generated:

  • http://example.com/en (route name: en.home)
  • http://example.com/nl (route name: nl.home)

You can also define a fallback route that will redirect to the localized route:

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

Translating route segments

You can translate each route segment by adding it to the configured translations file in the lang directory.

For example, if you defined a route /about-us and you support the locales en and nl, you can add this to the configured translations files:

In lang/en/routes.php:

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

In lang/nl/routes.php:

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

This will then generate the following routes:

  • http://example.com/en/about-us (route name: en.about-us)
  • http://example.com/nl/over-ons (route name: nl.about-us)

Generating localized URLs

When using localized routes, you cannot use the route() helper to generate URLs. Instead, you can use the localizedRoute() helper:

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)

As you can see above, this one takes the same parameters as the route() helper.

Getting the localized routes for the current route

You can get the localized routes for the current route like this:

localization()->localizedRoutesForCurrentRoute();

This will for example return the following array if you are on the 'about-us' page:

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

This can be useful if you want to generate a language switcher.

Getting the localized routes for a specific route

You can get the localized routes for a specific route like this:

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

In this example, the result would be:

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

This can be useful if you want to tell Google about the other localized versions of a page. (See this article for more information)

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

outerweb/localization 适用场景与选型建议

outerweb/localization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 122 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 outerweb/localization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 outerweb/localization 我们能提供哪些服务?
定制开发 / 二次开发

基于 outerweb/localization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 122
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-26