This extension will set up your applications target language according to visitors browser setting, accepted languages list and country code passed in url.
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
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 ll_AA codes now remember to set $strict to true or your application may (and probably will) misbehave after updating to langhandler 1.1.
protected/extensions'preload'=>array('log','ELangHandler'),
'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).
'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.
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)
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
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
Total 12 comments
Here is my config/main.php for clean URLs with lang code
Now I want that if a user calls domain.com the url should be rewritten to domain.com/en How is that possible? Thanks.
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.
Awesome extension, works like a charm.
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 :).
thx for sharing i needed to change the route though to make it work with parameters ;)
'<lang:(th|en|de)>/<_c>/<_a>/*' => '<_c>/<_a>/',
Work perfect
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>',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 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 ?
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:
to:
is work.
but a little bug the name of the file ELangCUrlManager.php rename in ElangCUrlManager.php
Is possible to use urlformat='get'...
Leave a comment
Please login to leave your comment.