翻蚳ããã»ã¹ã¯2ã€ã®éšåã§æ§æãããŠããŸã-ã¡ãã»ãŒãžãœãŒã¹ãã翻蚳ãããæååãåä¿¡ããã¡ãã»ãŒãžããã©ãŒãããããŸãã ã¡ãã»ãŒãžã¯ã©ã¹yii \ i18n \ PhpMessageSource ã yii \ i18n \ DbMessageSource ã yii \ i18n \ GettextMessageSourceã¯ãã¡ãã»ãŒãžãœãŒã¹ãšããŠäœ¿çšã§ããŸãã
æžåŒèšå®ã«ã¯ã yii \ i18n \ Formatter ã¯ã©ã¹ãšyii \ i18n \ MessageFormatterã¯ã©ã¹ã䜿çšãããŸãããããã®ã¯ã©ã¹ã¯ã ICUã©ã€ãã©ãªã䜿çšããintlæ¡åŒµæ©èœã䜿çšããŸã ã ãŸãã CLDRä»æ§ãšAPIãªãã¡ã¬ã³ã¹ããããŸã ã
Yii :: tïŒïŒã¯I18NãåŒã³åºãããã®ã©ãããŒ:: translateïŒïŒ
é衚瀺ã®ããã¹ã
\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); } }
ã¡ãã»ãŒãžå ã®ãã©ã¡ãŒã¿ãŒã¯ãé åãšã³ã³ãã®2ã€ã®æ¹æ³ã§éä¿¡ã§ããŸãã Yiiããã¥ã¡ã³ãã«ã¯æ¬¡ã®äŸãå«ãŸããŠããŸãã
$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}
é¢æ°ã®ãœãŒã¹ã³ãŒãã確èªãããšããã®åœ¢åŒã®2çªç®ã®äŸãæ©èœããªãããšã«æ°ä»ãã®ã¯ç°¡åã§ãã ãã®ãããªåŒã³åºãã¯1ã€ã®ãã©ã¡ãŒã¿ãŒã®ã¿ã«äœ¿çšã§ããŸããããã¯ãI18N :: formatïŒïŒé¢æ°ã§é ååãžã®ãã£ã¹ããçºçããããã§ãã
$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
ICUã®ããã¥ã¡ã³ãã§ã¯ã10çš®é¡ã®åŒæ°ã«ã€ããŠèª¬æããŠããŸãã
plural select selectordinal choice number date time spellout ordinal duration
æåã®4ã€ã®ã¿ã€ãã¯è€éãªåŒæ°ãåç §ããæ®ãã¯åçŽãªåŒæ°ãåç §ããŸãã ããªããã£ãã®å ŽåãargStyleéšåã¯ãªãã·ã§ã³ã§ãã è€åäœãå¿ èŠã§ãããåã¿ã€ãã«ã¯ç¬èªã®åœ¢åŒããããŸãã
è€æ°
ããã¥ã¡ã³ããŒã·ã§ã³ è€æ°ã®ãã©ãŒã ãåŠçããããã«äœ¿çšãããŸãã 6ã€ã®å®çŸ©æžã¿ããŒã¯ãŒãããããŸãïŒ0ã1ã2ãå°æ°ãå€æ°ããã®ä»ã otherã®å€ã¯å¿ é ã§ãããåžžã«ãã¿ãŒã³ã«ååšããå¿ èŠããããŸãã ç°ãªãèšèªã«ã¯ç°ãªãããŒã¯ãŒãã®ã»ããããããŸãã ãã·ã¢èªã®å Žåãããã¯1ã€ãå°æ°ãå€æ°ããã®ä»ã§ãã è±èªã®å Žåããã®ä»ã CLDRä»æ§ã§ã¯ ãã³ã³ãã©ã€ã¢ã³ã¹ã«ãŒã«ã®è©³çŽ°ãªãªã¹ããèŠã€ããããšãã§ããŸãã ã«ãŒãã£ãã«ã«ãŒã«ã¿ã€ãã¯è€æ°åœ¢ã®ããŒã¯ãŒããå®çŸ©ããåºæ°ã¿ã€ãã¯selectordinalãå®çŸ©ããŸãã
$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
å éšã¡ãã»ãŒãžã®ãïŒãæåã¯ãåŒæ°ã®æ°å€ã«çœ®ãæããããŸãã å€ã¯ãã±ãŒã«ãèæ ®ããŠãã©ãŒããããããŠããããšã«æ³šæããŠãã ãã-å°æ°éšã®ã³ã³ããæ°åã®ã¹ããŒã¹ã setlocaleïŒïŒé¢æ°ã«ãã£ãŠèšå®ãããã·ã¹ãã ãã±ãŒã«ã¯ãICUã©ã€ãã©ãªã«åœ±é¿ããŸããã
$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
èšäºãæžããŠãããšãã«ãMeteor MessageFormatã®ããã¥ã¡ã³ããžã®äŸ¿å©ãªãªã³ã¯ãèŠã€ããŸãããããã§ã¯ããªã¢ã«ã¿ã€ã ã§ãã¹ãŠã«è§Šããããšãã§ããŸãã
éžæãã
ããã¥ã¡ã³ããŒã·ã§ã³ äžããããããŒã¯ãŒãã®ãã¬ãŒãºãéžæããããã«äœ¿çšãããŸããéåžžãåèªã®å¥³æ§/ç·æ§åœ¢åŒãéžæããŸãã ããŒã¯ãŒãotherã¯å¿ é ã§ãæ®ãã¯ä»»æã§ãã
$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 .
ã»ã¬ã¯ã¿ãŒ
ãã®ã¿ã€ãã¯è€æ°ã®ããŒã¯ãŒãã«äŒŒãŠããŸãããèš±å¯ãããããŒã¯ãŒãã®ã»ãããç°ãªãç¹ãç°ãªããŸãã åºæ°ãšé¢é£ããåŒã衚瀺ããããã«äœ¿çšãããŸãã ãã·ã¢èªã§ã¯ãotherãšããåèªã®ã¿ããµããŒããããŠããããã2æåã¯æ©èœããŸããã
$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-
äœããã®çç±ã§ãçªå·ä»ãã®ãã©ã¡ãŒã¿ãŒã®ã¿ãæ©èœããååä»ããã©ã¡ãŒã¿ãŒã®å ŽåããICU MessageFormat :: formatïŒïŒãžã®åŒã³åºãã«å€±æããŸããããšããèŠåã衚瀺ãããŸãã
éžæ
ããã¥ã¡ã³ããŒã·ã§ã³ æ°å€ç¯å²ã察å¿ããæååã«å€æããããã«äœ¿çšãããŸãã ãã®ã¿ã€ãã¯å ã è€æ°åœ¢ã察象ãšããŠããŸããã ãã ããå€ãã®èšèªã®ã«ãŒã«ã¯ããã®æ©èœã«ã¯è€éãããŸãã éæšå¥šãšã¿ãªããã代ããã«è€æ°åœ¢ãšéžæãæšå¥šãããŸãã
ã¡ãã»ãŒãžåœ¢åŒã¯ãæ°å€ç¯å²[-âã+â]ãããã€ãã®ç¯å²ã«åå²ããŸãã åç¯å²ã¯æååã«é¢é£ä»ããããŠããŸãã åŒæ°å€ãäžé以äžã®æåã®ç¯å²ãéžæããã察å¿ããæååãè¿ãããŸãã åæç¯å²ã®çªå·ã¯ç¡èŠãããããã¯-âã«å¯Ÿå¿ããŸãã èšå·ã<ãïŒless_thanïŒã¯å¢çãé€å€ããèšå·ãïŒãïŒless_than_or_equalïŒã¯ç¯å²ã«å«ãŸããŸãã æ°åã®ä»£ããã«ãèšå·ãâãïŒU + 221EïŒãæå®ã§ããŸãããïŒãã®ä»£ããã«èšå·ãâ€ãïŒU + 2264ïŒãæå®ã§ããŸãã èšå·ã|ãã®å ã¹ããŒã¹ããªãã¯ãã§ããããã§ãªãå Žåã¯ãåã®ç¯å²ã®ã¡ãã»ãŒãžããã¹ãã«å«ãŸããŸãã
$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
æ°
ããã¥ã¡ã³ããŒã·ã§ã³ æ°å€ã衚瀺ããããã«äœ¿çšãããŸãã ã¹ã¿ã€ã«ã®æŽæ°ãé貚ãããŒã»ã³ããæå®ãããããã¿ãŒã³ãæåã§æå®ã§ããŸãã å€ã¯ããã±ãŒã«ãã©ã¡ãŒã¿ïŒå°æ°ç¹èšå·ãååäœã®åºåãèšå·ãé貚èšå·ãªã©ïŒãèæ ®ããŠãã©ãŒããããããŸããããã©ã«ãã§ã¯ãæ°åã«ã¯å°æ°ç¹ä»¥äž3æ¡ãŸã§ã衚瀺ãããŸãã
$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%
ã¡ãã»ãŒãžè¡ãä»ããŠãããã®ãã©ã¡ãŒã¿ãŒã«åœ±é¿ãäžããããšã¯ã§ããŸããã å¥ã®åœ¢åŒã§è¡šç€ºããå¿ èŠãããå Žåã¯ãç¹æ®ãªé¢æ°yii \ i18n \ Formatter :: asDecimalïŒïŒã䜿çšããããšããå§ãããŸãã
$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
åŒã³åºãã®çµæïŒæ°å€ãé貚ïŒããã±ãŒã«ã«ãã£ãŠç°ãªããŸããç¹ã«ãé貚ã®æå®ã¯ãã±ãŒã«ããååŸãããŸãã ãã®ãããé貚ã䜿çšããè¡ã®ç¿»èš³ããŸã ãªãå ŽåãäŸ¡æ Œã¯ãã«ã§è¡šç€ºããã翻蚳ã衚瀺ããããã«ãŒãã«ã§è¡šç€ºãããŸãã ãããã£ãŠãç¹å¥ãªé¢æ°yii \ i18n \ Formatter :: asCurrencyïŒïŒã䜿çšããããšããå§ãããŸãã
$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 £
ãšããã§ããã±ãŒã«ã¯æ¬¡ã®åœ¢åŒã§æå®ã§ããŸãïŒru-RU @ currency = GBPã 詳现ã«ã€ããŠã¯ã ICUãŠãŒã¶ãŒã¬ã€ããã芧ãã ããã
$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 £
æ¥ä»
æé
ããã¥ã¡ã³ããŒã·ã§ã³ æ¥ä»ãšæå»ã衚瀺ããããã«äœ¿çšãããŸãã ã¹ã¿ã€ã«ãçããäžãé·ããå®å šã«æå®ãããããã¿ãŒã³ãæåã§æå®ã§ããŸãã ãã¿ãŒã³ã䜿çšããå Žåãäœã瀺ããã¯é¢ä¿ãããŸãã-æ¥ä»ãŸãã¯æå»ã 圢åŒã¯éåžžã«åçŽã§ããã¹ãŠã®æåã®èª¬æã¯ããã¥ã¡ã³ããŸãã¯Unicodeæšæºã«ãããŸãã ãã£ã©ã¯ã¿ãŒã®ç°ãªãåæ°ã®ç¹°ãè¿ãã¯ç°ãªãæå³ãæã¡ãŸãã ãã©ãŒãããæã«ãçŸåšã®ãã±ãŒã«ã§é±ãå§ãŸãæ¥ïŒæ¥ææ¥ãŸãã¯æææ¥ïŒãèæ ®ãããŸãã
$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
ãã·ã¢èªã®å Žåãããã©ã«ãã¯ç·æ§ã§ãã 女æ§ïŒ21ã³ããã¯ïŒã«è¡šç€ºããã«ã¯ãã«ãŒã«ã»ããã®ååãè¿œå ããå¿ èŠããããŸãã ãïŒ ãèšå·ã®åã«ã¹ããŒã¹ãå ¥ããªãã§ãã ãããããããªããšæ©èœããŸããã
echo Yii::t('app', '{value, spellout}', ['value' => 21]); // twenty-one // '{value, spellout}' => '{value, spellout,%spellout-cardinal-feminine}' //
stackoverflowã§ãã®ã¡ãœãããèŠã€ããŸããã Saxonã·ã¹ãã ã®ããã¥ã¡ã³ããžã®ãªã³ã¯ããããŸã ãããã¯ãå€ããå°ãªããæ確ãªèª¬æãããå¯äžã®å Žæã§ãã
ãã©ãŒããã¿ãŒãç¹å®ã®ãã±ãŒã«ã«ã€ããŠç解ããã«ãŒã«ã¯ã次ã®ããã«ããããŸãã
$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;
å®å šãæãããã«ãé¢æ°yii \ i18n \ Formatter :: asSpelloutïŒïŒãasIntegerïŒïŒãšåãã·ã°ããã£ãæã£ãŠãããšäŸ¿å©ã§ãã çŸåšã§ã¯ã1ã€ã®ãã©ã¡ãŒã¿ãŒãåããŸãã 次ã«ããã®ããã«æžãããšãã§ããŸãïŒ
echo Yii::$app->formatter->asSpellout(21, [], [\NumberFormatter::DEFAULT_RULESET => "%spellout-cardinal-feminine"]);
é åºåãšæéåã3çªç®ã®ãã©ã¡ãŒã¿ãŒããµããŒãããŸãã ããã©ã«ãã®ã«ãŒã«ã»ããã®ååã¯ã ICUãããžã§ã¯ãã®ãœãŒã¹ã«ãããŸã ïŒé¢æ°RuleBasedNumberFormat :: initDefaultRuleSetïŒïŒïŒïŒspellout-'ïŒ spellout-numbering'ãordinal-'ïŒ digits-ordinal'ãduration-'ïŒ duration'ã
åºæ°
åºåã·ãªã¢ã«çªå·ã ãªãã·ã§ã³ãæå®ããæ©èœã®ãªãã¢ããã°ã»ã¬ã¯ã¿ãŒã ãã·ã¢èªã§ã¯ãæ°åã®åŸã«ãããã®ã¿ã衚瀺ãããŸãã
echo Yii::t('app', 'You are the {n, ordinal} visitor here!', ['n' => 42]); // You are the 42nd visitor here! // 42. !
æé
ãã©ã¡ãŒã¿ãŒã¯ç§æ°ãšèŠãªããã 'hïŒmïŒs'ã®åœ¢åŒã§äœãã®æéãšããŠè¡šç€ºãããŸãã ãã·ã¢èªãæ©èœããªããããå€ã¯æŽæ°åœ¢åŒã§ãã®ãŸãŸè¡šç€ºãããŸãã
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!
åäœæ©èœ
intlæ¡åŒµã§ã¯ããšã©ãŒèŠåãåä¿¡ãããã©ãããæå®ã§ããŸãã ãšã©ãŒã¬ãã«ã¯php.iniããã©ã¡ãŒã¿ãŒintl.error_levelã§èšå®ãããŸãã åé€ããŠããèŠåã¯çºçããŸããã ã¡ãã»ãŒãžã¯ãã©ãŒããããããããã®ãŸãŸè¡šç€ºãããŸãã
intlæ¡åŒµæ©èœãã€ã³ã¹ããŒã«ãããŠããªãå Žåããã®åäœã¯ãšãã¥ã¬ãŒããããŸããããšãã¥ã¬ãŒã·ã§ã³ã®å¯èœæ§ã¯éãããŠããŸãã éžæã¿ã€ãã¯å®å šã«ãµããŒããããŠããŸããè€æ°åœ¢ã®å Žåã¯1ã€ããã³ä»ã®åœ¢åŒã®ã¿ããµããŒãããïŒè±èªãªã©ïŒãæ°å€åã®å Žåã¯æŽæ°åã®åŒæ°ã®ã¿ããµããŒããããŸãã ä»ã®ã¿ã€ãã¯ãµããŒããããŠããŸããã
ééã£ã圢åŒãéä¿¡ãããå Žåãæ¡åŒµæ©èœã䜿çšãããšãã«ãèŠåmessageFormatter :: __ã³ã³ã¹ãã©ã¯ãïŒïŒïŒmsgfmt_createïŒmessage formatter creation failedã衚瀺ãããŸãã ãšãã¥ã¬ãŒã·ã§ã³ã¢ãŒãã§ã¯ãšã©ãŒãªãã§æ©èœããã¡ãã»ãŒãžã¯ãã®ãŸãŸè¡šç€ºãããŸãã
ããã¹ãå ã®æ§ææåã¯ãã¢ãã¹ãããã£ã§å²ãå¿ èŠããããŸãã äºéã¢ãã¹ãããã£ã¯åäžã«çœ®ãæããããŸãã åäžã®ã¢ãã¹ãããã£ã¯ãã®ãŸãŸè¡šç€ºãããŸãããã¡ãã»ãŒãžã®è§£æãšã©ãŒãçºçããå¯èœæ§ããããŸãã
$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
å€æ°ãYii :: tïŒïŒã«æž¡ãããªãå ŽåããŸãã¯ãã©ãŒãããèŠåãªãã§äžæ¬åŒ§ã§æž¡ãããå Žåãintlã®åŒã³åºãã¯ãããŸããã ã¡ãã»ãŒãžã¯ãã®ãŸãŸè¡šç€ºãããäºéã¢ãã¹ãããã£ã䜿çšããŠãã·ã¹ãã ãã±ãŒã«ã§å°æ°å€ã衚瀺ãããŸãã I18N :: formatïŒïŒé¢æ°ã«ã¯ãã«ãŒã«ããã§ãã¯ããæ£èŠè¡šçŸããããŸãã
$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
ããããããã¯intlæ¡åŒµã®ãã°ã®ããã«èŠããŸãã å Žåã«ãã£ãŠã¯ãåãã¡ãã»ãŒãžã§åãå€æ°ã2å䜿çšããããšã¯ã§ããŸããããMessageFormatter :: formatïŒïŒïŒInconsistent types defined for argumentããšããèŠåã¡ãã»ãŒãžã衚瀺ãããŸãã ããšãã°ããªãã·ã§ã³ã®1ã€ã§ã¿ã€ããselectããã³è€æ°ãšããŠæå®ãããŠããªãå Žåã¯è¡šç€ºã§ããŸããããnumberããã³è€æ°ãšããŠå°å·ã§ããŸãã ãšãã¥ã¬ãŒã·ã§ã³ã¢ãŒãã§ã¯ããã¹ãŠãæ£åžžã«æ©èœããŸãã
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
ãã®èŠåã¯ã msgformat_helpers.cppãã¡ã€ã«ã®intlãããžã§ã¯ãã®ãœãŒã¹ã«ãããŸãã ããå°ãé«ãã®ã¯ããåãåŒæ°ã«å¯ŸããŠç°ãªãåãèŠã€ãããŸããïŒããšããã³ã¡ã³ãã§ããããã¯æããã«æåŸã®äŸãšã¯äžèŽããŸããã
å°ããªè¿œå
intlæ¡åŒµæ©èœãã€ã³ã¹ããŒã«ãããŠããªãå ŽåããŸãã¯ç¹å®ã®å Žåã«ãã·ã¢èªã®ãã©ãŒã ãæ£ããåŠçããã«ã¯ãäžéšã®ãã«ããŒã§æ¬¡ã®æ©èœãå®è¡ã§ããŸãã
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
èšäºã¯éåžžã«å€§èŠæš¡ã§ããããšãå€æããŸãããããã®ãããã¯ã«é¢ãããã¹ãŠã®åºæ¬æ å ±ã1ãæã§åéããæ¹ãè¯ããšå€æããŸããã