This extension will set up your applications target language according to visitors browser setting, accepted languages list and country code passed in url.
…and my browser prefered language is ‘fr’. Now if I want to switch to english, I set the lang parameter to ‘en’
/yiiWebApp/index.php?r=site/index&lang=en
…and the page is displayed in english. But as sourceLanguage is ‘en’, the ELangCUrlManager does not add the ‘lang’ parameter to URL it creates (this is what the optimization does). On the next page request, as there is no ‘lang’ parameter, the browser prefered language is choosen ‘fr’ (because in my case, it is supported) : the page is now in french, it switched from ‘en’ for ‘fr’ because there was no ‘lang’ parameter defined.
Maybe I’ve missed something in the configuration … any help is appreciated.
Isn’t it how stock urlManager creates links as well? Url is created for controller “site” and action “page”. Now, you have to pass “view” GET parameter with static page name. When you create an url without any routes it just points to current controller/action but you have to input GET parameter once again. Hence the whole idea of langhandler, it makes sure lang GET parameter is passed between requests.
For this particular example correct function call should look like this:
<?php echo CHtml::link('this link will open current page in german',array('','lang'=>'de','view'=>'about')) ?>
Thanx for the quick reply. I did try out languagepicker before I tried yours, but the design requires a list of links instead of a drop down list. I’m not sure if I can turn it into a list of links.
I’ll check them out and see if I can make it work, thank you.
Setup the route rules to make lang param a prefix and then I did it by force:
<?php
$request = $_SERVER['REQUEST_URI'];
$path_a = explode("/",$request);
$uri = (isSet($_GET["lang"])?
substr($request, strlen($path_a[1])+1) //strip language prefix and the slash
:$request); //don't process if the page is in default language
echo CHtml::link('English', CHtml::normalizeUrl($uri)); //no need to add default language prefix
echo CHtml::link('中文', CHtml::normalizeUrl('/tw'.$uri));
echo CHtml::link('日本語', CHtml::normalizeUrl('/jp'.$uri));
?>
<?php
$params = $_GET; if(isset($params['_lang'])) unset($params['_lang']); // prevents your '_lang' from being overwritten by the parameter in $_GET
echo CHtml::link('German', array_merge(array('', '_lang' => 'de'), $params));
?>
<?php
$request = $_SERVER['REQUEST_URI'];
$path_a = explode("/",$request);
$haveLang = isSet($_GET["lang"]);
$uri = ($haveLang?
substr($request, strlen($path_a[1])+1) //strip language prefix and the slash
:$request); //don't process if the page is in default language
echo CHtml::link(CHtml::encode(Yii::app()->name), CHtml::normalizeUrl(($haveLang?'/'.$_GET["lang"].'/':'/')), array('id'=>'logo'));
?>
<div id="lang_switch">
<?php
echo CHtml::link('English', CHtml::normalizeUrl($uri), array('class'=>'en')); //no need to add default language prefix
echo CHtml::link('中文', CHtml::normalizeUrl('/tw'.$uri), array('class'=>'tw'));
echo CHtml::link('日本語', CHtml::normalizeUrl('/jp'.$uri), array('class'=>'jp'));
?>
I would suggest you to play with the order of the rules. I think the rules are match in sequence, once a rule is matched the rest are skipped. So if you have a link that would fit two rules, then put the rule you would like it to apply to the front of the list.