langhandler
This extension will set up your applications target language according to visitors browser setting, accepted languages list and country code passed in url.
Resources
Copyright © 2009 by sidewinder (contact at forums http://www.yiiframework.com) All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of sidewinder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Available Downloads
| File | Summary | Uploaded on | Downloaded |
|---|---|---|---|
| langhandler.zip (0.9 KB) | bugfix #1 | July 17, 2009 | 355 |
July 12, 2009
- Initial release.
July 17, 2009
- Bugfix #1 -small typo fixed Thx to cosimfi74
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 usefull 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 japan 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
