Nowadays, if you want to get in touch with the world, you’ve got to think about the people who don’t speak your native language.

In Yii there is a simple mechanism to integrate translation. While it’s a quite a long topic I will try to make it short and basic.

The setup

It is of good usage to get familiar with the country codes and the country language code. I’m going to use an example without so many explanation about language and country codes (if you want to know more about it: Google it !).

In our example we need a website in spanish, french and english (the american english). It is here important to note that there always is a base language from which all others translate. Our base language is going to be english. After all, when you create your webapp with Yii it’s all in english.

Normally you use a language switcher on your site, but I’m going to skip that for a next post. So, to be able to test our application we are going to need a target language. Let’s say spanish !

You will have to edit the main.php file in your protected/config folder to set the base language (the source) and the target language.

Just a t the beginning of your file, after the name of your app, write this :

'sourceLanguage' => 'en_us',

'Language' => 'es_es',

 

as you noticed, the first two letters are for the language, the 2 others for the country.

 

Once you are done with the main configuration file, create a folder named messages  in your protected folder. Inside this folder create two folders which should finally look like this.

messages folder

messages folder

 

Implementation

OK, so far so good. Now we are going to edit our site homepage. For the sake of the example I’m going to use this code snippet.


 

Notice the first parameter : home. It is important because we are now going to create a home.php file inside the protected/messages/es_es folder. Inside that page we are going to code this :

<?php return array('Hello world !' => 'Hola mundo !');

 

And that’s pretty it. Now if you go on your home page with your browser you can see that “Hello world !” is now “Hola mundo !”.

By this example you can now better understand how translation is managed inside Yii. There will be more about this on coming posts.

Cheers !!