Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

6.5 KiB

Internationalization

Internationalization (I18N) refers to the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. For Web applications, this is of particular importance because the potential users may be worldwide.

Locale and Language

There are two languages defined in Yii application: \yii\base\Application::$sourceLanguage and \yii\base\Application::$language.

Source language is the language original application messages are written in such as:

echo \Yii::t('app', 'I am a message!');

Tip: Default is English and it's not recommended to change it. The reason is that it's easier to find people translating from English to any language than from non-English to non-English.

Target language is what's currently used. It's defined in application configuration like the following:

// ...
return array(
	'id' => 'applicationID',
	'basePath' => dirname(__DIR__),
	'language' => 'ru_RU' // ← here!

Later you can easily change it in runtime:

\Yii::$app->language = 'zh_CN';

Basic message translation

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 targer language. Message itself is specified as the first \Yii::t method parameter:

echo \Yii::t('app', 'This is a string to translate!');

Yii tries to load approprite translation from one of the message sources defined via i18n component configuration.

TBD: https://github.com/yiisoft/yii2/issues/930

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:

$username = 'Alexander';
echo \Yii::t('app', 'Hello, {username}!', array(
	'username' => $username,
));

Note that the parameter assignment is without the brackets.

Positional placeholders

$sum = 42;
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

In order to use advanced features you need to install and enable intl PHP extension. After installing and enabling it you will be able to use extended syntax for placeholders. Either short form {placeholderName, argumentType} that means default setting or full form {placeholderName, argumentType, argumentStyle} that allows you to specify formatting style.

Full reference is available at ICU website but since it's a bit crypric we have our own reference below.

Numbers

$sum = 42;
echo \Yii::t('app', 'Balance: {0, number}', $sum);

You can specify one of the built-in styles (integer, currency, percent):

$sum = 42;
echo \Yii::t('app', 'Balance: {0, number, currency}', $sum);

Or specify custom pattern:

$sum = 42;
echo \Yii::t('app', 'Balance: {0, number, ,000,000000}', $sum);

Formatting reference.

Dates

echo \Yii::t('app', 'Today is {0, date}', time());

Built in formats (short, medium, long, full):

echo \Yii::t('app', 'Today is {0, date, short}', time());

Custom pattern:

echo \Yii::t('app', 'Today is {0, date, YYYY-MM-dd}', time());

Formatting reference.

Time

echo \Yii::t('app', 'It is {0, time}', time());

Built in formats (short, medium, long, full):

echo \Yii::t('app', 'It is {0, time, short}', time());

Custom pattern:

echo \Yii::t('app', 'It is {0, date, HH:mm}', time());

Formatting reference.

Spellout

echo \Yii::t('app', '{n,number} is spelled as {n, spellout}', array(
	'n' => 42,
));

Ordinal

echo \Yii::t('app', 'You are {n, ordinal} visitor here!', array(
	'n' => 42,
));

Will produce "You are 42nd visitor here!".

Duration

echo \Yii::t('app', 'You are here for {n, duration} already!', array(
	'n' => 42,
));

Will produce "You are here for 47 sec. already!".

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 word in certain situations.

echo \Yii::t('app', 'There {n, plural, =0{are no cats} =1{is one cat} other{are # cats}}!', array(
	'n' => 0,
));

Will give us "There are no cats!".

In the plural rule arguments above =0 means exactly zero, =1 stands for exactly one other is for any other number. # is replaced with the n argument value. It's not that simple for languages other than English. Here's an example for Russian:

Здесь {n, plural, =0{котов нет} =1{есть один кот} one{# кот} few{# кота} many{# котов} other{# кота}}!

In the above it worth mentioning that =1 matches exactly n = 1 while one matches 21 or 101.

To learn which inflection forms you should specify for your language you can referer to rules reference at unicode.org.

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.

echo \Yii::t('app', '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!', array(
	'name' => 'Snoopy',
	'gender' => 'dog',
));

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.

Formatters

In order to use formatters you need to install and enable intl PHP extension.