定制 sinkcup/laravel-locale-setter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sinkcup/laravel-locale-setter

最新稳定版本:1.0.0

Composer 安装命令:

composer require sinkcup/laravel-locale-setter

包简介

detect Accept-Language, setLocale, then you can use Laravel localization

README 文档

README

This package detect Accept-Language in HTTP header(query string and cookie will be comming), setLocale, then you can use Laravel localization for I18N.

CircleCI

Installation

Install the package via Composer:

composer require sinkcup/laravel-locale-setter

Next, add the package's service provider to your config/app.php:

// config/app.php

'providers' => [
    sinkcup\LaravelLocaleSetter\LocaleServiceProvider::class
]

and then you'll just need to publish the package's configuration:

php artisan vendor:publish --provider="sinkcup\LaravelLocaleSetter\LocaleServiceProvider"

which will create config/locale.php.

Usage

Global Middleware

Normally, it should run during every HTTP request, simply add the middleware class to the $middleware property of your app/Http/Kernel.php class. For example:

// app/Http/Kernel.php

protected $middleware = [
    \sinkcup\LaravelLocaleSetter\Http\Middleware\DetectLocale::class,
];

Assigning Middleware To Routes

if you would like to assign middleware to specific routes, you should add the middleware class to the $routeMiddleware property of your app/Http/Kernel.php class. For example:

// app/Http/Kernel.php

protected $routeMiddleware = [
    'locale' => \sinkcup\LaravelLocaleSetter\Http\Middleware\DetectLocale::class,
]

Once the middleware has been defined in the HTTP kernel, you may use the middleware method to assign middleware to a route:

Route::group(['middleware' => ['auth:api', 'locale']], function () {
    Route::resource('/users', 'UserController');
    Route::resource('/photos', 'PhotoController');
});

Config

Linux locale and HTML 4 language code followed RFC1766 which was released at March 1995. It's out-of-date.

Due to historical reasons, Linux locale and browsers are still using old standard at now.

iOS want to change it, use zh-Hans and zh-Hans-CN, but it's wrong too.

If you want to do something right, follow the modern standard - BCP47 which HTML5 using, and map old language codes to new.

You should use cmn-Hans, cmn-Hans-CN, cmn-Hant and cmn-Hant-TW, instead of zh-CN, zh-TW, zh-Hans and zh-Hant.

create dirs:

    resources/lang/cmn_Hans
    resources/lang/cmn_Hant

then use:

// config/locale.php

'locale_map' => [
    'cmn_hans' => 'cmn_Hans',
    'zh_cn' => 'cmn_Hans',
    'zh_hans' => 'cmn_Hans',
    'cmn_hant' => 'cmn_Hant',
    'zh_hant' => 'cmn_Hant',
    'zh_hk' => 'cmn_Hant',
    'zh_tw' => 'cmn_Hant',
],

If you care about regional differences, create dirs:

    resources/lang/cmn_Hans_CN
    resources/lang/cmn_Hans_HK
    resources/lang/cmn_Hant_HK
    resources/lang/cmn_Hant_TW

then use:

// config/locale.php

'locale_map' => [
    'cmn_hans_cn' => 'cmn_Hans_CN',
    'zh_cn' => 'cmn_Hans_CN',
    'cmn_hans_hk' => 'cmn_Hans_HK',
    'cmn_hant_hk' => 'cmn_Hant_HK',
    'zh_hk' => 'cmn_Hant_HK',
    'cmn_hant_tw' => 'cmn_Hant_TW',
    'zh_tw' => 'cmn_Hant_TW',
],

统计信息

  • 总下载量: 3.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-18