Yii Localization - Yii

What do you understand by Yii Internationalization?

The process by which an application is designed such that the application can be adapted to different languages is Internationalization (I18N). A full range of I18N features is offered by Yii.

Parameters specifying the user’s language and country is defined as Locale. For instance, en-US stands for the English locale and the United States.

Yii provides two types of languages: source language and target language. The source language is the language in which all text messages in the application are written. The target language is the language that should be used to display content to end users.

What is Yii message translation?

The messages are translated from the source language to the target language by the message translation component. To translate the message, the message translation service must look it up in a message source.

What are the steps to use Yii message translation?

The steps to use the message translation service are:

  • Wrap text messages you want to be translated in the Yii::t() method.
  • Configure message sources.
  • Store messages in the message source.

Step 1 − The Yii::t() method can be used by the following way:

In the above code snippet, the 'app' stands for a message category.

Step 2 − The config/web.php file is modified by:

The source and target languages are defined. A message source is specified which is 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, the file messages/ru-RU/app.php contains all Russian translations.

Step 3 − The messages/ru-RU directory structure is created. A file app.php is created inside the ru-RU folder, which stores all EN → RU translations.

Step 4 − A function actionTranslation() is created in the SiteController.

Step 5 − Enter the URL http://localhost:8080/index.php?r=site/translation in the web browser, and the output appears as:


The message was translated into Russian as the target language is set to ru-RU. The language of the application can be dynamically changed.

Step 6 − The actionTranslation() method is modified by:

Now, the message is displayed in English −

Action Translation Method

Step 7 − In a translated message, one or multiple parameters can be inserted.

Following will be the output.

Translated Message

A whole view script cab be translated, instead of translating individual text messages. For instance, if the target language is ru-RU and want to translate the views/site/index.php view file, you should translate the view and save it under the views/site/ru-RU directory.

Step 8 − The views/site/ru-RU directory structure is created. Then, inside the ru-RU folder a file index.php is created by the code:

Step 9 − The target language is ru-RU, so if you enter the URL http://localhost:8080/index.php?r=site/index, the page appears in Russian translation.

Russian Translation

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics