Changeset View
Changeset View
Standalone View
Standalone View
src/internationalization/PhutilTranslator.php
| Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | final class PhutilTranslator extends Phobject { | ||||
| * @return PhutilTranslator Provides fluent interface. | * @return PhutilTranslator Provides fluent interface. | ||||
| */ | */ | ||||
| public function setTranslations(array $translations) { | public function setTranslations(array $translations) { | ||||
| $this->translations = $translations; | $this->translations = $translations; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function translate($text /* , ... */) { | public function translate($text /* , ... */) { | ||||
| $translation = idx($this->translations, $text, $text); | if (isset($this->translations[$text])) { | ||||
| $translation = $this->translations[$text]; | |||||
| } else { | |||||
| $translation = $text; | |||||
| } | |||||
| $args = func_get_args(); | $args = func_get_args(); | ||||
| while (is_array($translation)) { | while (is_array($translation)) { | ||||
| $arg = next($args); | $arg = next($args); | ||||
| $translation = $this->chooseVariant($translation, $arg); | $translation = $this->chooseVariant($translation, $arg); | ||||
| if ($translation === null) { | if ($translation === null) { | ||||
| $pos = key($args); | $pos = key($args); | ||||
| if (is_object($arg)) { | if (is_object($arg)) { | ||||
| $kind = get_class($arg); | $kind = get_class($arg); | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | if ($variant instanceof PhutilNumber) { | ||||
| $is_sex = true; | $is_sex = true; | ||||
| $variant = $variant->getSex(); | $variant = $variant->getSex(); | ||||
| } else if (is_int($variant)) { | } else if (is_int($variant)) { | ||||
| $is_sex = false; | $is_sex = false; | ||||
| } else { | } else { | ||||
| return null; | return null; | ||||
| } | } | ||||
| // TODO: Move these into PhutilLocale if benchmarks show we aren't | |||||
| // eating too much of a performance cost. | |||||
| switch ($this->localeCode) { | |||||
| case 'en_US': | |||||
| case 'en_GB': | |||||
| case 'es_ES': | |||||
| case 'en_W*': | |||||
| case 'en_P*': | |||||
| case 'en_R*': | |||||
| case 'en_A*': | |||||
| list($singular, $plural) = $translations; | |||||
| if ($variant == 1) { | |||||
| return $singular; | |||||
| } | |||||
| return $plural; | |||||
| case 'cs_CZ': | |||||
| if ($is_sex) { | if ($is_sex) { | ||||
| list($male, $female) = $translations; | return $this->locale->selectGenderVariant($variant, $translations); | ||||
| if ($variant == PhutilPerson::SEX_FEMALE) { | } else { | ||||
| return $female; | |||||
| } | // NOTE: This is a microoptimization which slightly improves performance | ||||
| return $male; | // for common languages with simple plural rules. Languages do not need | ||||
| } | // to be added here even if they use the simple rules. The benefit of | ||||
| // inclusion here is small, on the order of 5%. | |||||
| static $simple_plural = array( | |||||
| 'en_US' => true, | |||||
| 'en_GB' => true, | |||||
| 'en_ES' => true, | |||||
| 'ko_KR' => true, | |||||
| ); | |||||
| list($singular, $paucal, $plural) = $translations; | if (isset($simple_plural[$this->localeCode])) { | ||||
| if ($variant == 1) { | if ($variant == 1) { | ||||
| return $singular; | return reset($translations); | ||||
| } else { | |||||
| return end($translations); | |||||
| } | } | ||||
| if ($variant >= 2 && $variant <= 4) { | } else { | ||||
| return $paucal; | return $this->locale->selectPluralVariant($variant, $translations); | ||||
| } | } | ||||
| return $plural; | |||||
| case 'ko_KR': | |||||
| list($singular, $plural) = $translations; | |||||
| if ($variant == 1) { | |||||
| return $singular; | |||||
| } | } | ||||
| return $plural; | |||||
| default: | |||||
| throw new Exception(pht("Unknown locale '%s'.", $this->localeCode)); | |||||
| } | |||||
| } | } | ||||
| /** | /** | ||||
| * Translate date formatted by `$date->format()`. | * Translate date formatted by `$date->format()`. | ||||
| * | * | ||||
| * @param string Format accepted by `DateTime::format()`. | * @param string Format accepted by `DateTime::format()`. | ||||
| * @param DateTime | * @param DateTime | ||||
| * @return string Formatted and translated date. | * @return string Formatted and translated date. | ||||
| ▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines | |||||