Yii Framework Forum: Example of changing language - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Example of changing language Rate Topic: -----

#1 User is offline   mauphung 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 12-December 09

Posted 16 December 2009 - 12:26 PM

Is there any tutorial or step by step example to change language in blog demo or any?
Thanks,
MP

I made a very simple test and it's OK. Followings are 4 steps to add the support for french language:

1/ Add following line in protected/views/layout/main.php

$lang = $_GET['lang'];//Yii::app()->language;
echo $lang.'<br/>';
Yii::app()->setLanguage($lang);
echo Yii::t('blog','Active record class "{class}" does not have a scope named "{scope}".');

2/ Create a folder fr or whatever language you like under protected\messages\

3/ Create message file named blog.php in folder protected\messages\fr\ with the content as belows or whatever text that you want to translate.

return array (
'Active record class "{class}" does not have a scope named "{scope}".' => 'La classe Active record « {class} n\'a pas de scope nommé « {scope} ».',
);

4/ Change to french language with uri "index.php?lang=fr"

Please refer to http://www.yiiframew...ide/topics.i18n for more details.
1

#2 User is offline   Tyr84 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 08-January 10

Posted 22 January 2010 - 09:13 AM

Nice work.
But this mean thay you must call Yii::t(...).
I want to set to Slovenian language, so all messagess will be writen in my language(of course if i will translate it). I know that i can do this in confing. But this only work if i put translated messages in
yii/framework/messages. But this is not very useful...
I want to use it like you, but without calling Yii::t(...)

Do you know how to do that?

Tnx

View Postmauphung, on 16 December 2009 - 12:26 PM, said:

Is there any tutorial or step by step example to change language in blog demo or any?
Thanks,
MP

I made a very simple test and it's OK. Followings are 4 steps to add the support for french language:

1/ Add following line in protected/views/layout/main.php

$lang = $_GET['lang'];//Yii::app()->language;
echo $lang.'<br/>';
Yii::app()->setLanguage($lang);
echo Yii::t('blog','Active record class "{class}" does not have a scope named "{scope}".');

2/ Create a folder fr or whatever language you like under protected\messages\

3/ Create message file named blog.php in folder protected\messages\fr\ with the content as belows or whatever text that you want to translate.

return array (
'Active record class "{class}" does not have a scope named "{scope}".' => 'La classe Active record « {class} n\'a pas de scope nommé « {scope} ».',
);

4/ Change to french language with uri "index.php?lang=fr"

Please refer to http://www.yiiframew...ide/topics.i18n for more details.

0

#3 User is offline   teher811 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 25-January 10
  • Location:Glasgow, Scotland, UK

Posted 25 January 2010 - 12:10 PM

Nice. I only have a question: I have looked around without finding an answer: does the message to translate have to be the key? Or can we replace it by an id number or though?

Thanks!
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 25 January 2010 - 01:07 PM

View Postteher811, on 25 January 2010 - 12:10 PM, said:

Nice. I only have a question: I have looked around without finding an answer: does the message to translate have to be the key? Or can we replace it by an id number or though?

Thanks!


If you do:

Yii::t('test', "example message");


"example message" must be in the language defined over Yii::app()->sourceLanguage. If you set sourceLanguage to something else (eg: xx), you can then provide translation for your original sourceLanguage - "xx" does not exist and is used as a dummy.

Yii::app()->sourceLanguage = 'xx';
Yii::app()->language = 'en';
Yii::t('test', "0001");


You must provide this file: "protected/messages/en/test.php":

return array(
   '0001' => 'example message';
);


So you can basically use placeholders as keys and provide translations for all possible languages.
0

#5 User is offline   teher811 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 25-January 10
  • Location:Glasgow, Scotland, UK

Posted 25 January 2010 - 02:42 PM

View PostY!!, on 25 January 2010 - 01:07 PM, said:

If you do:

Yii::t('test', "example message");


"example message" must be in the language defined over Yii::app()->sourceLanguage. If you set sourceLanguage to something else (eg: xx), you can then provide translation for your original sourceLanguage - "xx" does not exist and is used as a dummy.

Yii::app()->sourceLanguage = 'xx';
Yii::app()->language = 'en';
Yii::t('test', "0001");


You must provide this file: "protected/messages/en/test.php":

return array(
   '0001' => 'example message';
);


So you can basically use placeholders as keys and provide translations for all possible languages.

Smooth hack!! This is actually exactly what I needed and it will make things so much simpler, specially if you modify a bit of your text!
Thanks a lot!
0

#6 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 25 January 2010 - 02:56 PM

View Postteher811, on 25 January 2010 - 02:42 PM, said:

Smooth hack!! This is actually exactly what I needed and it will make things so much simpler, specially if you modify a bit of your text!
Thanks a lot!


Yes, but take in mind that you can use console command that helps you with translation. At first I wasn't happy at all with the Yii i18n approach, but now I use it all over. If a message changes (which shouldn't happen often once you translated a production-ready project) or if you add new messages, you can simply run that console command in order to create a merged translation file (which contains all old messages and changes). Then translate that file, delete the old "en.php" and rename "en.php.merged" to "en.php". It's quite simple.

Quote

Tip: The yiic tool in Yii can be used to manage message translations when CPhpMessageSource is used as the message source. Its message command can automatically extract messages to be translated from selected source files and merge them with existing translations if necessary.

0

#7 User is offline   choiceerp 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 24-April 10
  • Location:EUROPA - France - Paris

  Posted 06 May 2010 - 07:22 PM

Quand je lit la méthode proposé pour la traduction je deviens FOUUUUU...
When I read the method to translate all the msg i become CRAZYYYY...

I resume .... duplicate all the file where there are a msg you need to translate and translate the file in a directory ... ALL these think are crazy ... why not translate all YII framework and make distrib for each language in the word ??? NO do like linux or other bestseler of the opensource world .... crazy ... what these bestseler do for the support of 'i18n' ???
The base is same:

Yii::t('programName','key');

a dir with the target language ex 'fr-FR'

But the rest is very different:

in that dir a list of file.txt with the name : programName.txt and inside a list of :
'key1'=>'translation'
'key2'=>'translation'
...

Il est 2h20 du matin et je suis vert de rage de decouvrir seulement maintenant le MEGA problème de traduction qu'il y a dans YII framework (moi qui le croyais ... presque PARFAIS)
It is 2:20 in the morning and I am green with rage just now discovering the MEGA problem of translation there is in YII framework (which I'm almost near to the perfection ...)
knowledge is valid only if it is shared ;-)
0

#8 User is offline   teher811 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 25-January 10
  • Location:Glasgow, Scotland, UK

Posted 07 May 2010 - 03:44 AM

View Postchoiceerp, on 06 May 2010 - 07:22 PM, said:

Quand je lit la méthode proposé pour la traduction je deviens FOUUUUU...
When I read the method to translate all the msg i become CRAZYYYY...

I resume .... duplicate all the file where there are a msg you need to translate and translate the file in a directory ... ALL these think are crazy ... why not translate all YII framework and make distrib for each language in the word ??? NO do like linux or other bestseler of the opensource world .... crazy ... what these bestseler do for the support of 'i18n' ???
The base is same:

Yii::t('programName','key');

a dir with the target language ex 'fr-FR'

But the rest is very different:

in that dir a list of file.txt with the name : programName.txt and inside a list of :
'key1'=>'translation'
'key2'=>'translation'
...

Il est 2h20 du matin et je suis vert de rage de decouvrir seulement maintenant le MEGA problème de traduction qu'il y a dans YII framework (moi qui le croyais ... presque PARFAIS)
It is 2:20 in the morning and I am green with rage just now discovering the MEGA problem of translation there is in YII framework (which I'm almost near to the perfection ...)

I don't understand your point.
You don't need one translation file per language and per file where you invoke Yii::t(...).
Yii::t('fileName','text to translate')
If you want, all messages can be translated in one big file if you use the same 'fileName' in your whole WebApp.

But don't be so mean. I'm not sure BTW I get what is your complaint about. Did you read documentation (guide, API...) about message and file translation?
0

#9 User is offline   choiceerp 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 24-April 10
  • Location:EUROPA - France - Paris

Posted 07 May 2010 - 04:13 AM

Ok yesterday it was late and i became crazy for nothing, I'm wrong, now tell me pleise what you do step by step to translate these messages yii/framework/validators/CRequireValidator.php in you application:

line 51 : $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be {value}.'
line 58 : $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} cannot be blank.');

Regards,
knowledge is valid only if it is shared ;-)
0

#10 User is offline   choiceerp 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 24-April 10
  • Location:EUROPA - France - Paris

Posted 07 May 2010 - 04:39 AM

I know how that work: thank to all:

the (system) translation file are in yii\framework\messages\YOUR_LANG\yii.php

To use it, it very simple (of course when we know every thing is easy), simply put these to line in your app:

in the file : yii/myApp/protected/config/main.php add these to line
'sourceLanguage'=>'en',
'language'=>'fr',

and it's finish ;-) (it's finish for system message but for private message ..., if some body know tel us else, I will write to you, late when I understand myself)

PS: just one thin it was not more simple to add these 4 lines of explanation in the documentation with a simple example and every body understand immediately :-)
knowledge is valid only if it is shared ;-)
0

#11 User is offline   Kheang Hok Chin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 53
  • Joined: 12-August 10
  • Location:Gatineau, Qc (CANADA)

Posted 12 August 2010 - 01:51 PM

Nevermind I got it to work!

View PostY!!, on 25 January 2010 - 01:07 PM, said:

If you do:

Yii::t('test', "example message");


"example message" must be in the language defined over Yii::app()->sourceLanguage. If you set sourceLanguage to something else (eg: xx), you can then provide translation for your original sourceLanguage - "xx" does not exist and is used as a dummy.

Yii::app()->sourceLanguage = 'xx';
Yii::app()->language = 'en';
Yii::t('test', "0001");


You must provide this file: "protected/messages/en/test.php":

return array(
   '0001' => 'example message';
);


So you can basically use placeholders as keys and provide translations for all possible languages.

0

#12 User is offline   jbcrouigneau 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 17-December 09

Posted 25 October 2010 - 10:30 AM

I read a lot of things about language setting, but I can't find a "clean" way to manage it.

On my site, users can store their spoken language in their profile (in database). I get the user language with :
$language=User::model()->notsafe()->findByPk(Yii::app()->user->id)->profile->language;

I can also store it in a cookie at each login using :
Yii::app()->user->setState('language',$language);


Then, the difficulties begin. My understanding is :

If I want to use Yii localization (to format dates for instance), I need to set Yii::app()->language variable on each client request.
Setting the language in "protected/views/layout/main.php" is quite ugly to me. Is there another way to do it (without defining a new master controller or any other heavy solution) ?
Just want to put this on the right place :
Yii::app()->language=Yii::app()->user->getState('language');


Second difficulty, I want to use the CJuiDatePicker and its language property.
But CJuiDatePicker need 'fr' when Yii use 'fr_fr', and worst, it need '' for 'en_us'.
So I can't use DatePicker this way :
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name'=>'Character[birthday]',
    'value'=>$model->birthday,
    'language'=>Yii::app()->language,
    // additional javascript options for the date picker plugin
    'options'=>array(
	'showAnim'=>'fold',
    ),
    'htmlOptions'=>array(
	'style'=>'height:20px;'
    ),
)); ?>


By the way, DatePicker date format do not match any Yii/i18N format (nor syntax, nor result). I can't find a way to fill DatePicker input with a stored date correctly formatted (except if I put a hard-coded format).
I think the CJuiDatePicker need a date format "translator" to be usable.

Any convenient solutions or advices are welcome!
0

#13 User is offline   Giovanni D. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 238
  • Joined: 23-December 08
  • Location:London (UK)

Posted 30 October 2010 - 12:44 PM

Hi jbcrouigneau.

You can see my solution on the yii playground project: here, here and here :blink: ;D

Could you tell me what you think?

..feel free to ask if you have any doubt.

bye,
Giovanni.
- Yii Playground: collaborative demo app with small examples to play with.. join us ;) [Fork on github]

- DeploYii: Yii powered task runner and deployment solution

- Social: LinkedIn · Twitter
0

#14 User is offline   xav 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 50
  • Joined: 09-August 10

Posted 10 January 2011 - 11:23 AM

View PostVirtual DarKness, on 30 October 2010 - 12:44 PM, said:

Hi jbcrouigneau.

You can see my solution on the yii playground


Hello, This playGround is a goldmine !!

However, I've created the locale file which adds the new date formats under i18n/data, but it doesn't seem to merge it with the framework's as i get an error such as 'small' is an undefined index.

Any ideas about it ?

Many thanks,

Regards,

xav
0

#15 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 03 March 2011 - 03:22 AM

Hi all!

I've extended my Yii application with CDbMessageSource:
    // application components
    'components' => array(
        'messages' => array(
            'class' => 'CDbMessageSource', /* 02 marzo aggiunto supporto per le lingue */
        ),


It's all ok, messages are retrieved correctly but ... how can i change language without set any parameter in config files?

return array(
    ...
    'language' => 'en',
    ....


ps. I just want to use db stored messages. No files no other stuff. I mean, ... something like this..:
Yii::app()->language = $_SESSION['lang']; but ... where I have to put this code?
0

#16 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,986
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 03 March 2011 - 04:18 AM

This is a solution:

/protected/views/layouts/main.php now contains:
<?php @session_start(); Yii::app()->setLanguage(isset($_SESSION['lang'])?$_SESSION['lang']:'it'); ?>

at first line.

with "index.php?r=site/changelanguage&lang=it" i can change language.
  public function actionChangelanguage() {
    @session_start();
    $_SESSION['lang'] = (strtolower($_GET['lang']));
    $this->redirect(array('site/index'));
  }

0

#17 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 24 May 2011 - 05:16 PM

this extension I just created might help you

Cheers,
Gustavo
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#18 User is offline   naoric 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 25-September 11

Posted 26 September 2011 - 10:17 AM

View Postchoiceerp, on 07 May 2010 - 04:39 AM, said:

I know how that work: thank to all:

the (system) translation file are in yii\framework\messages\YOUR_LANG\yii.php

To use it, it very simple (of course when we know every thing is easy), simply put these to line in your app:

in the file : yii/myApp/protected/config/main.php add these to line
'sourceLanguage'=>'en',
'language'=>'fr',

and it's finish ;-) (it's finish for system message but for private message ..., if some body know tel us else, I will write to you, late when I understand myself)

PS: just one thin it was not more simple to add these 4 lines of explanation in the documentation with a simple example and every body understand immediately :-)


You are right! I was looking for an example for hours!!
0

#19 User is offline   dckurushin 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 16-March 11
  • Location:Israel

Posted 27 September 2011 - 04:03 AM

Check out language picker extension in my signature
0

#20 User is offline   c@cba 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 02-December 11

Posted 27 December 2011 - 03:03 PM

View Postjbcrouigneau, on 25 October 2010 - 10:30 AM, said:

If I want to use Yii localization (to format dates for instance), I need to set Yii::app()->language variable on each client request.
Setting the language in "protected/views/layout/main.php" is quite ugly to me. Is there another way to do it (without defining a new master controller or any other heavy solution) ?
Just want to put this on the right place :
Yii::app()->language=Yii::app()->user->getState('language');



Here is how I did this...
In short: I attached an event handler to the onBeginRequest behavior and did set the application language within that event handler method. Since the method is called before each request, it is a good place...

Best regards...
1

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users