swayok / laravel-language-detector
使用浏览器偏好设置、子域名或路由前缀检测您应用程序的语言。
Requires
- illuminate/config: ~5.0
- illuminate/cookie: ~5.0
- illuminate/support: ~5.0
- illuminate/translation: ~5.0
Requires (Dev)
- fabpot/php-cs-fixer: ~1.11
- mockery/mockery: 0.9.*
- orchestra/testbench: 3.0.*
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-14 03:32:56 UTC
README
此包提供了一种简单的方法,通过使用浏览器偏好设置、子域名或路由前缀,来检测并应用您应用程序的语言。
安装
使用Composer安装包
composer require vluzrmos/language-detector
如下添加服务提供者
Laravel
编辑您的config/app.php
将以下代码行插入到列出的RouteServiceProvider之上,例如
Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class,
App\Providers\RouteServiceProvider::class,
::class表示法是可选的。
Lumen
编辑bootstrap/app.php
$app->register(Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class);
::class表示法是可选的。
配置
Laravel有两种选择,要么使用以下命令发布包配置
php artisan vendor:publish
然后编辑新的config/lang-detector.php
文件,或者将以下行添加到您的.env文件中
#Indicates whether the language should be autodetected (can be removed) LANG_DETECTOR_AUTODETECT=true #Driver to use, the default is browser LANG_DETECTOR_DRIVER="browser" #Segment to use in the uri or subdomain driver, default 0 (can be removed) LANG_DETECTOR_SEGMENT=0 #Name of cookie to cache the detected language (use false|null to disable) LANG_DETECTOR_COOKIE=locale #Comma-separated list of languages provided on the app LANG_DETECTOR_LANGUAGES="en,fr,pt_BR" #To alias the language use the notation ":", "=", ":=" or "=>" to separate the alias and its value. # LANG_DETECTOR_LANGUAGES="en, en-us:en, pt-br:pt_BR"
如果您不想使用它,只需使用php artisan vendor:publish
发布包配置,然后在生成的config/lang-detector.php
上进行编辑。
对于Lumen,请考虑将vendor/vluzrmos/language-detector/config/lang-detector.php
复制到您的configs目录,并在注册LanguageDetectorServiceProvider
之前使用$app->configure('lang-detector')
。
检测驱动程序
有几个驱动程序可以使用,请选择一个与您的应用程序设计相匹配的驱动程序
浏览器偏好设置
驱动程序browser
将尝试根据请求语言(浏览器偏好设置)检测应用程序的语言。此驱动程序不需要其他配置,只需配置可用的语言。
子域名
驱动程序subdomain
将尝试检测与主机名子域名匹配的应用程序语言。例如。
http://fr.site.domain
subdomain
驱动程序将检测语言fr
并将应用程序设置为fr
,如果它是lang-detector
配置文件中可用的语言之一。
注意:子域名和uri驱动程序需要在lang-detector配置文件中对语言-locales进行别名。
路由前缀
驱动程序uri
将尝试根据路由前缀检测语言
http://site.domain/en-us/home
该驱动程序将检测en-us并将其设置为应用程序。(注意:考虑别名该区域设置)
并且不用担心,如果URL如下
http://site.domain/home
语言不会改变,应用程序将使用您在config/app.php
文件上配置的默认语言。
使用uri
驱动程序,您的路由组需要如下所示
//That would be nice if you put (edit) it on your App\Providers\RouteServiceProvider. // Laravel Route::group(['prefix' => app('language.routePrefix'), 'namespace' => 'App\Http\Controllers'], function ($router) { require __DIR__.'/../Http/routes.php'; }); //For lumen, it should be on bootstrap/app.php. // Lumen $app->group(['prefix' => app('language.routePrefix'), 'namespace' => 'App\Http\Controllers'], function ($app) { require __DIR__.'/../app/Http/routes.php'; }
问题:Lumen 5.0不支持空字符串的路由前缀,您应该使用以下脚本
$prefix = app('language.routePrefix'); $options = []; if (!empty($prefix) && $prefix!="/") { $options['prefix'] = $prefix; } // any other options here $options['namespace'] = 'App\Http\Controllers'; $app->group($options, function () use($app) { // ... });
注意:这仅适用于Lumen 5.0,最新版本(5.1)已经修复了这个问题。
语言区域设置别名
您可以使用lang_LOCALE
样式或仅在resources/lang
目录中使用lang
。您选择的语言检测驱动程序将尝试检测与您的config/lang-detector.php
中可用的lang
或lang_LOCALE
匹配的语言。
'languages' => ['en', 'pt_BR' ...]
示例
├── lang
│ ├── en
│ │ ├── messages.php
│ │ └── validation.php
│ └── pt_BR
│ ├── messages.php
│ └── validation.php
如果您不遵循这种语言名称风格或如果您正在使用subdomain
或uri
驱动程序,只需在config/lang-detector.php
文件上配置它
'languages' => [ 'pt_BR' => 'pt-BR', //will detect pt_BR language, and set pt-BR to the application, 'pt' => 'pt-BR', //aliasing, will detect pt and set pt-BR to the application 'pt-br' => "pt-BR", //aliasing, will detect pt-br and set pt-BR to the application (you will need it with subdomain driver) 'en', //will detect 'en' language ]
或者如果您正在使用.env
而不是配置文件
#Just put the languages in a comma-separated string.
#To aliase the language use the notation ":", "=", ":=" or "=>" to separate the alias and its value.
LANG_DETECTOR_LANGUAGES="pt_BR:pt-BR, pt:pt-BR, pt-br:pt-BR, en"
建议
默认Laravel语言行已翻译成51种语言,在这里提供
如果您想翻译您的模型,可以使用此包
许可协议
MIT.