alexjoffroy / laravel-localization
一个易于处理的 Laravel 本地化包
Requires
- php: ^7.1
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0
- phpunit/phpunit: ^6.3|^7.0
This package is not auto-updated.
Last update: 2024-09-19 17:39:09 UTC
README
这个 Laravel 包使您能够轻松处理应用程序中的本地化。它提供了一组辅助函数,您基本上可以执行以下操作:
GET /en/about # Displays the about page in english GET /fr/a-propos # Displays the about page in french
您仍然可以使用 Laravel 核心功能,如测试、路由缓存、语言文件等。
目录
安装
您可以通过 composer 安装此包
composer require alexjoffroy/laravel-localization
此包将自动注册自己。
可选地,您可以发布配置文件 config/localization.php
php artisan vendor:publish --provider="AlexJoffroy\Localization\LocalizationServiceProvider --tag="config"
如果您想自定义 默认切换视图
php artisan vendor:publish --provider="AlexJoffroy\Localization\LocalizationServiceProvider --tag="views"
配置
默认区域设置
默认区域设置可以在配置文件中更改。默认情况下,此值与 Laravel 的 fallback_locale
相同(在 config/app.php
中设置)。
'default_locale' => config('app.fallback_locale'),
支持的区域设置
您可以在这里列出您想要支持的所有区域设置。
每个键应该是一个区域设置代码(en、fr、...)。
native
字段是将在切换视图中渲染的标签。regional_code
、charset
和 constants
字段是使用 PHP 的 setlocale 所必需的,该函数在 Localization::setLocale
中调用。这对于像 Carbon 中的日期格式化这样的操作很有用。
'supported_locales' => [ 'en' => [ 'native' => 'English', 'regional_code' => 'en_GB', 'charset' => 'UTF-8', 'constants' => ['LC_TIME'], ], 'fr' => [ 'native' => 'Français', 'regional_code' => 'fr_FR', 'charset' => 'UTF-8', 'constants' => ['LC_TIME'], ], ],
在 URL 中隐藏默认区域设置
默认情况下,所有 URL 都将使用区域设置进行前缀。
# `en` is default locale GET /en/about # Displays the about page in english GET /fr/a-propos # Displays the about page in french
当将 hide_default_locale_in_url
设置为 true 时,将移除默认区域设置的此前缀。
'hide_default_locale_in_url' => false,
# `en` is default locale GET /about # Displays the about page in english GET /fr/a-propos # Displays the about page in french
用法
注册中间件
第一步是将 SetLocaleFromCurrentRoute
中间件注册到您的应用程序中。此中间件将从 URL 中猜测并设置当前区域设置。最简单的方法是全局注册它
// app/Http/Kernel.php protected $middleware = [ // ... \AlexJoffroy\Localization\Http\Middlewares\SetLocaleFromCurrentRoute::class, ];
添加您的路由
现在,您可以使用方便的辅助函数注册您的区域设置路由
// routes/web.php Route::locales(function() { Route::get( trans('routes.about'), 'App\Http\Controllers\AboutController@index' )->name('about'); });
注意:所有在 locales
组内定义的路由都应该有名称。
根据您支持 en
和 fr
区域设置,并且您为 routes.about
定义了翻译,上面的代码将注册这些路由
本地化实例
\AlexJoffroy\Localization\Localization
类提供了一组有助于在您的应用程序中使用的方法。该对象作为单例注册,可以通过应用程序容器、L10n
外观或 l10n()
辅助函数访问。
$l10n = app('localization'); // or $l10n = L10n::getFacadeRoot(); // or $l10n = l10n();
获取/设置区域设置
// Given `en` is the current locale $l10n->getLocale(); // `en` $l10n->setLocale('fr'); // Set the current locale to `fr`
Localization::getLocale
和 Localization::setLocale
是 App::getLocale
和 App::setLocale
的别名
检查
// Given `en` is the current locale $l10n->isCurrentLocale('en'); // true $l10n->isCurrentLocale('not-current'); // false $l10n->isSupportedLocale('en'); // true $l10n->isSupportedLocale('not-supported'); // false // Given `en` is the default locale $l10n->isDefaultLocale('en'); // true $l10n->isDefaultLocale('not-default'); // false
获取配置值
$l10n->getSupportedLocales(); // All supported locales (from supported_locales) $l10n->getSupportedLocale('en'); // Given supported locale (from supported_locales) $l10n->getSupportedLocaleKeys(); // All supported locale keys (from supported_locales) $l10n->getDefaultLocale(); // Default locale (from default_locale) // Given `en` is the default locale $l10n->shouldHideLocaleInUrl('en'); // True if `hide_default_locale_in_url` is true $l10n->shouldHideLocaleInUrl('fr'); // False, even if `hide_default_locale_in_url` is true
生成路由
$l10n->route('about', [], true, 'en'); // `https://yourapp.com/en/about` $l10n->route('about', [], false, 'en'); // `/en/about` $l10n->route('about', [], true, 'fr'); // `https://yourapp.com/fr/a-propos` // Shortcut will fallback to current locale $l10n->route('about'); // `https://yourapp.com/en/about` // Given the current app url is `https://yourapp.com/en/about` $l10n->currentRoute('fr'); // `https://yourapp.com/fr/a-propos` $l10n->currentRoute('fr', false); // `/fr/a-propos`
渲染切换
应在 Blade 视图中调用此函数,例如 {{ l10n()->renderSwitch() }}
。当使用自定义视图时,您可以直接从 $l10n
变量中访问本地化实例。
// Default view $l10n->renderSwitch(); // Custom view $l10n->renderSwitch('path.to.view'); // Custom view, with additional data $l10n->renderSwitch('path.to.view', ['foo' => 'bar']);
自定义视图示例
<select name="switch" id="switch"> @foreach($l10n->getSupportedLocales() as $locale => $localeSettings) <option value="{{ $locale }}" {{ $l10n->isCurrentLocale($locale) ? 'selected' : '' }}> {{ ucfirst($localeSettings['native']) }} </option> @endforeach </select>
外观
本地化方法也可从 L10n
外观中获取。
L10n::getLocale(); L10n::setLocale(); L10n::route(); L10n::currentRoute(); // etc
辅助函数
// Get the Localization instance $l10n = l10n(); // Get the current locale $current = locale(); // Set the current locale locale('en'); // Get supported locale $supported = locales();
测试
composer test
变更日志
有关最近更改的详细信息,请参阅 CHANGELOG
贡献
有关详细信息,请参阅 CONTRIBUTING
安全
如果您发现任何安全问题,请通过电子邮件发送至 hello@alexjoffroy.me,而不是使用问题跟踪器。
致谢
此软件包最初是在Spatie在 spatie/skeleton-php 提供的软件包骨架上构建的。
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可文件。