mikehaertl/localeurls
Composer 安装命令:
composer require mikehaertl/localeurls
包简介
localeurls is a Yii extension for automatic locale/language management through URLs.
README 文档
README
Automatic locale/language management for URLs.
This extension allows to use URLs that contain a language code like
/en/some/page
/de/some/page
http://www.example.com/en/some/page
http://www.example.com/de/some/page
Since 1.1.3 you can also configure friendlier language names if you want:
http://www.example.com/english/some/page
http://www.example.com/deutsch/some/page
The language code is automatically inserted into every URL created and read back on every request. No extra URL rules are required. For best user experience the language is also restored from session/cookie if the user returns to a URL without a language code.
Installation
Extract the package to your protected/extensions directory and rename
it to localeurls. Then configure the components in your protected/config/main.php:
<?php return array( // ... 'components' => array( // ... 'urlManager' => array( 'class' => 'ext.localeurls.LocaleUrlManager', // Advanced configuration with defaults (see below) //'languageParam' => 'language', ), 'request' => array( 'class' => 'ext.localeurls.LocaleHttpRequest', 'languages' => array('en_us','en','de','fr'), // Since version 1.1.3 you can also map url => language // 'languages' => array( // 'english' => 'en', // 'deutsch' => 'de', // 'fr', // ) // Advanced configuration with defaults (see below) //'detecLanguage' => true, //'languageCookieLifetime' => 31536000, //'persistLanguage' => true, //'redirectDefault' => false, ), // ... ), );
NOTE: You need to configure all available languages including the default language of your application. More specific language codes should come first, e.g.
en_usbeforeenabove.
Mode of operation
With the above configuration in place you're all set to go. If a request comes in
that has no language set, the component will try to auto detect the language from
the HTTP headers and redirect to that URL, for example to www.example.com/fr. If
fr is the default language of your application (i.e. what you configured in
your main.php), it will not redirect - unless you set redirectDefault
to true.
NOTE: If
redirectDefaultis enabled you can't accesswww.example.comanymore because you're always redirected to a specific language URL even for the default application language.
All URLs you create with any createUrl() and createAbsoluteUrl() method will
also contain the current visitor's language in their URL. The same rules apply for
the default language: Unless you set redirectDefault to true, the URLs you create
will not contain a language code if the visitor uses the default language.
To let your users switch to another language, you can create URLs with the usual methods
and add a language parameter there:
<?php $germanUrl = $this->createUrl('site/contact', array('language' => 'de'));
Once a user visited a URL with a language code in it, this language is stored in his session. If the user returns to the start page (or any other page) without a language in the URL, he's automatically redirected to his last language choice. See below for a useful widget that creates a simple language selector.
API
LocaleHttpRequest
Properties
detectLanguage: Wether to auto detect the preferred user language from the HTTP headers. Default istrue.languageCookieLifeTime: How long to store the user language in a cookie. Default is 1 year. Set tofalseto disable cookie storage.languages: Array of available language codes. More specific patterns must come first, i.e.en_usmust be listed beforeen. The default language from the application configuration must also be listed here. Since 1.1.3 you can also map URL values to languages:array('de','fr','english'=>'en').persistLanguage: Wether to store the user language selection in session and cookie. If the user returns to any page without a language in the URL, he's redirected to the stored language's URL. Default istrue.redirectDefault: Wether to also redirect the application's default language, i.e. fromwww.example.comtowww.example.com/enif the main application language isen. Default is false.
Methods
getDefaultLanguage(): Returns the default language as it was configured in the main application configuration before it was overwritten during language detection
LocaleUrlManager
Properties
languageParam: Name of the parameter that contains the desired language when constructing a URL. Default islanguage.
How to switch languages
Here's a simple example how you can create a language selector widget. It creates a dropdown taylored towards the popular Bootstrap framework.
<?php class LanguageSelector extends CWidget { public function run() { $app = Yii::app(); $route = $app->controller->route; $languages = $app->request->languages; $language = $app->language; $params = $_GET; echo CHtml::link($language. ' <b class="caret"></b>', '#', array( 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown' )); array_unshift($params, $route); echo '<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">'; foreach($languages as $lang) { if($lang===$language) continue; $params['language'] = $lang; echo '<li>'.CHtml::link($lang,$params ).'</li>'; } echo '</ul>'; } }
Changelog
1.1.6
- Fix #8: URLs that contain the language code again are messed up
1.1.5
- Fix #4: Query parameters lost when switching to default language
1.1.4
- Fix #3: Could not create URL to switch to default language
1.1.3
- Add mapping feature.
- Add debug output under category
ext.localeurls(only ifYII_DEBUGis set)
Upgrade
From 1.0.0
- The parameter
defaultLanguagewas removed. You should configure the default language in your main application config instead. If you want to redirect to your default language, you can setredirectDefaultto true.
mikehaertl/localeurls 适用场景与选型建议
mikehaertl/localeurls 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 793 次下载、GitHub Stars 达 10, 最近一次更新时间为 2013 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「url」 「locale」 「language」 「yii」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mikehaertl/localeurls 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mikehaertl/localeurls 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mikehaertl/localeurls 相关的其它包
同方向 / 同关键字的高下载量 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
Easy URL rewrites in your Laravel application
统计信息
- 总下载量: 793
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-02-20