Перевести на язык программирования следующие команды начало

2 последователя

Интернационализация (I18N) относится к процессу разработки программного приложения таким образом. Чтобы оно могло быть адаптировано к различным языкам и регионам без инженерных изменений. Для веб-приложений это особенно важно. Поскольку потенциальные пользователи могут находиться по всему миру. Yii предлагает полный спектр функций I18N. Которые поддерживают перевод сообщений. Просмотр перевода. Форматирование даты и чисел.

Локаль и язык

Локаль

Locale-это набор параметров, который определяет язык пользователя. Страну и любые специальные предпочтения вариантов. Которые пользователь хочет видеть в своем пользовательском интерфейсе.

Он обычно идентифицируется идентификатором. Состоящим из идентификатора языка и идентификатора региона.

Например, ID en-USобозначает локаль

По соображениям согласованности все идентификаторы локали. Используемые в приложениях Yii . Должны быть канонизированы в форматеll-CC, где llэто двух — или трехбуквенный строчный код языка в соответствии с ISO-639 и CCдвухбуквенный код страны в соответствии с ISO-3166. Более подробную информацию о локали можно найти в документации проекта ICU.

Язык

В Yii мы часто используем термин

Приложение Yii использует два вида языков:

  • исходный язык: Это относится к языку. На котором написаны текстовые сообщения в исходном коде.
  • целевой язык: Это язык, который следует использовать для отображения контента конечным пользователям.

Так называемая служба перевода сообщений в основном переводит текстовое сообщение с исходного языка на целевой.

Конфигурация

Вы можете настроить языки приложений в разделе

return [ 'language' => 'ru-RU', 'sourceLanguage' => 'en-US', ...... ]; 

Значение по умолчанию для исходного языка таковоen-US: Американский английский. Рекомендуется сохранить это значение по умолчанию неизменным.

Обычно гораздо легче найти людей. Которые могут переводить с

Часто вам приходится динамически устанавливать целевой язык, основываясь на различных факторах. Таких как языковые предпочтения конечных пользователей. Вместо того чтобы настраивать его в конфигурации приложения. Вы можете использовать следующий оператор для изменения целевого языка:

 \Yii::$app->language = 'zh-CN'; 

Совет: Если исходный язык различается в разных частях кода. Вы можете переопределить исходный язык для разных источников сообщений. Которые описаны в следующем разделе.

Перевод сообщения

От исходного языка к целевому

Служба перевода сообщений переводит текстовое сообщение с одного языка (обычно исходного) на другой (обычно целевой).

Он выполняет перевод, просматривая сообщение, подлежащее переводу. В источнике сообщений. Который хранит исходные сообщения и переведенные сообщения. Если сообщение найдено. Будет возвращено соответствующее переведенное сообщение; в противном случае исходное сообщение будет возвращено непереведенным.

Как это реализовать

Чтобы воспользоваться услугой перевода сообщений. Вам в основном необходимо выполнить следующую работу:

  1. Оберните каждое текстовое сообщение, которое должно быть переведено. В вызов метода Yii::t ().
  2. Настройте один или несколько источников сообщений. В которых служба перевода сообщений может искать переведенные сообщения.
  3. Пусть переводчики переводят сообщения и хранят их в источнике(источниках) сообщений.

1. Оберните текстовое сообщение

Метод Yii::t() можно использовать следующим образом,

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

где второй параметр относится к текстовому сообщению. Подлежащему переводу. В то время как первый параметр относится к названию категории. Которая используется для категоризации сообщения.

2. Настройте один или несколько источников сообщений.

Метод Yii::t() вызовет метод компонента i18n приложения translate для выполнения фактической работы по переводу. Компонент может быть сконфигурирован в конфигурации приложения следующим образом,

'components' => [ 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', ], ], ], ], ], 

In the above code. A message source supported by yii\i18n\PhpMessageSource is being configured.

Category wildcards with * symbol

The pattern app* indicates that all message categories whose names start with app should be translated using this message source.

3. Let the translators translate messages and store them in the message source(s)

The yii\i18n\PhpMessageSource class uses PHP files with a simple PHP array to store message translations. These files contain a map of the messages in source language to the translation in the target language.

Info: You can automatically generate these PHP files by using the message command, which will be introduced later in this chapter.

Each PHP file corresponds to the messages of a single category. By default, the file name should be the same as the category name. Example for app/messages/nl-NL/main.php:

 return [ 'welcome' => 'welkom' ]; 
File mapping

You may configure fileMap to map a category to a PHP file with a different naming approach.

In the above example, the category app/error is mapped to the PHP file @app/messages/ru-RU/error.php (assuming ru-RU is the target language). However, without this configuration the category would be mapped to @app/messages/ru-RU/app/error.php instead.

Other storage types

Besides storing the messages in PHP files, you may also use the following message sources to store translated messages in different storage:

Message Formatting

When translating a message, you can embed some placeholders and have them replaced by dynamic parameter values. You can even use special placeholder syntax to have the parameter values formatted according to the target language. In this subsection, we will describe different ways of formatting messages.

Message Parameters

In a message to be translated, you can embed one or multiple parameters (also called placeholders) so that they can be replaced by the given values. By giving different sets of values, you can variate the translated message dynamically. In the following example, the placeholder {username} in the message 'Hello, {username}!' will be replaced by 'Alexander' and 'Qiang', respectively.

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

While translating a message containing placeholders, you should leave the placeholders as is. This is because the placeholders will be replaced with the actual values when you call Yii::t() to translate a message.

You can use either named placeholders or positional placeholders, but not both, in a single message.

The previous example shows how you can use named placeholders. That is, each placeholder is written in the format of {name}, and you provide an associative array whose keys are the placeholder names (without the curly brackets) and whose values are the corresponding values placeholder to be replaced with.

Positional placeholders use zero-based integer sequence as names which are replaced by the provided values according to their positions in the call of Yii::t(). In the following example, the positional placeholders {0}, {1} and {2} will be replaced by the values of $price, $count and $subtotal, respectively.

$price = 100; $count = 2; $subtotal = 200; echo \Yii::t('app', 'Price: {0}, Count: {1}, Subtotal: {2}', [$price, $count, $subtotal]); 

In case of a single positional parameter its value could be specified without wrapping it into array:

echo \Yii::t('app', 'Price: {0}', $price); 

Tip: In most cases you should use named placeholders. This is because the names will make the translators understand better the whole messages being translated.

Parameter Formatting

You can specify additional formatting rules in the placeholders of a message so that the parameter values can be formatted properly before they replace the placeholders. In the following example, the price parameter value will be treated as a number and formatted as a currency value:

$price = 100; echo \Yii::t('app', 'Price: {0,number,currency}', $price); 

Note: Parameter formatting requires the installation of the intl PHP extension.

You can use either the short form or the full form to specify a placeholder with formatting:

short form: {name,type} full form: {name,type,style} 

Note: If you need to use special characters such as {, }, ', #, wrap them in ':

echo Yii::t('app', "Example of string with ''-escaped characters'': '{' '}' '{test}' {count,plural,other{''count'' value is # '#{}'}}", ['count' => 3]); 

Complete format is described in the ICU documentation. In the following we will show some common usages.

Number

The parameter value is treated as a number. For example,

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

You can specify an optional parameter style as integer, currency, or percent:

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

You can also specify a custom pattern to format the number. For example,

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

Characters used in the custom format could be found in ICU API reference under «Special Pattern Characters» section.

The value is always formatted according to the locale you are translating to i.e. you cannot change decimal or thousands separators, currency symbol etc. without changing translation locale. If you need to customize these you can use yii\i18n\Formatter::asDecimal() and yii\i18n\Formatter::asCurrency().

Date

The parameter value should be formatted as a date. For example,

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

You can specify an optional parameter style as short, medium, long, or full:

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

You can also specify a custom pattern to format the date value:

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

Formatting reference.

Time

The parameter value should be formatted as a time. For example,

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

You can specify an optional parameter style as short, medium, long, or full:

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

You can also specify a custom pattern to format the time value:

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

Formatting reference.

Spellout

The parameter value should be treated as a number and formatted as a spellout. For example,

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

By default the number is spelled out as cardinal. It could be changed:

 echo \Yii::t('app', 'I am {n,spellout,%spellout-ordinal} agent', ['n' => 47]); 

Note that there should be no space after spellout, and before %.

To get a list of options available for locale you’re using check «Numbering schemas, Spellout» at http://intl.rmcreative.ru/.

Ordinal

The parameter value should be treated as a number and formatted as an ordinal name. For example,

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

Ordinal supports more ways of formatting for languages such as Spanish:

 echo \Yii::t('app', '{n,ordinal,%digits-ordinal-feminine}', ['n' => 471]); 

Note that there should be no space after ordinal, and before %.

To get a list of options available for locale you’re using check «Numbering schemas, Ordinal» at http://intl.rmcreative.ru/.

Duration

The parameter value should be treated as the number of seconds and formatted as a time duration string. For example,

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

Duration supports more ways of formatting:

 echo \Yii::t('app', '{n,duration,%in-numerals}', ['n' => 471227]); 

Note that there should be no space after duration, and before %.

To get a list of options available for locale you’re using check «Numbering schemas, Duration» at http://intl.rmcreative.ru/.

Plural

Different languages have different ways to inflect plurals. Yii provides a convenient way for translating messages in different plural forms that works well even for very complex rules. Instead of dealing with the inflection rules directly, it is sufficient to provide the translation of inflected words in certain situations only. For example,

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

In the plural rule arguments above, = means explicit value. So =0 means exactly zero, =1 means exactly one. other stands for any other value. # is replaced with the value of n formatted according to target language.

Plural forms can be very complicated in some languages. In the following Russian example, =1 matches exactly n = 1 while one matches 21 or 101:

Здесь {n,plural,=0{котов нет} =1{есть один кот} one{ 

These other, few, many and other special argument names vary depending on language. To learn which ones you should specify for a particular locale, please refer to «Plural Rules, Cardinal» at http://intl.rmcreative.ru/. Alternatively you can refer to rules reference at unicode.org.

Note: The above example Russian message is mainly used as a translated message, not an original message, unless you set the source language of your application as ru-RU and translating from Russian.

When a translation is not found for an original message specified in Yii::t() call, the plural rules for the source language will be applied to the original message.

There’s an offset parameter for the cases when the string is like the following:

$likeCount = 2; echo Yii::t('app', 'You {likeCount,plural, offset: 1 =0{did not like this} =1{liked this} one{and one other person liked this} other{and # others liked this} }', [ 'likeCount' => $likeCount ]); 

Ordinal selection

The parameter type of selectordinal is meant to choose a string based on language rules for ordinals for the locale you are translating to:

$n = 3; echo Yii::t('app', 'You are the {n,selectordinal,one{#st} two{#nd} few{#rd} other{#th}} visitor', ['n' => $n]); 'You are the {n,selectordinal,one{#st} two{#nd} few{#rd} other{#th}} visitor' => 'Вы {n,selectordinal,other{#-й}} посетитель', 

The format is very close to what’s used for plurals. To learn which arguments you should specify for a particular locale, please refer to «Plural Rules, Ordinal» at http://intl.rmcreative.ru/. Alternatively you can refer to rules reference at unicode.org.

Selection

You can use the select parameter type to choose a phrase based on the parameter value. For example,

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

In the expression above, both female and male are possible parameter values, while other handles values that do not match either one of them. Following each possible parameter value, you should specify a phrase and enclose it in a pair of curly brackets.

Specifying default message source

You can specify default message source that will be used as a fallback for category that doesn’t match any configured category. You can do that by configuring a wildcard category *. In order to do that, add the following to the application config:

 'i18n' => [ 'translations' => [ '*' => [ 'class' => 'yii\i18n\PhpMessageSource' ], ], ], 

Now you can use categories without configuring each one, which is similar to Yii 1.1 behavior. Messages for the category will be loaded from a file under the default translation basePath that is @app/messages:

echo Yii::t('not_specified_category', 'message from unspecified category'); 

The message will be loaded from @app/messages//not_specified_category.php.

Translating module messages

If you want to translate the messages for a module and avoid using a single translation file for all the messages, you can do it like the following:

 namespace app\modules\users; use Yii; class Module extends \yii\base\Module { public $controllerNamespace = 'app\modules\users\controllers'; public function init() { parent::init(); $this->registerTranslations(); } public function registerTranslations() { Yii::$app->i18n->translations['modules/users/*'] = [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en-US', 'basePath' => '@app/modules/users/messages', 'fileMap' => [ 'modules/users/validation' => 'validation.php', 'modules/users/form' => 'form.php', ... ], ]; } public static function t($category, $message, $params = [], $language = null) { return Yii::t('modules/users/' . $category, $message, $params, $language); } } 

In the example above we are using wildcard for matching and then filtering each category per needed file. Instead of using fileMap, you can simply use the convention of the category mapping to the same named file. Now you can use Module::t('validation', 'your custom validation message') or Module::t('form', 'some form label') directly.

Translating widgets messages

The same rule as applied for Modules above can be applied for widgets too, for example:

 namespace app\widgets\menu; use yii\base\Widget; use Yii; class Menu extends Widget { public function init() { parent::init(); $this->registerTranslations(); } public function registerTranslations() { $i18n = Yii::$app->i18n; $i18n->translations['widgets/menu/*'] = [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en-US', 'basePath' => '@app/widgets/menu/messages', 'fileMap' => [ 'widgets/menu/messages' => 'messages.php', ], ]; } public function run() { echo $this->render('index'); } public static function t($category, $message, $params = [], $language = null) { return Yii::t('widgets/menu/' . $category, $message, $params, $language); } } 

Instead of using fileMap you can simply use the convention of the category mapping to the same named file. Now you can use Menu::t('messages', 'new messages {messages}', ['{messages}' => 10]) directly.

Note: For widgets you also can use i18n views, with the same rules as for controllers being applied to them too.

Translating framework messages

Yii comes with the default translation messages for validation errors and some other strings. These messages are all in the category yii. Sometimes you want to correct the default framework message translation for your application. In order to do so, configure the i18n application component like the following:

'i18n' => [ 'translations' => [ 'yii' => [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en-US', 'basePath' => '@app/messages' ], ], ], 

Now you can place your adjusted translations to @app/messages//yii.php.

Handling missing translations

Even if the translation is missing from the source, Yii will display the requested message content. Such behavior is very convenient in case your raw message is a valid verbose text. However, sometimes it is not enough. You may need to perform some custom processing of the situation, when the requested translation is missing from the source. This can be achieved using the missingTranslation-event of yii\i18n\MessageSource.

For example, you may want to mark all the missing translations with something notable, so that they can be easily found at the page. First you need to setup an event handler. This can be done in the application configuration:

'components' => [ 'i18n' => [ 'translations' => [ 'app*' => [ 'class' => 'yii\i18n\PhpMessageSource', 'fileMap' => [ 'app' => 'app.php', 'app/error' => 'error.php', ], 'on missingTranslation' => ['app\components\TranslationEventHandler', 'handleMissingTranslation'] ], ], ], ], 

Now you need to implement your own event handler:

 namespace app\components; use yii\i18n\MissingTranslationEvent; class TranslationEventHandler { public static function handleMissingTranslation(MissingTranslationEvent $event) { $event->translatedMessage = "@MISSING: {$event->category}.{$event->message} FOR LANGUAGE {$event->language} @"; } } 

If yii\i18n\MissingTranslationEvent::$translatedMessage is set by the event handler it will be displayed as the translation result.

Note: each message source handles its missing translations separately. If you are using several message sources and wish them to treat the missing translations in the same way, you should assign the corresponding event handler to each of them.

Using the message command

Translations can be stored in php files, .po files or in a database. See specific classes for additional options.

First of all you need to create a configuration file. Decide where you want to store it and then issue the command

./yii message/config-template path/to/config.php 

Open the created file and adjust the parameters to fit your needs. Pay special attention to:

  • languages: an array representing what languages your app should be translated to;
  • messagePath: path where to store message files, which should match the i18n‘s basePath parameter stated in config.

You may also use ‘./yii message/config’ command to dynamically generate configuration file with specified options via cli. For example, you can set languages and messagePath parameters like the following:

./yii message/config --languages=de,ja --messagePath=messages path/to/config.php 

To get list of available options execute next command:

./yii help message/config 

Once you’re done with the configuration file you can finally extract your messages with the command:

./yii message path/to/config.php 

Also, you may use options to dynamically change parameters for extraction.

You will then find your files (if you’ve chosen file based translations) in your messagePath directory.

View Translation

Instead of translating individual text messages, sometimes you may want to translate a whole view script. To achieve this goal, simply translate the view and save it under a subdirectory whose name is the same as target language. For example, if you want to translate the view script views/site/index.php and the target language is ru-RU, you may translate the view and save it as the file views/site/ru-RU/index.php. Now whenever you call yii\base\View::renderFile() or any method that invoke this method (e.g. yii\base\Controller::render()) to render the view views/site/index.php, it will end up rendering the translated view views/site/ru-RU/index.php, instead.

Note: If the target language is the same as source language original view will be rendered regardless of presence of translated view.

Formatting Date and Number Values

See the Data Formatting section for details.

Setting Up PHP Environment

Yii uses the PHP intl extension to provide most of its I18N features, such as the date and number formatting of the yii\i18n\Formatter class and the message formatting using yii\i18n\MessageFormatter. Both classes provide a fallback mechanism when the intl extension is not installed. However, the fallback implementation only works well for English target language. So it is highly recommended that you install intl when I18N is needed.

The PHP intl extension is based on the ICU library which provides the knowledge and formatting rules for all different locales. Different versions of ICU may produce different formatting result of date and number values. To ensure your website produces the same results across all environments, it is recommended that you install the same version of the intl extension (and thus the same version of ICU) in all environments.

To find out which version of ICU is used by PHP, you can run the following script, which will give you the PHP and ICU version being used.

 echo "PHP: " . PHP_VERSION . "\n"; echo "ICU: " . INTL_ICU_VERSION . "\n"; echo "ICU Data: " . INTL_ICU_DATA_VERSION . "\n"; 

It is also recommended that you use an ICU version equal or greater than version 49. This will ensure you can use all the features described in this document. For example, an ICU version below 49 does not support using # placeholders in plural rules. Please refer to http://site.icu-project.org/download for a complete list of available ICU versions. Note that the version numbering has changed after the 4.8 release (e.g., ICU 4.8, ICU 49, ICU 50, etc.)

Additionally the information in the time zone database shipped with the ICU library may be outdated. Please refer to the ICU manual for details on updating the time zone database. While for output formatting the ICU timezone database is used, the time zone database used by PHP may be relevant too. You can update it by installing the latest version of the pecl package timezonedb.