josiasmontag/laravel-localization
Composer 安装命令:
composer require josiasmontag/laravel-localization
包简介
Localization for Laravel framework
README 文档
README
Introduction
The Laravel Localization package is built for Laravel 5.5+ and provides:
- Localized routes with language URL prefixes.
- Domain based localized routes.
- Middleware to detect user language based on HTTP header and cookie.
- Redirect the user to the localized version.
- Possibility to hide the language URL prefix for the default language.
- Possibility to localize a subset of routes only.
- Language Switcher and Hreflang Meta Tags
- Patched
route()method to use localized routes whenever possible. - Compatibility with
artisan route:cache.
Installation
To get started, use Composer to add the package to your project's dependencies:
composer require josiasmontag/laravel-localization
Add the HandleLocalization Middleware to the web group in App/Http/Kernel.php:
protected $middlewareGroups = [ 'web' => [ // Other Middleware \Lunaweb\Localization\Middleware\LocalizationHandler::class, ], ];
Configuration
To publish the config file to config/localization.php:
php artisan vendor:publish --provider "Lunaweb\Localization\LocalizationServiceProvider"
Default configuration:
return [ // Add any language you want to support 'locales' => [ 'en' => ['name' => 'English'], 'de' => ['name' => 'German'], ], // The default locale is configured in config/app.php (locale) // Default locale will not be shown in the url. // If enabled and 'en' is the default language: // / -> English page, /de -> German page // If disabled: // /en -> English Page, /de -> German page 'hide_default_locale_in_url' => true, // Use query parameter if there are no localized routes available. // Set it to null to disable usage of query parameter. 'locale_query_parameter' => 'hl', // Enable redirect if there is a localized route available and the user locale was detected (via HTTP header or session) 'redirect_to_localized_route' => true, // Try to detect user locale via Accept-Language header. 'detect_via_http_header' => true, // Remember the user locale using a cookie. 'remember_via_cookie' => true, // Cookie expire time in minutes 'cookie_expires' => 20160 // 14 days ];
Usage
Prefix Based Localized Routes
To add localized routes with language prefixes, edit your routes/web.php and use localizedRoutesGroup helper:
Localization::localizedRoutesGroup(function() { Route::get('/', 'HomeController@uploadDocuments')->name('index'); Route::get('/register', 'RegisterController@showRegisterForm')->name('register'); });
Under the hood this will create the following routes for you:
| Route | Route Name | Language |
|---|---|---|
/ |
index |
English (Default Language) |
/de |
de.index |
German |
/fr |
fr.index |
French |
/register |
register |
English (Default Language) |
/de/register |
de.register |
German |
/fr/register |
fr.register |
French |
Domain Based Localized Routes
To add domain-based localized routes, add the localized domains to your config/localization.php configuration:
'locales' => [ 'en' => ['domain'=> 'domain.com', 'name' => 'English'], 'de' => ['domain'=> 'domain.de', 'name' => 'German'], 'fr' => ['domain'=> 'domain.fr', 'name' => 'French'], ],
The example from above will then create the following routes:
| Route | Route Name | Language |
|---|---|---|
domain.com |
index |
English (Default Language) |
domain.de |
de.index |
German |
domain.fr |
fr.index |
French |
domain.com/register |
register |
English (Default Language) |
domain.de/register |
de.register |
German |
domain.fr/register |
fr.register |
French |
Localization Specific Routes
You can manually create language specific routes using the localization() macro.
Route::get('/contact', 'ContactController@showContactForm') ->localization('en') ->name('contact'); Route::get('/kontakt', 'ContactController@showContactForm') ->localization('de') ->name('de.contact');
Helpers
Localized route()
route() will automatically use the localized version, if there is any available. Using the example from above, route('index') resolves to the index, de.index or fr.index route depending on the user's language.
Check if the current route is localized
Localization::isLocalizedRoute()
or...
Route::current()->getLocalization() === null
Get the localization of the current route
Route::current()->getLocalization()
Get the URL to a different language version of the current route
Localization::getLocaleUrl($localeCode)
Get the route name to a different language version of the current route
Localization::getLocaleRoute($localeCode)
Hreflang Meta Tags
@if(Localization::isLocalizedRoute()) @foreach(Localization::getLocales() as $localeCode => $properties) <link rel="alternate" hreflang="{{ $localeCode }}" href="{{ Localization::getLocaleUrl($localeCode) }}"> @endforeach @endif
Language Switcher
<ul>
@foreach(Localization::getLocales() as $localeCode => $properties)
<li>
<a rel="alternate" hreflang="{{ $localeCode }}" href="{{ Localization::getLocaleUrl($localeCode, true) }}">
{{ $properties['native'] }} </a>
</li>
@endforeach
</ul>
josiasmontag/laravel-localization 适用场景与选型建议
josiasmontag/laravel-localization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36.87k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2017 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「language」 「laravel」 「localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 josiasmontag/laravel-localization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 josiasmontag/laravel-localization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 josiasmontag/laravel-localization 相关的其它包
同方向 / 同关键字的高下载量 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
Set Links with a specific language parameter
Assign a language to a module
统计信息
- 总下载量: 36.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-04