Yii 1.1: language-picker

This extension renders simple language picker
20 followers

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

Requirements

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

Usage

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

public function init()
{
    Yii::import('ext.LangPick.ELangPick'); 
    ELangPick::setLanguage();
    parent::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(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'Demo',
    ...
    'sourceLanguage'=>'en',
    ...

Translations

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

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

Each translation file should contain translation array

<?php
 
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
)); ?>

Look

Look

Try out

Total 20 comments

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

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
@kavitama

You're right, thanks.

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

$this->widget('ext.LanguagePicker.ELangPick'

MUST BE

$this->widget('ext.LangPick.ELangPick'

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

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
@nima_naraghi

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
@renathy

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
enableCookieValidation

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

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

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
'components'=>array(
    ..
    'coreMessages'=>array('basePath'=>'protected/messages'),
    ...

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

//'enableCsrfValidation'=>true,

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:

http://localhost/mySite/index.php?r=mespagesweb/cv&view=cv

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
@guim

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(
                'items'=>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:

http://localhost/mySite/index.php
#10020 report it
lowlliet at 2012/09/30 04:08pm
@guim

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
@guim

Hi,

to make it work, just change from

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

to

$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:

/var/www/testYii/protected/extensions/LangPick/ELangPick.php(46)

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

/var/www/testYii/framework/messages$

rather than

/var/www/testYii/protected/messages$

Thank you.

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

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
@vijaykoogu

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

Leave a comment

Please to leave your comment.

Create extension