This component extends CUrlManager and adds language features.
Features ¶
Adds the current language as first part of the url
The generated urls will look like http://yourapp.com/**de**/site/index, http://yourapp.com/**en**/site/index depending on the current language
Saves the current language in a cookie
Checks the preferred language
Dropdown, Links and imageLinks for switching the language
There are similar extensions, but this one supports language cookie and UI components for switching the language.
Requirements ¶
Developed with Yii 1.1.12
Installation ¶
Extract the ELangUrlManager.php into protected/components
Configure the urlManager in config/main.php
components => array(
...
'urlManager'=>array(
//ELangUrlManager configuration
'class'=>'ELangUrlManager',
'languages'=>array('de'=>'Deutsch','en'=>'English'), //assoziative array language => label
'cookieDays'=>10, //keep language 10 days
//'languageParam'=>'lang' //=default
//common configuration for the yii urlManager
//don't add 'language' rules here, these rules will be added by the ELangUrlManager
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
...
),
.....
),
...
)
Thats all.
The ELangUrlManager adds the language rules as first items to the rules. See code comment ELangUrlManager::addLanguageUrlRules. No need for adding the language rules manually.
Adds the current language to all urls created with Yii::app()->creatUrl or controller->createUrl
Usage ¶
Switch between languages:
Add the language to the url http://yourapp.com/**de**, http://yourapp.com/**en**
Add the configured languageParam: http://yourapp.com/**?lang=de**, http://yourapp.com/**?lang=de**
Use one of the helpers from the ELangUrlManager
The current active page (controller/action) will be displayed in the selected language on switching.
// a language dropdownlist
echo Yii::app()->urlManager->getLanguageDropDown();
//a list of links
$links = Yii::app()->urlManager->getLanguageImageLinks();
foreach($links as $link)
echo $link .'<br/>';
//a image link list with the array(language=>image,...) as first param
$links = Yii::app()->urlManager->getLanguageImageLinks(array('de'=>'images/flags/de.gif', ....));
foreach($links as $link)
echo $link .'<br/>';
More params/methods: Take a look at the code.
Add a language switch to a bootstrap navbar (requires Yiibooster).
$this->widget('bootstrap.widgets.TbNavbar', array(
'items' => array(
array(
'class' => 'bootstrap.widgets.TbMenu',
'items' => array(
//your menu items
array('label' => 'Home', 'url' => array('site/index')),
...
)
),
//the language switch
array(
'class'=>'bootstrap.widgets.TbMenu',
'htmlOptions'=>array('class'=>'pull-right'),
'items'=>Yii::app()->urlManager->getLanguageMenuItems(),
),
)
));
Note: The component works together with the menubuilder, because the menubuilder does a Yii::app()->createUrl on rendering the menu items.
Changelog ¶
- v1.1 added method getLanguageMenuItems
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.