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
)); ?>
Im getting only one language
Im getting only one language and ie EN, what i have to do to get more language options...
@vijaykoogu
I have updated usage instruction, please let me know if you still have troubles.
Cant we take language files...
how about using language files, what we get on net rather then creating translation file (strings.php) by us.
@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.
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.
@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.
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',
@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.
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
@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.
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".
@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...
enableCookieValidation
This doesn't work if enableCookieValidation = true, is it right?
@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.
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.
@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).
@lowlliet
Problem was encoding and extra hidden characters in the header.
Your extension works perfect. Thank you
Just a TYPO
$this->widget('ext.LanguagePicker.ELangPick'
MUST BE
$this->widget('ext.LangPick.ELangPick'
@kavitama
You're right, thanks.
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
@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).
How about enabling csrf validation ,
Hi ..
am happy to use your extension but i must disable csrf validation
so i tried to edit your ElangPick.php line 116
echo CHtml::link(strtoupper($trans), Yii::app()->homeUrl, array('class'=>(Yii::app()->getLanguage() == $trans ? 'active' : 'inactive'), 'submit'=>'','csrf'=>"YII_CSRF_TOKEN" , 'params'=>array('languagePicker'=>$trans))) . ($trans === end($translations) ? '' : $this->linksSeparator);
and its work
hope its also working for anyone
Cannot find Bootstrap component / Yiistrap 1.3.0
I'm trying to get this to work with yiistrap 1.3.0
It stops at the exception 'Cannot find Bootstrap component'.
Does anyone know how to fix that?
case 'buttons': if (!isset(Yii::app()->components['bootstrap'])) throw new CException('Cannot find Bootstrap component')
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.