Create seo-friendly language urls

This component extends CUrlManager and adds language 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.


Developed with Yii 1.1.12


Extract the ELangUrlManager.php into protected/components

Configure the urlManager in config/main.php

components => array(
             //ELangUrlManager configuration
            '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 

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


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(
                'class' => 'bootstrap.widgets.TbMenu',
                'items' => array(
                    //your menu items   
                    array('label' => 'Home', 'url' => array('site/index')),
            //the language switch

Note: The component works together with the menubuilder, because the menubuilder does a Yii::app()->createUrl on rendering the menu items.


  • v1.1 added method getLanguageMenuItems

Similar extensions

Be the first person to leave a comment

Please to leave your comment.

Create extension