kingscode/laravel-localize
Composer 安装命令:
composer require kingscode/laravel-localize
包简介
Laravel Localization in a yiffy.
README 文档
README
An as minimalistic as possible localization package, makes it so the app.locale is set to nl in the case of /nl/home.
Installation
Require the package.
composer require kingscode/laravel-localize
... and optionally publish the config.
php artisan vendor:publish --provider="KingsCode\LaravelLocalize\LocalizeServiceProvider"
Add the middleware to the needed middleware groups, in our case only to the web group.
<?php namespace App\Http; class Kernel extends HttpKernel { ... /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ ... LocaleSelector::class, // <<<<<< Here it is. SubstituteBindings::class, // Its best to register before substitute bindings. ... ], ]; ... }
Now you must register the route file that holds your localized routes, you will do this twice:
- Once without a prefix and prefixed name.
- Once with a prefix and prefixed name.
This makes it so that your default locale can use / instead of being forced to have /en when your website is english by default.
<?php use Illuminate\Contracts\Config\Repository; use Illuminate\Routing\Router; use KingsCode\LaravelLocalize\Localize; class RouteServiceProvider extends ServiceProvider { public function map() { // Web routes without localization, like a "/file/{file}" route or such. $this->mapWebRoutes(); // And as the last method call you'll do the localized web routes. $this->mapLocalizedWebRoutes(); } /** * Define the localized "web" routes for the application. * * @return void */ protected function mapLocalizedWebRoutes() { // We'll take stuff from the config to keep things easily configurable. // Not a must, but it prevents you from having to override stuff. $config = $this->app->make(Repository::class); // We'll need a router to register routes duh. $router = $this->app->make(Router::class); /** @var Localize $localize */ $localize = $this->app->make(Localize::class); // Okay so here is an IMPORTANT part. // Register the {locale} routes first otherwise {locale}/{any} will not be reachable and {any} will catch everything. $router->middleware('web') ->namespace($this->namespace) ->prefix('{' . $config->get('localize.route_parameter_key') . '}') // We add the prefix. ->where([$config->get('localize.route_parameter_key') => $localize->getRouteRegex()]) ->name($config->get('localize.route_name_prefix') . '.') // And the name prefix. ->group(base_path('routes/localized.web.php')); $router->middleware('web') ->namespace($this->namespace) ->group(base_path('routes/localized.web.php')); } }
kingscode/laravel-localize 适用场景与选型建议
kingscode/laravel-localize 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 907 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 01 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kingscode/laravel-localize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kingscode/laravel-localize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 907
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-31