|
|
|
@ -45,8 +45,10 @@ If there's no translation for `ru-RU` Yii will try `ru` as well before failing.
|
|
|
|
|
> **Note**: you can further customize details specifying language |
|
|
|
|
> [as documented in ICU project](http://userguide.icu-project.org/locale#TOC-The-Locale-Concept). |
|
|
|
|
|
|
|
|
|
Basic message translation |
|
|
|
|
------------------------- |
|
|
|
|
Message translation |
|
|
|
|
------------------- |
|
|
|
|
|
|
|
|
|
### Basics |
|
|
|
|
|
|
|
|
|
Yii basic message translation in its basic variant works without additional PHP extension. What it does is finding a |
|
|
|
|
translation of the message from source language into target language. Message itself is specified as the second |
|
|
|
@ -97,7 +99,7 @@ language is used.
|
|
|
|
|
|
|
|
|
|
Instead of configuring `fileMap` you can rely on convention which is `messages/BasePath/LanguageID/CategoryName.php`. |
|
|
|
|
|
|
|
|
|
### Named placeholders |
|
|
|
|
#### Named placeholders |
|
|
|
|
|
|
|
|
|
You can add parameters to a translation message that will be substituted with the corresponding value after translation. |
|
|
|
|
The format for this is to use curly brackets around the parameter name as you can see in the following example: |
|
|
|
@ -111,7 +113,7 @@ echo \Yii::t('app', 'Hello, {username}!', [
|
|
|
|
|
|
|
|
|
|
Note that the parameter assignment is without the brackets. |
|
|
|
|
|
|
|
|
|
### Positional placeholders |
|
|
|
|
#### Positional placeholders |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
$sum = 42; |
|
|
|
@ -121,8 +123,8 @@ echo \Yii::t('app', 'Balance: {0}', $sum);
|
|
|
|
|
> **Tip**: Try keep message strings meaningful and avoid using too many positional parameters. Remember that |
|
|
|
|
> translator has source string only so it should be obvious about what will replace each placeholder. |
|
|
|
|
|
|
|
|
|
Advanced placeholder formatting |
|
|
|
|
------------------------------- |
|
|
|
|
### Advanced placeholder formatting |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In order to use advanced features you need to install and enable [intl](http://www.php.net/manual/en/intro.intl.php) PHP |
|
|
|
|
extension. After installing and enabling it you will be able to use extended syntax for placeholders. Either short form |
|
|
|
@ -132,7 +134,7 @@ that allows you to specify formatting style.
|
|
|
|
|
Full reference is [available at ICU website](http://icu-project.org/apiref/icu4c/classMessageFormat.html) but since it's |
|
|
|
|
a bit cryptic we have our own reference below. |
|
|
|
|
|
|
|
|
|
### Numbers |
|
|
|
|
#### Numbers |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
$sum = 42; |
|
|
|
@ -155,7 +157,7 @@ echo \Yii::t('app', 'Balance: {0, number, ,000,000000}', $sum);
|
|
|
|
|
|
|
|
|
|
[Formatting reference](http://icu-project.org/apiref/icu4c/classicu_1_1DecimalFormat.html). |
|
|
|
|
|
|
|
|
|
### Dates |
|
|
|
|
#### Dates |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
echo \Yii::t('app', 'Today is {0, date}', time()); |
|
|
|
@ -175,7 +177,7 @@ echo \Yii::t('app', 'Today is {0, date, YYYY-MM-dd}', time());
|
|
|
|
|
|
|
|
|
|
[Formatting reference](http://icu-project.org/apiref/icu4c/classicu_1_1SimpleDateFormat.html). |
|
|
|
|
|
|
|
|
|
### Time |
|
|
|
|
#### Time |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
echo \Yii::t('app', 'It is {0, time}', time()); |
|
|
|
@ -196,13 +198,13 @@ echo \Yii::t('app', 'It is {0, date, HH:mm}', time());
|
|
|
|
|
[Formatting reference](http://icu-project.org/apiref/icu4c/classicu_1_1SimpleDateFormat.html). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Spellout |
|
|
|
|
#### Spellout |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
echo \Yii::t('app', '{n,number} is spelled as {n, spellout}', ['n' => 42]); |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
### Ordinal |
|
|
|
|
#### Ordinal |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
echo \Yii::t('app', 'You are {n, ordinal} visitor here!', ['n' => 42]); |
|
|
|
@ -210,7 +212,7 @@ echo \Yii::t('app', 'You are {n, ordinal} visitor here!', ['n' => 42]);
|
|
|
|
|
|
|
|
|
|
Will produce "You are 42nd visitor here!". |
|
|
|
|
|
|
|
|
|
### Duration |
|
|
|
|
#### Duration |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```php |
|
|
|
@ -219,7 +221,7 @@ echo \Yii::t('app', 'You are here for {n, duration} already!', ['n' => 47]);
|
|
|
|
|
|
|
|
|
|
Will produce "You are here for 47 sec. already!". |
|
|
|
|
|
|
|
|
|
### Plurals |
|
|
|
|
#### Plurals |
|
|
|
|
|
|
|
|
|
Different languages have different ways to inflect plurals. Some rules are very complex so it's very handy that this |
|
|
|
|
functionality is provided without the need to specify inflection rule. Instead it only requires your input of inflected |
|
|
|
@ -251,7 +253,7 @@ Total {count, number} {count, plural, one{item} other{items}}.
|
|
|
|
|
To learn which inflection forms you should specify for your language you can referrer to |
|
|
|
|
[rules reference at unicode.org](http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html). |
|
|
|
|
|
|
|
|
|
### Selections |
|
|
|
|
#### Selections |
|
|
|
|
|
|
|
|
|
You can select phrases based on keywords. The pattern in this case specifies how to map keywords to phrases and |
|
|
|
|
provides a default phrase. |
|
|
|
@ -268,25 +270,6 @@ Will produce "Snoopy is dog and it loves Yii!".
|
|
|
|
|
In the expression `female` and `male` are possible values. `other` handler values that do not match. Strings inside |
|
|
|
|
brackets are sub-expressions so could be just a string or a string with more placeholders. |
|
|
|
|
|
|
|
|
|
Views |
|
|
|
|
----- |
|
|
|
|
|
|
|
|
|
You can use i18n in your views to provide support for different languages. For example, if you have view `views/site/index.php` and |
|
|
|
|
you want to create special case for russian language, you create `ru-RU` folder under the view path of current controller/widget and |
|
|
|
|
put there file for russian language as follows `views/site/ru-RU/index.php`. |
|
|
|
|
|
|
|
|
|
> **Note**: If language is specified as `en-US` and there are no corresponding views, Yii will try views under `en` |
|
|
|
|
> before using original ones. |
|
|
|
|
|
|
|
|
|
Formatters |
|
|
|
|
---------- |
|
|
|
|
|
|
|
|
|
In order to use formatters you need to install and enable [intl](http://www.php.net/manual/en/intro.intl.php) PHP |
|
|
|
|
extension. |
|
|
|
|
|
|
|
|
|
Examples |
|
|
|
|
-------- |
|
|
|
|
|
|
|
|
|
### Specifying default translation |
|
|
|
|
|
|
|
|
|
You can specify default translation that will be used as a fallback for categories that don't match any other translation. |
|
|
|
@ -410,4 +393,36 @@ Instead of using `fileMap` you can simply use convention of category mapping to
|
|
|
|
|
|
|
|
|
|
> **Note**: For widgets you also can use i18n views, same rules as for controllers are applied to them too. |
|
|
|
|
|
|
|
|
|
TBD: provided classes overview. |
|
|
|
|
Views |
|
|
|
|
----- |
|
|
|
|
|
|
|
|
|
You can use i18n in your views to provide support for different languages. For example, if you have view `views/site/index.php` and |
|
|
|
|
you want to create special case for russian language, you create `ru-RU` folder under the view path of current controller/widget and |
|
|
|
|
put there file for russian language as follows `views/site/ru-RU/index.php`. |
|
|
|
|
|
|
|
|
|
> **Note**: If language is specified as `en-US` and there are no corresponding views, Yii will try views under `en` |
|
|
|
|
> before using original ones. |
|
|
|
|
|
|
|
|
|
i18n formatter |
|
|
|
|
-------------- |
|
|
|
|
|
|
|
|
|
i18n formatter component is the localized version of formatter that supports formatting of date, time and numbers based |
|
|
|
|
on current locale. In order to use it you need to configure formatter application component as follows: |
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
return [ |
|
|
|
|
// ... |
|
|
|
|
'components' => [ |
|
|
|
|
'formatter' => [ |
|
|
|
|
'class' => 'yii\i18n\Formatter', |
|
|
|
|
], |
|
|
|
|
], |
|
|
|
|
]; |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
After cofiguring the component can be accessed as `Yii::$app->formatter`. |
|
|
|
|
|
|
|
|
|
Note that in order to use i18n formatter you need to install and enable |
|
|
|
|
[intl](http://www.php.net/manual/en/intro.intl.php) PHP extension. |
|
|
|
|
|
|
|
|
|
In order to learn about formatter methods refer to its API documentation: [[yii\i18n\Formatter]]. |