|
1 | 1 | Internationalization
|
2 | 2 | ====================
|
3 | 3 |
|
4 |
| -> Note: This section is under development. |
5 |
| -
|
6 | 4 | Internationalization (I18N) refers to the process of designing a software application so that it can be adapted to
|
7 | 5 | various languages and regions without engineering changes. For Web applications, this is of particular importance
|
8 |
| -because the potential users may be worldwide. |
| 6 | +because the potential users may be worldwide. Yii offers a full spectrum of I18N features that support message |
| 7 | +translation, view translation, date and number formatting. |
9 | 8 |
|
10 |
| -Yii offers several tools that help with internationalization of a website such as message translation and |
11 |
| -number- and date-formatting. |
12 | 9 |
|
13 |
| -Locale and Language |
14 |
| -------------------- |
| 10 | +## Locale and Language <span id="locale-language"></span> |
15 | 11 |
|
16 |
| -There are two languages defined in the Yii application: [[yii\base\Application::$sourceLanguage|source language]] and |
17 |
| -[[yii\base\Application::$language|target language]]. |
| 12 | +Locale is a set of parameters that defines the user's language, country and any special variant preferences |
| 13 | +that the user wants to see in their user interface. It is usually identified by an ID consisting of a language |
| 14 | +ID and a region ID. For example, the ID `en-US` stands for the locale of English and United States. |
| 15 | +For consistency, all locale IDs used in Yii applications should be canonicalized to the format of |
| 16 | +`ll-CC`, where `ll` is a two- or three-letter lowercase language code according to |
| 17 | +[ISO-639](http://www.loc.gov/standards/iso639-2/) and `CC` is a two-letter country code according to |
| 18 | +[ISO-3166](http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html). |
| 19 | +More details about locale can be found in check the |
| 20 | +[documentation of the ICU project](http://userguide.icu-project.org/locale#TOC-The-Locale-Concept). |
18 | 21 |
|
19 |
| -The source language is the language in which the original application messages are written directly in the code such as: |
| 22 | +In Yii, we often use the term "language" to refer to a locale. |
20 | 23 |
|
21 |
| -```php |
22 |
| -echo \Yii::t('app', 'I am a message!'); |
23 |
| -``` |
| 24 | +A Yii application uses two kinds of languages: [[yii\base\Application::$sourceLanguage|source language]] and |
| 25 | +[[yii\base\Application::$language|target language]]. The former refers to the language in which the text messages |
| 26 | +in the source code are written, while the latter is the language that should be used to display content to end users. |
| 27 | +The so-called message translation service mainly translates a text message from source language to target language. |
24 | 28 |
|
25 |
| -The target language is the language that should be used to display the current page, i.e. the language that original messages need |
26 |
| -to be translated to. It is defined in the application configuration like the following: |
| 29 | +You can configure application languages in the application configuration like the following: |
27 | 30 |
|
28 | 31 | ```php
|
29 | 32 | return [
|
30 |
| - 'id' => 'applicationID', |
31 |
| - 'basePath' => dirname(__DIR__), |
32 |
| - // ... |
33 |
| - 'language' => 'ru-RU', // <- here! |
34 |
| - // ... |
35 |
| -] |
| 33 | + // set target language to be Russian |
| 34 | + 'language' => 'ru-RU', |
| 35 | + |
| 36 | + // set source language to be English |
| 37 | + 'sourceLanguage' => 'en-US', |
| 38 | + |
| 39 | + ...... |
| 40 | +]; |
36 | 41 | ```
|
37 | 42 |
|
38 |
| -> **Tip**: The default value for the [[yii\base\Application::$sourceLanguage|source language]] is English and it is |
39 |
| -> recommended to keep this value. The reason is that it's easier to find people translating from |
40 |
| -> English to any language than from non-English to non-English. |
| 43 | +The default value for the [[yii\base\Application::$sourceLanguage|source language]] is `en-US`, meaning |
| 44 | +US English. It is recommended that you keep this default value unchanged, because it is usually much easier |
| 45 | +to find people who can translate from English to other languages than from non-English to non-English. |
41 | 46 |
|
42 |
| -You may set the application language at runtime to the language that the user has chosen. |
43 |
| -This has to be done at a point before any output is generated so that it affects all the output correctly. |
44 |
| -Therefor just change the application property to the desired value: |
| 47 | +You often need to set the [[yii\base\Application::$language|target language]] dynamically based on different |
| 48 | +factors, such as the language preference of end users. Instead of configuring it in the application configuration, |
| 49 | +you can use the following statement to change the target language: |
45 | 50 |
|
46 | 51 | ```php
|
| 52 | +// change target language to Chinese |
47 | 53 | \Yii::$app->language = 'zh-CN';
|
48 | 54 | ```
|
49 | 55 |
|
50 |
| -The format for the language/locale is `ll-CC` where `ll` is a two- or three-letter lowercase code for a language according to |
51 |
| -[ISO-639](http://www.loc.gov/standards/iso639-2/) and `CC` is the country code according to |
52 |
| -[ISO-3166](http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html). |
53 |
| - |
54 |
| -> **Note**: For more information on the concept and syntax of locales, check the |
55 |
| -> [documentation of the ICU project](http://userguide.icu-project.org/locale#TOC-The-Locale-Concept). |
56 |
| -
|
57 |
| -Message translation |
58 |
| -------------------- |
| 56 | +## Message Translation <span id="message-translation"></span> |
59 | 57 |
|
60 | 58 | Message translation is used to translate the messages that are output by an application to different languages
|
61 | 59 | so that users from different countries can use the application in their native language.
|
|
0 commit comments