Chapter 10. Localize the App

This chapter explains how to write a multilingual app. Localization, also known as Internationalization (I18N), takes care that a software application can be adapted and rendered in different languages without changes in the source code. This is particularly important in a web application where users speak different languages.

Yii provides powerful tools to handle this task, choosing from the file or database approach (according to the application's complexity). We will cover the following topics:

  • Setting the default language
  • File-based translations
    • Example – using file-based translation for the entire website
  • Placeholders formatting
  • DB-based translations
    • Example – translating the room's description using DB

Setting the default language

A Yii application uses two kinds of languages: source language and target language.

Source language specifies the language employed to write the source code; the default setting is en-US, and it is advisable not to change this value since English is the most used and known language in software development. On the other hand, there is a target language used to display content to end users, and we are going to work specifically on this aspect.

This language can be set in the configuration file using the language property:

return [
    // set target language to be Italian
    'language' => 'it-IT',
   
      ....
      ....
];

Alternatively, you can use the following code:

// change target language to Italian
Yii::$app->language = 'it-IT';

Now, let's see how to handle app localization in practice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.66.13