vluzrmos/language-detector
Composer 安装命令:
composer require vluzrmos/language-detector
包简介
Detect the language for your application using browser preferences, subdomains or route prefixes.
README 文档
README
This package provides an easy way to detect and apply the language for your application using browser preferences, subdomains or route prefixes.
Installation
Require the package using composer:
composer require vluzrmos/language-detector
Add the service provider as follows:
Laravel
This is not needed for Laravel 5.5 and upper version.
Edit your config/app.php:
Insert this line of code above the listed RouteServiceProvider, ex:
Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class,
App\Providers\RouteServiceProvider::class,
::class notation is optional.
Lumen
Edit the bootstrap/app.php:
$app->register(Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider::class);
::class notation is optional.
Configuration
Two options for Laravel, either publish the package configuration using:
php artisan vendor:publish --provider="Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider"
then edit the new config/lang-detector.php file or add the following lines to your .env file:
#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"
If you not want to use that, just publish the configurations of the package with
php artisan vendor:publish and edit on config/lang-detector.php generated.
For Lumen, consider to copy vendor/vluzrmos/language-detector/config/lang-detector.php
to your configs dir and use $app->configure('lang-detector') before register the
LanguageDetectorServiceProvider.
Detector Drivers
There are a few drivers that you might to use, choose one which matches with your application design:
Browser Preferences
The driver browser will try to detect the language of the application based on the request languages (browser preferences). This driver doesn't need any other configuration, just configure the available languages.
Subdomains
The driver subdomain will try to detect the language of the application which matches with subdomain of the hostname.
eg.:
http://fr.site.domain
The subdomain driver will detect language fr and set the application to fr if it is one of the available languages in the lang-detector config file.
Note: subdomain and uri drivers require aliases of the the language-locales in the lang-detector config file.
Route Prefixes
The driver uri will try to detect the language based on the route prefix:
http://site.domain/en-us/home
That driver will detect en-us and set it to the application. (Note: Consider to aliase that locale)
And don't worry, if the url is like:
http://site.domain/home
The language will not be changed, the application will use your default language configured on your config/app.php file.
With uri driver, your route group needs be like this:
//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'; }
Issue: Lumen 5.0 doesn't support route prefix with empty strings, you should use that script:
$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) { // ... });
Note: That is only for Lumen 5.0, the newest version (5.1) already fixes it.
Aliasing language locales
You might to use the style lang_LOCALE or just lang on your resources/lang dir.
The language detector driver you have chosen will try to detect the language
which matches with lang or lang_LOCALE available on your config/lang-detector.php.
'languages' => ['en', 'pt_BR' ...]
example:
├── lang
│ ├── en
│ │ ├── messages.php
│ │ └── validation.php
│ └── pt_BR
│ ├── messages.php
│ └── validation.php
If you are not following that style of languages names or if you are using
the subdomain or uri drivers, just configure it on config/lang-detector.php file:
'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 ]
or if you are using .env instead of config file:
#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"
Suggestions
The default Laravel language lines are available translated into 51 languages here:
If you want to translate your models you can use this package:
License
MIT.
vluzrmos/language-detector 适用场景与选型建议
vluzrmos/language-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 572.43k 次下载、GitHub Stars 达 109, 最近一次更新时间为 2015 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「locale」 「language」 「laravel」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vluzrmos/language-detector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vluzrmos/language-detector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vluzrmos/language-detector 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
A custom URL rule class for Yii 2 which allows to create translated URL rules
Set Links with a specific language parameter
统计信息
- 总下载量: 572.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 111
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-03