benconstable/laravel-localize-middleware
Composer 安装命令:
composer require benconstable/laravel-localize-middleware
包简介
Configurable localization middleware for your Laravel >=5.1 application
README 文档
README
Configurable localization middleware for your Laravel >=5.1 application.
This package provides a simple set of configuration and middleware to allow you to automatically set your application's locale using the current request. You can set the locale from a request parameter, header, the current host, a cookie or session data.
Installation
Install the package via Composer:
$ composer require benconstable/laravel-localize-middleware
Next, add the package's service provider to your config/app.php:
// config/app.php 'providers' => [ BenConstable\Localize\LocalizeServiceProvider::class ]
and then you'll just need to publish the package's configuration:
$ php artisan vendor:publish --provider="BenConstable\Localize\LocalizeServiceProvider"
which will create config/localize-middleware.php.
Usage
Out-of-the-box, the package is configured to set the application locale using a
request parameter called locale (see the next section for more info). To enable
this functionality, just register the provided middleware in your app/Http/Kernel.php class:
// app/Http/Kernel.php protected $middleware = [ \BenConstable\Localize\Http\Middleware\Localize::class ];
It's recommended to set this middleware globally and early in the stack, but you're free to register it in whatever way that suits your needs.
Configuration
Configuration can be found at config/localize-middleware.php. From there, you
can configure which localization determiner you'd like to use in your application
and set options for it. You simply have to change the driver option.
The list of available determiners is shown below.
Determining the locale from a request parameter
Driver name: parameter
The default determiner sets the application locale from a request parameter
called locale. You can change this using the parameter configuration option.
The parameter will be discovered in the query string or request body.
Determining the locale from a request header
Driver name: header
This determiner sets the application locale from a request header, which defaults
to Accept-Language. You can change this using the header configuration option.
Aside: For information on using Accept-Language to determine the locale,
see this info from the W3C.
Determining the locale using the current host
Driver name: host
This determiner sets a different application locale depending on the current host.
You'll need to set a map of your application's locales to hosts using the hosts
configuration option.
Determining the locale from a cookie
Driver name: cookie
This determiner sets the application locale from a cookie called locale. You can
change this using the cookie configuration option.
Determining the locale from the session
Driver name: session
This determiner sets the application locale from a session value called locale.
You can change this using the session configuration option.
Using multiple determiners
Sometimes it might be useful to try and determine the locale from more than one
source. If you'd like to do this, just set the driver configuration option to an
array of other driver names. For example:
'driver' => [ 'cookie', 'parameter' ]
The locale will then be deteremined from whichever determiner first provides a successful match, so make sure you add the drivers in the correct order (earliest in the array will be used first).
Determining the locale outside of middleware
You don't have to use the provided middleware if you don't want to. You can instead write your own, or avoid using middleware entirely.
To determine the locale in your own code, first register an alias for the provided
facade (which is actually a reference to BenConstable\Localize\DeterminerManager, if
you want to inject it).
// config/app.php 'aliases' => [ 'Localizer' => BenConstable\Localize\DeterminerFacade::class ]
Then, you can just do:
$locale = Localizer::determineLocale($request);
to determine the locale and do with it what you like.
Contributing
See CONTRIBUTING.md.
Other Localization Projects
Here are some other Laravel localization projects that might be useful to you:
License
MIT © Ben Constable 2017. See LICENSE for more info.
benconstable/laravel-localize-middleware 适用场景与选型建议
benconstable/laravel-localize-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.39k 次下载、GitHub Stars 达 91, 最近一次更新时间为 2016 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「laravel」 「localize」 「localise」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 benconstable/laravel-localize-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 benconstable/laravel-localize-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 benconstable/laravel-localize-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
An easy automatic database driven translator for Laravel 5.*
PSR-15 middleware to handle CSRF-token verification
A simple HTTP application builder using PSR-15 HTTP Server Request Handler and Middleware.
统计信息
- 总下载量: 31.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 92
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-19