vluzrmos/language-detector

通过浏览器偏好设置、子域名或路由前缀检测您应用程序的语言。

v2.3.4 2023-02-28 17:04 UTC

README

Join the chat at https://gitter.im/vluzrmos/laravel-language-detector

Latest Stable Version Total Downloads License

Build Status Scrutinizer Code Quality Code Climate Codacy Badge StyleCI

此包提供了一种简单的方法,通过使用浏览器偏好设置子域名路由前缀来检测并应用您应用程序的语言。

安装

使用composer安装包

composer require vluzrmos/language-detector

按照以下方式添加服务提供者

Laravel

对于Laravel 5.5及以上版本,此步骤不是必需的。

编辑您的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 --provider="Vluzrmos\LanguageDetector\Providers\LanguageDetectorServiceProvider"

然后编辑新的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)已修复此问题。

别名语言区域

您可以在resources/lang目录中使用lang_LOCALE样式或仅使用lang。您选择的检测驱动程序将尝试检测与您的config/lang-detector.php中可用的langlang_LOCALE匹配的语言。

'languages' => ['en', 'pt_BR' ...]

示例

├── lang
│   ├── en
│   │   ├── messages.php
│   │   └── validation.php
│   └── pt_BR
│       ├── messages.php
│       └── validation.php

如果您不遵循语言名称的此样式或如果您正在使用subdomainuri驱动程序,只需在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 许可证。