Yii 1.1: language-picker

This extension renders simple language picker

This extension allows to change site language. I was using this extension earlier and I decided to make my own.


  • Bootstrap (works with newest alpha version) - only if you choose buttons style
  • Yii 1.1
  • Enabled cookies


Just add this code to init() method in your main controller class (protected/components/Controller.php)

public function init()

And in the place where you want to render Language Picker

<?php $this->widget('ext.LangPick.ELangPick', array()); ?>

And don't forget to set source language in (protected/config/main.php)

return array(


You have to create translations, which should be placed in messages folder

    de  // folder
        strings.php // translation file
    pl  // folder
        strings.php // translation file

Each translation file should contain translation array

return array (
    'Home' => 'Główna',
    'About' => 'O stronie',

finally, to use translations:

Yii::t('strings', 'Home')

For more info, please read manual.

Additional information

If you want to customize it

<?php $this->widget('ext.LangPick.ELangPick', array(
    'excludeFromList' => array('pl', 'en'), // list of languages to exclude from list
    'pickerType' => 'buttons',              // buttons, links, dropdown
    //'linksSeparator' => '<b> | </b>',   // if picker type is set to 'links'
    'buttonsSize' => 'mini',                // mini, small, large
    'buttonsColor' => 'success',            // primary, info, success, warning, danger, inverse
)); ?>



Try out

Total 20 comments

#14837 report it
lowlliet at 2013/09/13 02:51pm

You need to change renderContent method in ELangPick.php file. I'll try to find some time during the weekend and implement handling different frameworks (YiiBooster, Yiistrap).

#14834 report it
kavitama at 2013/09/13 09:35am
How about YIISTRAP?

I am not so used to YII yet, but how should I change your extension to have it work with YIISTRAP?

I can only use the dropdown (because it is the same both in bootstrap and yiistrap) but links and the buttons not.

Thanks in advance

#14827 report it
lowlliet at 2013/09/12 05:13pm

You're right, thanks.

#14820 report it
kavitama at 2013/09/11 10:49pm
Just a TYPO




#12891 report it
nima_naraghi at 2013/04/18 05:45am

Problem was encoding and extra hidden characters in the header. Your extension works perfect. Thank you

#12878 report it
lowlliet at 2013/04/17 10:34am

I can't tell you much more than error message already does... It would be useful if you could show me your config file and application.log from runtime folder (assuming that logging is enabled).

#12876 report it
nima_naraghi at 2013/04/17 10:01am
Famous error

I got this naughty error : Cannot modify header information - headers already sent by (output started at /home/website_name/public_html/webapp/protected/components/Controller.php:1)

for this line: Yii::app()->request->cookies['language'] = $cookie;

I was doing it in my way but got that error. After found your extension I wished it can solve my problem but same error appeared again. This is work on localhost but not on online host and I can't understand why.

#11573 report it
lowlliet at 2013/01/21 07:20am

I can't check it right now, but this setting shouldn't influence my extension. It won't work if browser has disabled cookies.

#11569 report it
renathy at 2013/01/21 06:10am

This doesn't work if enableCookieValidation = true, is it right?

#10041 report it
lowlliet at 2012/10/01 03:47pm

It's hard for me to investigate without knowing server configuration and other things...

First of all, don't worry about url - LangPick uses POST requests, you can check it using FireBug extension (or any other, depending on your browser) - example.

I'll send you private message on forum containing my whole demo page (which works both on localhost and remote host with same config file).

EDIT: You can also try to add

// application components

to your config file...

#10033 report it
guim at 2012/10/01 10:09am
CSRF disabled: URL does nothing

Hi Lowlliet,

I have commented the following line in /protected/config/main.php


Now there is no longer error message displayed. But nothing happend when I click on both the langage links FR|EN; nothing happend. There is no view change. The URL in the address bar stays the same:


It is the same URL than when I click on the link labelled "Curriculum vitae".

#10029 report it
lowlliet at 2012/10/01 04:06am

The url is correct (Language Picker uses POST requests).

Could you try disabling CSRF token validation: Link and let me know if this works for you.

#10023 report it
guim at 2012/09/30 08:05pm
CSRF error message

Hi lowlliet,

I have updated the code as you said.

Now I have an array of 2 languages on the web page as I had wanted.

I have this into /protected/views/layouts/main.php

<?php $this->widget('zii.widgets.CMenu',array(
                    array('label'=>'Curriculum vitae', 'url'=>array('/mespagesweb/cv', 'view'=>'cv')),
                    array($this->widget('ext.LangPick.ELangPick', array('excludeFromList' => array('hu', 'lv','zh_tw', 'ja', 'bg', 'nl', 'no', 'sv', 'sr_yu', 'pl', 'bs', 'he', 'de', 'zh_cn', 'ko_kr', 'ar', 'id', 'th', 'pt_br', 'lt', 'pt', 'fa_ir', 'ru', 'uk', 'sk', 'tr', 'it', 'kk', 'cs', 'ro', 'ta_in', 'sr_sr', 'es', 'vi', 'el')))),
            )); ?>

But when I click on both of the language symbol I have on the view, I have the following error message:

Bad Request
The CSRF token could not be verified.
The request could not be understood by the server due to malformed syntax. Please do not repeat the request without modifications.

If you think this is a server error, please contact the webmaster.

When I rollover the mouse on both the language symbol, I can see this url showing into the task bar:

#10020 report it
lowlliet at 2012/09/30 04:08pm

Hi, this extension displays all folders within messages folder, but to solve your problem I've added some feature (please download LangPick [updated].zip) and change your code to

<?php $this->widget('ext.LanguagePicker.ELangPick', array(
    'excludeFromList' => array('pl', 'en'),
)); ?>

in excludeFromList put all languages you want to prevent from displaying.

#10018 report it
guim at 2012/09/30 02:02pm
All language selectoor diplay at the same time

Thank you lowlliet. I no longer have error. However, I have exactly the following array displaying on the pages: "HU | FR | LV | ZH_TW | JA | BG | NL | NO | SV | SR_YU | PL | BS | HE | DE | ZH_CN | KO_KR | AR | EN | ID | TH | PT_BR | LT | PT | FA_IR | RU | UK | SK | TR | IT | KK | CS | RO | TA_IN | SR_SR | ES | VI | EL". Yet I only want 2 of them to be displayed. Did I missed something?

This is the code of /protected/views/layout/myPage.php

<?php array($this->widget('ext.LanguagePicker.ELangPick', array())); ?>

This is the code of protected/config:

'sourceLanguage' => 'fr',
    'language' => 'en',
#10008 report it
lowlliet at 2012/09/28 11:02am


to make it work, just change from

$dirs = new DirectoryIterator(Yii::app()->messages->basePath);


$dirs = new DirectoryIterator(Yii::app()->basePath . '/../framework/messages');

or change Yii messages path.

#10007 report it
guim at 2012/09/28 10:40am
Directory not found while using language-picker

Hi lowlliet,

I am getting the following error message «DirectoryIterator::__construct(/var/www/testYii/protected/messages): failed to open dir: No such file or directory» about this line:


43         private static function getLanguages()
44         {
45             $translations = array();
46             $dirs = new DirectoryIterator(Yii::app()->messages->basePath);
47             foreach ($dirs as $dir)
48                 if ($dir->isDir() && !$dir->isDot())
49                     $translations[$dir->getFilename()] = $dir->getFilename(); 
50             return in_array(Yii::app()->sourceLanguage, $translations) ? $translations : array_merge($translations, array(Yii::app()->sourceLanguage => Yii::app()->sourceLanguage));
51         }

On my system, I have this path


rather than


Thank you.

#9783 report it
lowlliet at 2012/09/10 06:19am

If you talk about translation providing method, in Yii you can use few ways to provide translation strings: CPhpMessageSource, CGettextMessageSource, CDbMessageSource. Translations itself you can create on your own or like you said, you can get it from any proper translated site.

#9782 report it
vijaykoogu at 2012/09/10 05:48am
Cant we take language files...

how about using language files, what we get on net rather then creating translation file (strings.php) by us.

#9781 report it
lowlliet at 2012/09/10 05:37am

I have updated usage instruction, please let me know if you still have troubles.

Leave a comment

Please to leave your comment.

Create extension