@@ -55,23 +55,29 @@ you can use the following statement to change the target language:
55
55
56
56
## Message Translation <span id =" message-translation " ></span >
57
57
58
- Message translation is used to translate the messages that are output by an application to different languages
59
- so that users from different countries can use the application in their native language.
58
+ Message translation service translates a text message from one language (usually the [[ yii\base\Application::$sourceLanguage|source language]] )
59
+ to another (usually the [[ yii\base\Application::$language|target language]] ). It does the translation by looking
60
+ up the message to be translated in a message source which stores the original messages and the translated messages.
61
+ If the message is found, the corresponding translated message will be returned; otherwise the message will be returned
62
+ untranslated.
60
63
61
- The message translation feature in Yii works simply as finding a
62
- translation of the message from a source language into a target language.
63
- To use the message translation feature you wrap your original message strings with a call to the [[ Yii::t()]] method.
64
- The first parameter of this method takes a category which helps to distinguish the source of messages in different parts
65
- of the application and the second parameter is the message itself.
64
+ To use message translation service, you mainly need to do the following work:
65
+
66
+ * Wrap every text message that needs to be translated in a call to the [[ Yii::t()]] method;
67
+ * Configure one or multiple message sources in which the message translation service can look for translated messages;
68
+ * Let the translators to translate messages and store them in the message source(s).
69
+
70
+ The method [[ Yii::t()]] can be used like the following,
66
71
67
72
``` php
68
73
echo \Yii::t('app', 'This is a string to translate!');
69
74
```
70
75
71
- Yii tries to load an appropriate translation according to the current [[ yii\base\Application::$language|application language]]
72
- from one of the message sources defined in the ` i18n ` [ application component] ( structure-application-components.md ) .
73
- A message source is a set of files or a database that provides translation messages.
74
- The following configuration example defines a messages source that takes the messages from PHP files:
76
+ where the second parameter refers to the text message to be translated, while the first parameter refers to
77
+ the name of the category which is used to categorize the message.
78
+
79
+ The [[ Yii::t()]] method will call the ` i18n ` [ application component] ( structure-application-components.md )
80
+ to perform the actual translation work. The component can be configured in the application configuration as follows,
75
81
76
82
``` php
77
83
'components' => [
@@ -92,22 +98,20 @@ The following configuration example defines a messages source that takes the mes
92
98
],
93
99
```
94
100
95
- In the above ` app* ` is a pattern that specifies which categories are handled by the message source. In this case we're
96
- handling everything that begins with ` app ` . Message files are located in ` @app/messages ` , the ` messages ` directory
97
- in your application directory. The [[ yii\i18n\PhpMessageSource::fileMap|fileMap]] array
98
- defines which file is to be used for which category.
99
- Instead of configuring ` fileMap ` you can rely on the convention which is to use the category name as the file name
100
- (e.g. category ` app/error ` will result in the file name ` app/error.php ` under the [[ yii\i18n\PhpMessageSource::basePath|basePath]] .
101
-
102
- When translating the message for ` \Yii::t('app', 'This is a string to translate!') ` with the application language being ` ru-RU ` , Yii
103
- will first look for a file ` @app/messages/ru-RU/app.php ` to retrieve the list of available translations.
104
- If there is no such file under ` ru-RU ` , it will try ` ru ` as well before failing.
101
+ In the above code, a message source supported by [[ yii\i18n\PhpMessageSource]] is being configured. The pattern
102
+ ` app* ` indicates that all message categories whose names start with ` app ` should be translated using this
103
+ message source. The [[ yii\i18n\PhpMessageSource]] class uses PHP files to store message translations. Each
104
+ PHP file corresponds to the messages of a single category. By default, the file name should be the same as
105
+ the category name. However, you may configure [[ yii\i18n\PhpMessageSource::fileMap|fileMap]] to map a category
106
+ to a PHP file with a different naming approach. In the above example, the category ` app/error ` is mapped to
107
+ the PHP file ` @app/messages/ru-RU/error.php ` (assuming ` ru-RU ` is the target language). Without this configuration,
108
+ the category would be mapped to ` @app/messages/ru-RU/app/error.php ` , instead.
105
109
106
- Beside storing the messages in PHP files (using [[ yii\i18n\PhpMessageSource|PhpMessageSource ]] ), Yii provides two other
107
- classes :
110
+ Beside storing the messages in PHP files, you may also use the following message sources to store translated messages
111
+ in different storage :
108
112
109
- - [[ yii\i18n\GettextMessageSource]] that uses GNU Gettext MO or PO files.
110
- - [[ yii\i18n\DbMessageSource]] that uses a database.
113
+ - [[ yii\i18n\GettextMessageSource]] uses GNU Gettext MO or PO files to maintain translated messages .
114
+ - [[ yii\i18n\DbMessageSource]] uses a database table to store translated messages .
111
115
112
116
113
117
### Named placeholders
0 commit comments