Introduction to yii localization with I18n

back end developers

Internationalization (I18N) is the process of designing a software application so that it can be adapted to various languages and regions without doing engineering changes. My objective for writing this article is to help back end developers to make the transitions in much easier & smoother manner.

The need of Internationalization

For the web applications, the potential users may be worldwide. So this is very important for web applications. While building web application developer should think globally from the beginning because the application needs in other languages for the users from various countries. Implementing I18n from the beginning will save a lot of your time as well as headaches later.

Yii framework provides built-in support for I18n so it is relatively easy.

How I18n Works

I18n replaces all references to content displayed to the user with function calls that provide translation when needed.

For Example, here is code of attribute field names in model before I18n

Below is how the same code looks like with I18n:

Yii:t() function checks the currently selected language and displays the appropriately translated string.

In this case, the default language is English which is written into the code, as shown in above example.  Language resource files are lists of arrays of strings whose key is the default language text, e.g. ‘First Name’ or ‘Email’ and each file provides translated text values for their appropriate language.

Below is an example of completed French translation file, and the language code is “fr”. The Yii:t() function uses this file to find the appropriate translation to display

I18n Configuration File

Modify frontend\config\main.php file to generate I18n support. Here we’re using the Yii2 advanced application template for our demonstration application.

In the above code, the source and the target languages are defined.  We also specify a message source supported by yii\i18n\PhpMessageSource. The app* pattern indicates that all messages categories starting with app must be translated using this particular message source. In the above configuration file, all French translations are placed in the “messages/fr-FR /app.php” file.


I hope this article will help back end developers create your own multilingual web application using Yii framework which is easy and faster.

Share this: