Yii 1.1: langurlmanager

Multilingual Url Manager
8 followers

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

Total 10 comments

#14008 report it
Alex D. at 2013/07/11 10:15am
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>';
             }
#620 report it
Ekstazi at 2010/04/07 10:34am
Fixed

Fixed, please download the above version from download page

#1066 report it
Tova at 2009/12/20 05:29am
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.

#1067 report it
mauphung at 2009/12/19 10:55am
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'

#1109 report it
jerry2801 at 2009/12/07 08:23am
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>'; }

#1110 report it
Ekstazi at 2009/12/07 06:40am
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

#1112 report it
jerry2801 at 2009/12/06 02:14am
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' => '中文', ), ),

#1114 report it
Ekstazi at 2009/12/05 07:33am
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

#1115 report it
Ekstazi at 2009/12/05 07:30am
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

#1116 report it
jerry2801 at 2009/12/05 07:08am
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)>' => '',

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: Ekstazi
  • Category: Others
  • Votes: +10 / -2
  • Downloaded: 2,085 times
  • Created on: Dec 5, 2009
  • Last updated: Jul 16, 2010