\yii\BaseYii: public static function t($category, $message, $params = [], $language = null) { if (static::$app !== null) { return static::$app->getI18n()->translate($category, $message, $params, $language ?: static::$app->language); } else { $p = []; foreach ((array) $params as $name => $value) { $p['{' . $name . '}'] = $value; } return ($p === []) ? $message : strtr($message, $p); } } yii\i18n\I18N: public function translate($category, $message, $params, $language) { $messageSource = $this->getMessageSource($category); $translation = $messageSource->translate($category, $message, $language); if ($translation === false) { return $this->format($message, $params, $messageSource->sourceLanguage); } else { return $this->format($translation, $params, $language); } }
$username = 'Alexander'; echo Yii::t('app', 'Hello, {username}!', ['username' => $username]); // Hello, Alexander! $price = 100; $count = 2; $subtotal = 200; echo Yii::t('app', 'Price: {0}, Count: {1}, Subtotal: {2}', $price, $count, $subtotal); // Price: 100, Count: {1}, Subtotal: {2}
$price = 100; echo Yii::t('app', 'Price: {0}', $price);
yii\i18n\I18N: public function format($message, $params, $language) { $params = (array) $params; if ($params === []) { return $message; } if (preg_match('~{\s*[\d\w]+\s*,~u', $message)) { $formatter = $this->getMessageFormatter(); $result = $formatter->format($message, $params, $language); if ($result === false) { $errorMessage = $formatter->getErrorMessage(); Yii::warning("Formatting message for language '$language' failed with error: $errorMessage. The message being formatted was: $message.", __METHOD__); return $message; } else { return $result; } } $p = []; foreach ($params as $name => $value) { $p['{' . $name . '}'] = $value; } return strtr($message, $p); }
{PlaceholderName, ParameterType, ParameterStyle} // Yii {argNameOrNumber, argType, argStyle} // ICU
echo Yii::t('app', 'Price: {price, number, currency}', ['price' => 100]); // Price: $100.00
plural select selectordinal choice number date time spellout ordinal duration
$fileCount = 21; echo $fileCount.' '.Yii::t('app', '{fileCount, plural, one{file} other{files}}', ['fileCount' => $fileCount]); // (messages/ru-RU/app.php) '{fileCount, plural, one{file} other{files}}' => '{fileCount, plural, one{} few{} many{} other{}}' // // 1 // 2 // 11 // 21 // 0 // 1.5
$fileCount = 2; echo $fileCount.' '.Yii::t('app', '{fileCount, plural, =2{special case files} one{file} other{files}}', [ 'fileCount' => $fileCount ]); // 2 special case files
$fileCount = 21; echo Yii::t('app', 'Total {fileCount, plural, one{# file} other{# files}}', ['fileCount' => $fileCount]); // 'Total {fileCount, plural, one{# file} other{# files}}' => ' {fileCount, plural, one{# } few{# } many{# } other{# }}' // 21 // 21,2 // 1 000 000
$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 ]); // You and one other person liked this
$name = ''; $gender = 'male'; $city = ''; echo Yii::t('app', '{name} went to {city}', [ 'name' => $name, 'went_gender' => $gender, 'city' => $city, ]); // '{name} went to {city}' => '{name} {went_gender, select, male{} female{} other{}} {city}' // // //
$host = ''; $gender_of_host = 'female'; $num_guests = 3; $message = " {gender_of_host, select, female { {num_guests, plural, =0 {{host} does not celebrate her birthday.} one {{host} invites one guest to her birthday.} other {{host} invites # guests to her birthday.} } } male { {num_guests, plural, =0 {{host} does not celebrate his birthday.} one {{host} invites one guest to his birthday.} other {{host} invites # guests to his birthday.} } } other { {num_guests, plural, =0 {{host} do not celebrate their birthday.} one {{host} invite one guest to their birthday.} other {{host} invite # guests to their birthday.} } } } "; echo Yii::t('app', $message, [ 'host' => $host, 'gender_of_host' => $gender_of_host, 'num_guests' => $num_guests, ]); // " {gender_of_host, select, female { {num_guests, plural, =0 {{host} does not celebrate her birthday.} one {{host} invites one guest to her birthday.} other {{host} invites # guests to her birthday.} } } male { {num_guests, plural, =0 {{host} does not celebrate his birthday.} one {{host} invites one guest to his birthday.} other {{host} invites # guests to his birthday.} } } other { {num_guests, plural, =0 {{host} do not celebrate their birthday.} one {{host} invite one guest to their birthday.} other {{host} invite # guests to their birthday.} } } } " => " {gender_of_host, select, female { {num_guests, plural, =0 {{host} .} one {{host} # .} other {{host} # .} } } male { {num_guests, plural, =0 {{host} .} one {{host} # .} other {{host} # .} } } other { {num_guests, plural, =0 {{host} .} one {{host} # .} other {{host} # .} } } } " // 3 . // 3 . // 3 .
$n = 3; echo Yii::t('app', 'You are {0, selectordinal, one{#st} two{#nd} few{#rd} other{#th}} visitor', [$n]); // You are 3rd visitor // 'You are {0, selectordinal, one{#st} two{#nd} few{#rd} other{#th}} visitor' => ' {0, selectordinal, other{#-}} ', // 3-
$fileCount = 1; echo $fileCount.': '.Yii::t('app', '{fileCount, choice, 0 # no files| 1 # one file| 1 < many files}', [ 'fileCount' => $fileCount, ]); // 0 -inf // [-inf] | [1] | (1, +inf] // [-inf, 1) | [1] | (1, +inf] // 0: no files // 1: one file // 2: many files $day = 1; echo $day.' - '.Yii::t('app', '{day, choice, 0 # unknown|1 # Sun|2 # Mon|3 # Tue|4 # Wed|5 # Thu|6 # Fri|7 # Sat|8 # unknown}', ['day' => $day]); // [-inf, 1) | [1, 2) | [2, 3) | [3, 4) | [4, 5) | [5, 6) | [6, 7) | [7, 8) | [8, +inf] // 1 - Sun // 2 - Mon
$value = 123456.789012; echo Yii::t('app', 'Value: {value, number}', ['value' => $value]); // Value: 123,456.789 echo Yii::t('app', 'Value: {value, number, integer}', ['value' => $value]); // Value: 123,457 $value = 1.23; echo Yii::t('app', 'Value: {value, number, percent}', ['value' => $value]); // Value: 123%
$value = 123456.789; Yii::$app->formatter->decimalSeparator = '.'; Yii::$app->formatter->thousandSeparator = ' '; $formattedValue = Yii::$app->formatter->asDecimal($value); echo Yii::t('app', 'Balance: {value, number} - {formattedValue}', ['value' => $value, 'formattedValue' => $formattedValue]); // Balance: 123,456.789 - 123 456.789
// 0 - , $value = 1234567; echo $value.': '.Yii::t('app', 'Result - {value, number, 000000.0000}', ['value' => $value]); echo '<br>'; // 123: Result - 000123.0000 // 1234567: Result - 1234567.0000 // # - , , 0 $value = 123.456789; echo $value.': '.Yii::t('app', 'Result - {value, number, ######.####}', ['value' => $value]); echo '<br>'; // 123: Result - 123 // 123.456789: Result - 123.4568 // 1-9 - $value = 123.333; echo $value.': '.Yii::t('app', 'Result - {value, number, 0.2}', ['value' => $value]); echo '<br>'; // 123.111: Result - 123.2 // 123.333: Result - 123.4 // @ - $value = 1; echo $value.': '.Yii::t('app', 'Result - {value, number, @@@}', ['value' => $value]); echo '<br>'; // 123.456: Result - 123 // 1.23456: Result - 1.23 // 123456: Result - 123000 // 1: Result - 1.00 // 2 4 $value = 12.3456; echo $value.': '.Yii::t('app', 'Result - {value, number, @@##}', ['value' => $value]); echo '<br>'; // 12: Result - 12 // 12.3: Result - 12.3 // 12.3456: Result - 12.35 // . - ( ) // , - ( ) // , $value = 123456.789; echo $value.': '.Yii::t('app', 'Result - {value, number, #,###.##}', ['value' => $value]); echo '<br>'; // 123456.789: Result - 123,456.79 echo $value.': '.Yii::t('app', 'Result - {value, number, #,####.##}', ['value' => $value]); echo '<br>'; // 123456.789: Result - 12,3456.79 // // echo $value.': '.Yii::t('app', 'Result - {value, number, #}', ['value' => $value]); echo '<br>'; // 123456.789: Result - 123457 // 2 ( , ) $value = 987654321; echo $value.': '.Yii::t('app', 'Result - {value, number, #,##,###}', ['value' => $value]); echo '<br>'; // 987654321: Result - 98,76,54,321 // ; - // $value = -12.34; echo $value.': '.Yii::t('app', 'Result - {value, number, #.##;minus # value}', ['value' => $value]); echo '<br>'; // -12.34: Result - minus 12.34 value // E - $value = 123000000; echo $value.': '.Yii::t('app', 'Result - {value, number, #.##E+00}', ['value' => $value]); echo '<br>'; // 123000000: Result - 1.23E+08 // * - (padding); , * $value = 1234; echo $value.': '.Yii::t('app', 'Result - {value, number, *_######}', ['value' => $value]); echo '<br>'; // 123: Result - ___123 // 1234: Result - __1234 $value = 1234; echo $value.': '.Yii::t('app', 'Result - {value, number, ######*_}', ['value' => $value]); echo '<br>'; // 123: Result - 123___ // 1234: Result - 1234__ // % - ; 100 // ‰ - (U+2030); 1000 $value = 0.123; echo $value.': '.Yii::t('app', 'Result - {value, number, #.#‰}', ['value' => $value]); echo '<br>'; // 0.123: Result - 12.3% // 0.123: Result - 123‰ // ¤ - (U+00A4) // - , - , - // ( , , ) $value = 12.34; echo $value.': '.Yii::t('app', 'Result - {value, number, #.##¤}', ['value' => $value]); echo '<br>'; // 12.34: Result - 12.34$ echo $value.': '.Yii::t('app', 'Result - {value, number, #.##¤¤}', ['value' => $value]); echo '<br>'; // 12.34: Result - 12.34USD
$price = 123456; echo \Yii::t('app', 'Price: {price, number, currency}', ['price' => $price]); // Price: $123,456.00 // 'Price: {price, number, currency}' => ': {price, number, currency}' // : 123 456,00 . $price = Yii::$app->formatter->asCurrency(123456, 'GBP'); echo \Yii::t('app', 'Price: {price}', ['price' => $price]); // Price: 123 456,00 £
$price = 123456; echo Yii::t('app', 'Price: {0, number, currency}', $price, 'ru-RU@currency=GBP'); // (messages/ru-RU@currency=GBP/app.php) 'Price: {0, number, currency}' => ': {0, number, currency}' // : 123 456,00 £
$d = strtotime('2015-04-18 11:30:16'); echo Yii::t('app', 'Date: {d, date, short} | {d, date, medium} | {d, date, long} | {d, date, full}', ['d' => $d]); echo '<br>'; // Date: 4/18/15 | Apr 18, 2015 | April 18, 2015 | Saturday, April 18, 2015 // : 18.04.15 | 18 . 2015 . | 18 2015 . | , 18 2015 . echo Yii::t('app', 'Time: {d, time, short} | {d, time, medium} | {d, time, long} | {d, time, full}', ['d' => $d]); echo '<br>'; // Time: 11:30 AM | 11:30:16 AM | 11:30:16 AM GMT | 11:30:16 AM GMT // : 11:30 | 11:30:16 | 11:30:16 GMT | 11:30:16 GMT // y, M, d, H, m, s - , , , , , echo Yii::t('app', 'Date: {d, date, yyyy-MM-dd HH:mm:ss}', ['d' => $d]); echo '<br>'; // Date: 2015-04-18 11:30:16 // E, e, S, a - , , , AM/PM echo Yii::t('app', "Date: {d, date, d MMMM yyyy; EEEE; e 'day of week'; HH-mm-ss.SSS'ms'; a}", ['d' => $d + 0.100]); echo '<br>'; // Date: 18 April 2015; Saturday; 7 day of week; 11-30-16.100ms; AM // : 18 2015; ; 6 ; 11-30-16.100;
echo Yii::t('app', '{n, number} is spelled as {n, spellout}', ['n' => 42]); // 42 is spelled as forty-two // '{n, number} is spelled as {n, spellout}' => '{n, number} {n, spellout}' // 42
echo Yii::t('app', '{value, spellout}', ['value' => 21]); // twenty-one // '{value, spellout}' => '{value, spellout,%spellout-cardinal-feminine}' //
$formatter = new \NumberFormatter('ru-RU', \NumberFormatter::SPELLOUT); echo $formatter->getTextAttribute(\NumberFormatter::PUBLIC_RULESETS); // %spellout-numbering-year;%spellout-numbering;%spellout-cardinal-masculine;%spellout-cardinal-neuter;%spellout-cardinal-feminine;
echo Yii::$app->formatter->asSpellout(21, [], [\NumberFormatter::DEFAULT_RULESET => "%spellout-cardinal-feminine"]);
echo Yii::t('app', 'You are the {n, ordinal} visitor here!', ['n' => 42]); // You are the 42nd visitor here! // 42. !
echo Yii::t('app', 'You are here for {n, duration} already!', ['n' => 123]); // You are here for 47 sec. already! // You are here for 2:03 already! // 123!
$count = 3; echo Yii::t('app', "Example of string with ''syntax characters'': '{' '}' '{test}' {count, plural, other{''count'' value is # '#{}'}}", ['count' => $count]); // Example of string with 'syntax characters': { } {test} 'count' value is 3 #{} echo Yii::t('app', "Example of string with ''syntax characters'': ''{' '}' '{test}' {count, plural, other{''count'' value is # '#{}'}}", ['count' => $count]); // MessageFormatter::__construct(): msgfmt_create: message formatter creation failed
$count = 3.2; echo Yii::t('app', "Example of string with ''syntax characters'': {count}", ['count' => $count]); // "Example of string with ''syntax characters'': {count}" => " ''syntax characters'': {count}" // ''syntax characters'': 3.2
echo Yii::t('app', '{value} {value, plural, other{test}}', ['value' => 1]); // MessageFormatter::format(): Inconsistent types declared for an argument echo Yii::t('app', '{value, select, other{test}} {value, plural, other{test}}', ['value' => 1]); // MessageFormatter::format(): Inconsistent types declared for an argument echo Yii::t('app', '{value, number} {value, plural, other{test}}', ['value' => 1]); // 1 test
echo Yii::t('app', '{valueA} {valueB, plural, other{test}}', [ 'valueA' => 1, 'valueB' => 2, ]); // MessageFormatter::format(): Inconsistent types declared for an argument echo Yii::t('app', '{valueA} {valueB1, plural, other{test}}', [ 'valueA' => 1, 'valueB1' => 2, ]); // 1 test
public static function numberForWord($number) { $number = $number % 100; return ($number < 20 ? $number : $number % 10); } // $yearNumber = 22; echo Yii::t('app', '{yearNumber}{yearNumberForWord, plural, =1{st} =2{nd} =3{rd} other{th}} year', [ 'yearNumber' => $yearNumber, // 1, 2, 11, 22 'yearNumberForWord' => PluralHelper::numberForWord($yearNumber), // 1, 2, 11, 2 ]); // '{yearNumber}{yearNumberForWord, plural, =1{st} =2{nd} =3{rd} other{th}} year' => '{yearNumber}{yearNumberForWord, plural, =1{} =2{} =3{} =6{} =7{} =8{} other{}} ', // 22nd year // 22
Source: https://habr.com/ru/post/264009/
All Articles