Introduction ¶
This is a simple widget to pick a language from your internalizations available! It automatically searches your /application/messages/ for available translations and put it in the list, and user can pick it. This little CPortel, so it is easy to make it look cooler via css or even little flags for languages ;-)
Requirements ¶
Ability to follow instructions, and have Yii installed ;-)
Installation/Usage ¶
Download the extension, and put it in your extensions directory. I'm sure most of you extend CController...
it uses CPhpMessageSource
so in your main config should be
'messages' => array(
'class' => 'CPhpMessageSource'
),
Actually it is the default, so you can omit it ...
Put inside CController::init() , or in beforeAction method
Yii::import('ext.LanguagePicker.ELanguagePicker');
ELanguagePicker::setLanguage();
In your view, put:
$this->widget('ext.LanguagePicker.ELanguagePicker', array(
));
And you're done...
If you don't have any directories inside messages/ just create some, for example : ru, he, en etc. and you will the the list created
feel free to customize it, improve it, and don't forget to share your improvements ;-)
Additional links ¶
Make sure you check out my theme picker also http://www.yiiframework.com/extension/themepicker
Little lesson in intenalization: ¶
If you new to Yii maybe you don't know how to use internalization: Create 2 directories under APPPATH/messages/ lang1 and lang2
than, create there a file app.php
put inside lang1/app.php the following:
<?php
return array(
'abc' => ''this is first language translation',
);
put inside lang2/app.php the following:
<?php
return array(
'abc' => 'this is second language translation',
);
Now insert in your view
echo Yii::t('app', 'abc');
And try to play with language selector, and you will understand in seconds how it works
even more info ¶
you can add for example in main.php
'sourceLanguage' => 'en',
'language' => 'ru',
language will be the default translation, and you must know that translation happens only if sourceLanguage differs from language
Awesome.. Flags instead of Dropdown.
Its Wonder full, and easy to understand... I like it.
Can you please explain, how to use, Flags, instead of Dropdown language select?
Re: roopz
If you ask for simple solution just replace text with images:
<option style="background-image: url('./images/language/ru.gif');">ru</option> <option style="background-image: url('./images/language/en.gif');">en</option>
etc.
TO do so, the simplest way is to customize the dropDownOptions array,
this is a good start:
$this->widget('ext.LanguagePicker.ELanguagePicker', array( 'dropDownOptions' => array('style'=>''), ));
Also some more advanced javascript / jQuery will make it look even cooler
If you want to see an implementation of jQuery solution with dialog box, check out this project
http://www.chive-project.com/
It has very cool language picker with dialog box...
Different option value/name?
Would it be possible to have different values and names for the options? Ie.
<option value="en">English</option>
I could use 'English' as the folder name for English translations, however, accented folders are not displayed in the select so this isn't an option in every case.
LE: I did this by having a language table with language codes (folder names) and language names :)
yes! :D
Im interested in changing drowdown menu for flags too
Change Dropdown List Items
Hi, tks for the extension. Just wanna share how I apply this extension. I also use MultilingualBehavior which required to add params in config/main.php and I want so that there is conformity between these two extension and there is no multiple locale values. So this is how I apply this :
i. Set the params in config/main.php (params' key should be the same with the directory names):
'translatedLanguages' => array('en_us'=> 'English', 'in' => 'Indonesia','zh' => 'Chinese')
ii. In ELanguagePicker :
/** * @var string to decide the dropdown source */ public $params = ''; /* ... Line 47, change the dropdownlist source ... */ $this->params == '' ? $translations = self::getLanguagesList() : $translations = Yii::app()->params[$this->params]; /* ... */
iii. This is how I call the extension (add "params" to tell the name of your params. If you dont tell the params name, the it will get the list from the directories' name):
$this->widget('ext.LanguagePicker.ELanguagePicker', array('params'=>'translatedLanguages'));
Adding code for applied in html link tag
you may add this code in function setLanguage,
else if((Yii::app()->request->getParam('languageSelector') !== null && in_array($_GET['languageSelector'], self::getLanguagesList(), true))){ Yii::app()->setLanguage($_GET['languageSelector']); $cookie = new CHttpCookie('language', $_GET['languageSelector']); $cookie->expire = time() + 60*60*24*$cookieDays; Yii::app()->request->cookies['language'] = $cookie; }
so this extension can be applied to a html link tag (with GET parameter) like this
Your code here... <a <?php echo $id;?> href="<?php $url['languageSelector'] = 'id'; echo $this->createAbsoluteUrl($main_url, $url);?>" title="Indonesian Language"><img src="<?php echo Yii::app()->request->baseUrl; ?>/img/indonesia.gif" alt="Indonesia" /></a> <a <?php echo $ja;?> href="<?php $url['languageSelector'] = 'ja'; echo $this->createAbsoluteUrl($main_url, $url);?>" title="Japanese Language"><img src="<?php echo Yii::app()->request->baseUrl; ?>/img/japan.gif" alt="Japan" /></a> <a <?php echo $en;?> href="<?php $url['languageSelector'] = 'en'; echo $this->createAbsoluteUrl($main_url, $url);?>" title="English Language"><img src="<?php echo Yii::app()->request->baseUrl; ?>/img/english.gif" alt="English" /></a>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.