dijix / setlocale
Slim 框架中间件,用于设置您的应用程序的区域设置
dev-master
2017-10-06 10:55 UTC
Requires
- php: >=5.6.0
- psr/http-message: ^1.0
Requires (Dev)
- slim/slim: ^3.0
This package is auto-updated.
Last update: 2024-09-12 06:11:54 UTC
README
描述
Slim 框架中间件,用于设置您的应用程序的区域设置
此中间件将您的应用程序的区域设置设置为您的应用程序支持的区域设置和您的访客首选区域设置之间的最佳匹配。
它旨在与Slim 框架一起使用,但与PSR7兼容,因此也可以在其他地方使用。
访客的区域设置将按照以下顺序确定
- 传递给中间件构造函数的重写,例如来自现有的cookie/session
- URI的第一个部分,例如:example.com/en/welcome 或 example.com/en-gb/welcome。
- 用户的浏览器accept-language头,其中它选择最佳匹配
- 传递的默认区域设置,例如:en_GB,在上述任何一项不匹配时用作后备
当找到匹配项时,中间件将设置
- 请求属性 'locale'
- 响应 'Content-language' 标头
- 它将通过在匹配的区域设置上调用setlocale(LC_ALL)来设置环境 - 这可以通过set_locale标志切换。
安装
通过composer安装
$ composer require dijix/setlocale
使用方法
// In Slim PHP framework 3 // add the middleware to your app, often in the middleware.php or dependencies.php file // pass your settings as an array to the constructor. $app->add(new Dijix\Locale\setLocaleMiddleware([ // set the locales supported by your application "app_locales" => ["de_DE", "en_GB", "fr_FR", "pt_PT"], // set a default locale to fallback on if no match is found "app_default" => "en_GB", // call PHP setlocale(LC_ALL) to set the visitors locale? "set_locale" => true, // strict or partial matching of the locale codes, e.g. "en" matches "en_GB" "strict_match" => false, // override uri/headers locale, useful when setting locale from a cookie or user session "override" => "pt_PT" ]));
区域设置中间件将设置一个请求属性,可以通过以下方式访问
$locale = $request->getAttribute('locale'); // sets $locale to "en_GB"