This extension allows you to pass the application language via an url and then use it for all further generated urls (within the application itself). It can use the user preffered language as a default initial language.
Resources ¶
Documentation ¶
Requirements ¶
- Yii 1.0 or above
Installation ¶
- Extract the release file under
protected/extensions/urlManager/
Usage ¶
See the following code example:
'components'=>array(
....
'urlManager'=>array(
'class'=>'application.extensions.urlManager.LangUrlManager',
'languages'=>array('ru','uk'),
'langParam'=>'language',
),
...
)
Your can share the change language link follow this example:
<?php echo CHtml::link('<span class="ru">по-русски</span>',array(Yii::app()->defaultController.'/',Yii::app()->urlManager->langParam=>'ru'),array(
'class'=>((Yii::app()->language=='ru') ? 'action':''),
));?>
<?php echo CHtml::link('<span class="ua">украiнською</span>',array(Yii::app()->defaultController.'/',Yii::app()->urlManager->langParam=>'uk'),array(
'class'=>((Yii::app()->language=='uk') ? 'action':''),
));?>
Change Log ¶
December 5, 2009 ¶
- v1.0 final release.
- v1.2 added: support of parsing language url parameters in path format
Fixed
Fixed, please download the above version from download page
Re: It's work but Yii not translate
Had the same problem with parameter
'urlFormat'=>'path',
enabled. First disable this and check if your raw urls are correct.
It's work but Yii not translate
When I test the link is change from 'en' to 'fr' but :
Yii::t('blog','my message');//not translate into french.
Do you have any idea why? Thank you in advance for your response.
Following is the code of my test:
<?php
echo CHtml::link('English', array(Yii::app()->defaultController.'/',Yii::app()->urlManager->langParam=>'en')
,array('class'=>((Yii::app()->language=='en') ? 'action':''), ));
echo ' . ';
echo CHtml::link('French', array(Yii::app()->defaultController.'/',Yii::app()->urlManager->langParam=>'fr')
,array('class'=>((Yii::app()->language=='fr') ? 'action':''), ));
?>
Yii::t('blog','my message');
In my message language file in folder
protected/message/fr/blog.php
return array (
0 => '',
'My message' => 'Mon message',
);
When I click on French, output display as follows:
lang = "fr" is selected
My message
'My message' is not translated to 'Mon message'
mn...
defined:
languages=>array('en_us'=>'English', 'zh_cn'=>'中文')
and, you can list all languages in header on your site, look like:
``
foreach(Yii::app()->urlManager->languages as $lang=>$title) { echo '<li>' . CHtml::link($title, array('', 'lang'=>$lang)) . '</li>'; }
``
language parameter
And for what purpose you would like so I added it to the code? I personally have not discerned in this practical use. Although, maybe I'm wrong
languages parameter
i think languages parameter use following will beter:
'urlManager' => array(
'class'=>'application.extensions.urlManager.LangUrlManager', 'languages' => array( 'en_us' => 'English', // first item is primary language(source language) 'zh_cn' => '中文', ),
),
new feature
I think that will add several new features:
1) setting the default language of the preferred language transmitted by browser
2) improve the parsing of language
new feature
I think that will add several new features:
1) setting the default language of the preferred language transmitted by browser
2) improve the parsing of language
good job!
thanks for you extension share~
this extension is better than "langhandler"
http://www.yiiframework.com/extension/langhandler/
and, i share my default routes:
'<lang:(zh_cn|en_us)>/<_m:(srbac)>/<_c>/<_a>' => '<_m>/<_c>/<_a>',
'<lang:(zh_cn|en_us)>/<_c>/<_a>' => '<_c>/<_a>',
'<lang:(zh_cn|en_us)>/<_c>' => '<_c>',
'<lang:(zh_cn|en_us)>' => '',
Thank you!
@Ekstazi: Thank you for extension!
@jerry2801:
In case of using associative arrays with languages (proposed by jerry2801, think it is much more convenient) I'd add little tweaks to LangUrlManager.php
//if language pass via url use it if(isset($_GET[$this->langParam]) && array_key_exists($_GET[$this->langParam],$this->languages)){ Yii::app()->language=$_GET[$this->langParam]; //else if preffered language is allowed }elseif(array_key_exists($userLang,$this->languages)) { Yii::app()->language=$userLang; //else use the first language from the list }else { reset($this->languages); Yii::app()->language=key($this->languages); }
in such case you wont get annoying php-NOTICE and language will be assigned correctly!
Another tweak, by default it redirects you to index page of site. To change lang "on the fly" I'd use this in main.php template:
foreach (Yii::app()->urlManager->languages as $lang=>$title) { $route_array=array(); if (!empty($_GET['r'])) { $route_array[0]=$_GET['r']; foreach ($_GET as $key=>$value) { if ($key!='r' && $key!=Yii::app()->urlManager->langParam) $route_array[$key]=$value; } $route_array[Yii::app()->urlManager->langParam]=$lang; } else $route_array=Yii::app()->createUrl('site/index',array(Yii::app()->urlManager->langParam=>$lang)); echo '<li>' . CHtml::link($title, $route_array).'</li>'; }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.