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:

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:

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:

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:

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):

 

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.

Of course somewhere in view file you can create dropdown menu where user can select desired language but that is easy job.

How to make multi language website using Yii frameworkAmir DuranPHPProgrammingTutorialsUncategorizedmultilanguage,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...