Skip to content

Commit 9969ae0

Browse files
committed
i18n WIP
1 parent 30f0033 commit 9969ae0

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

docs/guide/tutorial-i18n.md

+29-25
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,29 @@ you can use the following statement to change the target language:
5555

5656
## Message Translation <span id="message-translation"></span>
5757

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.
6063

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,
6671

6772
```php
6873
echo \Yii::t('app', 'This is a string to translate!');
6974
```
7075

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,
7581

7682
```php
7783
'components' => [
@@ -92,22 +98,20 @@ The following configuration example defines a messages source that takes the mes
9298
],
9399
```
94100

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.
105109

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:
108112

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.
111115

112116

113117
### Named placeholders

0 commit comments

Comments
 (0)