How to make multi language website using Yii framework
You decided to make an incredible web application, and you want to offer multiple languges for your customers and visitors. That is quite standard today, but the question is how to do that? If you are using Yii framework then you are on the right place, and continue reading 🙂
Yii framework offers you a lot of small tools which enables you to easily develop functionalites on your website, and the same rule applies for website internationalization.
Here is the procedure that you should take in order to fully implement internationalization, and to make it really easy.
Extend default conroller
Always, but always extend Yii’s default Controller.This will give you ability to add general rules for every other Controller that will extend your controller.
For example, I always make my ApplicationController
which extends Yii’s default controller like this:
1 2 3 4 5 6 7 |
class ApplicationController extends Controller{ public function init(){ //Here you can add specific code for generating Menu, but the code to change the Yii's default language Yii::app()->language = 'de'; } } |
All other controllers you generate using Gii tool or you implement manually should be extended from ApplicationController
controller. This enables you to easily configure your application. As you can see in the ApplicationController
, I set default language to DE, and this applies to all other controllers which will extend ApplicationController
.
If you are using Yii modules, than make default controller for every module separately.
Always use Yii’s Yii::t() function
If you want to make your website available in multiple languages, than you have to use Yii’s Yii::t()
function. The function itself enables you internationalization, and that is the reason why you have to master it. Here is great article about Yii::t()
 function and Yii internationalization in general.
Please note that every piece of your website’s content (labels, error messages, notifications, static text, email templates etc…), should be generated using Yii::t()
 function in order tu fully support mulitple languages.
General use of the Yii::t()
function is this:
1 2 3 4 5 |
Yii::t('label','First name') Yii::t('label','Last name') Yii::t('errorMessages','Requested page does not exist') Yii::t('successMessages','Well done!') Yii::t('notifications','You have {numberOfMessages} messages', array('numberOfMessages'=>$messages)) |
The first parameter is category, and in my case I created four categories: label, errorMessages, successMessages, notifications, and you can create them as many as you want. This enables you to categorise your messages, and to easily separte them during translation.
The second parameter is the text itself you want to print/echo. The message may contain placeholder {numberOfMessages}
which will be replaced by the value of $messages
.
Working with modules
If you are working with modules, then you have to use Yii::t() function in a little bit different way. Let’s say there is a administration
module, then you have to use Yii::t() function like this:
1 2 3 4 5 |
Yii::t('administrationModule.label','First name') Yii::t('administrationModule.label','Last name') Yii::t('administrationModule.errorMessages','Requested page does not exist') Yii::t('administrationModule.successMessages','Well done!') Yii::t('administrationModule.notifications','You have {numberOfMessages} messages', array('numberOfMessages'=>$messages)) |
So in your module, Yii::t()
category has to be prefixed with moduleIDModule
text followed by category name.
Make your menu dynamic
Yii has built in support for making dynamic menus, but sometimes I want to create my own menus containing icons, texts, links and other information. I ussually  implement dynamic menus as nested arrays, and here is one examp of it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
array( array( 'link'=>Yii::app()->getBaseUrl(TRUE), 'text'=>Yii::t('menu',"Dashboard"), 'icon'=>"glyphicons glyphicons-home", children=>null ), array( 'link'=>$this->createUrl("user/search"), 'text'=>Yii::t('menu',"Search"), 'icon'=>"glyphicons glyphicons-search", children=>null ), |
As you can see text element of the array is generated using Yii’s Yii::t() function, which will be translated too, so my menu will also be available in multiple languages.
Generate language files
You finished your website, everything is done, and now you want to translate your website. Good, now you should use yiic tool to generate language files where you will enter translations for other languages.
To automatically generete language files, use yiic tool, which is comming with Yii framework and is saved inside framework
folder. To start yiic
tool, start your console (linux) or CMD (windows), and go to:
cd YOUR PATH TO YII FRAMEWORK FOLDER / framework/
Open any file editor (Notepad) and paste the following code (make edits as per your configuration):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php /** * This is the configuration for generating message translations * for the Yii framework. It is used by the 'yiic message' command. */ return array( //Folder that should be translated 'sourcePath'=>'FULL PATH TO YOUR APPLICATION PROTECTED FOLDER', //Example: /srv/www/<wbr />htdocs/public/testapp/protected/ 'messagePath'=>'FULL PATH TO YOUR MESSAGES FOLDER. HERE YOUR LANGUAGE FILES WILL APPEAR', //Example: /srv/www/htdocs/public/testapp/protected/messages 'languages'=>array('de'), 'fileTypes'=>array('php'), 'overwrite'=>true, 'exclude'=>array( '.svn', '.gitignore', 'yiilite.php', 'yiit.php', '/i18n/data', '/messages', '/vendors', '/web/js', ), ); |
Save this file as mytranslationScript.php
in framework
folder.
Go back to command prompt and execute the following line:
php yiic message mytranslationScript.php
The command above will generate language files, where you can translate your website.
Working with modules
If you are working with multiple modules, than probably you want to divide language files across modules. With yiic tool you can do that by specifing ‘sourcePath
‘ as path to your module files, and messagePath
as path to your messages
folder in desired module. For example let’s say there are two modules: frontend
and backend.
Backend module
'sourcePath'=>'/srv/www/htdocs/public/testapp/protected/modules/backend/',
'messagePath'=>'/srv/www/htdocs/public/testapp/protected/modules/backend/messages/',
Frontend module
'sourcePath'=>'/srv/www/htdocs/public/testapp/protected/modules/frontend/',
'messagePath'=>'/srv/www/htdocs/public/testapp/protected/modules/frontend/messages/',
Change language
Now, when you have generated language files, you can controll which language to load by simply changing the value of Yii::app()->language attribute of the Yii application. Here your default controller from the begginging of this article comes into play.
Now you can play with cookies, and save user’s preferred language in cookie, and load it upon page load.
1 2 3 4 5 6 7 8 9 |
class ApplicationController extends Controller{ public function init(){ if(isset(Yii::app()->request->cookies['language']->value))//If there is language defined in cookie, use it Yii::app()->language = Yii::app()->request->cookies['language']->value; else Yii::app()->language = 'en'; } } |
Of course somewhere in view file you can create dropdown menu where user can select desired language but that is easy job.
http://code-epicenter.com/how-to-make-multilanguage-website-using-yii-framework/How to make multi language website using Yii frameworkPHPProgrammingTutorialsUncategorizedmultilanguage,Yii frameworkYou decided to make an incredible web application, and you want to offer multiple languges for your customers and visitors. That is quite standard today, but the question is how to do that? If you are using Yii framework then you are on the right place, and continue reading...Amir DuranAmir Duranamir.duran@gmail.comAdministratorAmir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.Code Epicenter