This extension will set up your applications target language according to visitors browser setting, accepted languages list and country code passed in url.
Resources ¶
Change Log ¶
June 11, 2013 ¶
Updated to 1.2 REMOVED (Dead end) Actually improvements in langhandler are not needed. Currently language detection suffers from 2429 bug, which is particularly burdensome in Android browser.
-Improved language detection in non strict mode.
October 18, 2011 ¶
- Updated to 1.1
-read update section above for details.July 17, 2009 ¶
- Bugfix #1
-small typo fixed Thx to cosimfi74July 12, 2009 ¶
- Initial release.
Documentation ¶
Update to 1.1 (18.10.2011) ¶
After a long time langhandler has been updated to version 1.1 (version of langhandler, not Yii). There is an optional boolean parameter $strict available now. It defaults to false and can be set to true like any other component attribute:
'ELangHandler' => array (
'class' => 'application.extensions.langhandler.ELangHandler',
'languages' => array('pl','en','de'),
'strict' => true,
),
If $strict is left to default false it will not try to compare browser's preferred language exactly like reported, instead it will try to extract language code only. When it is set to true langhandler functionality will not be changed compared to initial release.
EXAMPLE
- prefered language (browser) => pl-PL; $strict=false; langhandler will use pl when searching languages array for supported codes.
- prefered language (browser) => pl-PL; $strict=true; langhandler will use __pl_PL__ when searching languages array for supported codes.
Notice that when language is reported with area code it will always be converted to canonical form with underscore separating codes. So if area matters for you (you may want to set locale based on area not only language) set $strict to true and insert codes pairs (ll_AA format) in languages array.
WARNING
This update changes defaults compared to initial release, so if you are using llAA codes now remember to set $strict_ to true or your application may (and probably will) misbehave after updating to langhandler 1.1.
Requirements ¶
- Yii 1.0 or above
Installation ¶
- Extract the release file under
protected/extensions
- add ELangHandler to preload array in main config file
'preload'=>array('log','ELangHandler'),
- add ELangHandler configuration in components array
'ELangHandler' => array (
'class' => 'application.extensions.langhandler.ELangHandler',
'languages' => array('pl','en','de'),
),
COMMENT: languages array should contain languages codes which will be accepted by application. sourceLanguage code doesn't have to be inserted, however it may be useful in some situations (will be explained later).
- update urlManager configuration in components array
'urlManager'=>array(
'class'=>'application.extensions.langhandler.ELangCUrlManager',
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<lang:(pl|en|de)>/<_c>/<_a>/' => '<_c>/<_a>',
),
),
COMMENT: urlFormat and showScriptName aren't required but recommended. Route rule and class override are a required minimum.
Usage ¶
After component has been configured like in examples above, application will support 3 target languages Polish (pl), English (en), German (de) and source language (yii defaults to en_us). Once language has been chosen langhandler will make sure that its code is passed in url in every request. If language is not specified in url, langhandler will try to recognize browser preffered language and use it, if its code is present in languages array in configuration file. If browser prefered language is not supported by your app, first language from configuration array will be used.
EXAMPLE
source language (en) supported languages (de,pl)
- prefered language (browser) => pl; application lang => pl because pl is in supported languages array
- prefered language (browser) => en; application lang => en because source language is always a supported language
- prefered language (browser) => jp; application lang => de because japanese language is not supported and first language from supported languages array is used. Thats why sometimes its worth to add sourceLanguage to langhandlers languages array.
When adding or removing supported languages remember to alter route rules as well.
If you want to change language simply add lang variable with language code to url.
EXAMPLE
<?php echo CHtml::link('this link will open current page in german',array('','lang'=>'de')) ?>
For more information about translating core messages, views and other files check Yii documentation
awesome
thx for sharing i needed to change the route though to make it work with parameters ;)
'<lang:(th|en|de)>/<_c>/<_a>/*' => '<_c>/<_a>/',
That was exactly what I needed.
Work perfect
Thanks!
That was exactly what I needed.
If you're working with modules, you may also add this rule:
'<lang:[a-z]{2}_[a-z]{2,}>/<_m>/<_c>/<_a>' => '<_m>/<_c>/<_a>',
same to Yurik
Yii::app()->createUrl('/')
will build:
http://abc.com/lang/zh_cn/
if (!isset($params['lang']))
modify to
if (!isset($params['lang']) && Yii::app()->GetLanguage()!=Yii::app()->components['ELangHandler']->languages[0])
is work! and build:
http://abc.com/
Please ask questions on forums
Please ask questions on this forums thread. This is for reviews only.
Yes its free for commercial use as long as you keep license copy with your software. Please check license tab on top of the screen for details.
is this free for business ?
is this free for business ?
No changes in URLs if site language is default
Forward thanks a lot 4 your extension!
I think, if current site language is default (equals site language in config) there's no need to modify URLs by adding language in URLs.
To use this method I changed a little ELangCUrlManager.php in line 6 from:
if (!isset($params['lang']))
to:
if (!isset($params['lang']) && Yii::app()->GetLanguage()!=Yii::app()->components['ELangHandler']->languages[0])
little bug
is work.
but a little bug the name of the file ELangCUrlManager.php rename in ElangCUrlManager.php
Is possible to use urlformat='get'...
Thanks for downloading
I've been away for a while and I was really surprised when I saw how popular this extension has become. Thank you all for comments and downloads :).
it works!
Awesome extension, works like a charm.
Preload not working
Thanks for the extension..
The preload of the ELangHandler is not working. Only the log preload.
'preload' => array('log', 'ELangHandler'),
Any idea why is being skipped?
I'm using the yii-environment extension, but I don't know if its has something to do because the log components is being preload.
Thank you.
domain.com -> domain.com/lang rule needed
Here is my config/main.php for clean URLs with lang code
'urlManager' => array( 'class' => 'ext.langhandler.ELangCUrlManager', 'urlFormat' => 'path', 'showScriptName' => false, 'caseSensitive' => false, 'rules' => array( '<lang:\w+>' => 'site/index', '<lang:\w+>/<action:\w+>' => 'site/<action>', '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ),
Now I want that if a user calls domain.com the url should be rewritten to domain.com/en
How is that possible? Thanks.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.