Changeset View
Changeset View
Standalone View
Standalone View
src/internationalization/PhutilTranslator.php
<?php | <?php | ||||
final class PhutilTranslator extends Phobject { | final class PhutilTranslator extends Phobject { | ||||
private static $instance; | private static $instance; | ||||
private $locale; | private $locale; | ||||
private $localeCode; | private $localeCode; | ||||
private $shouldPostProcess; | private $shouldPostProcess; | ||||
private $willTranslateCallback; | |||||
private $translations = array(); | private $translations = array(); | ||||
public static function getInstance() { | public static function getInstance() { | ||||
if (self::$instance === null) { | if (self::$instance === null) { | ||||
self::$instance = new PhutilTranslator(); | self::$instance = new PhutilTranslator(); | ||||
} | } | ||||
return self::$instance; | return self::$instance; | ||||
} | } | ||||
public static function setInstance(PhutilTranslator $instance) { | public static function setInstance(PhutilTranslator $instance) { | ||||
self::$instance = $instance; | self::$instance = $instance; | ||||
} | } | ||||
public function setLocale(PhutilLocale $locale) { | public function setLocale(PhutilLocale $locale) { | ||||
$this->locale = $locale; | $this->locale = $locale; | ||||
$this->localeCode = $locale->getLocaleCode(); | $this->localeCode = $locale->getLocaleCode(); | ||||
$this->shouldPostProcess = $locale->shouldPostProcessTranslations(); | $this->shouldPostProcess = $locale->shouldPostProcessTranslations(); | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setWillTranslateCallback($callback) { | |||||
$this->willTranslateCallback = $callback; | |||||
return $this; | |||||
} | |||||
public function getWillTranslateCallback() { | |||||
return $this->willTranslateCallback; | |||||
} | |||||
/** | /** | ||||
* Add translations which will be later used by @{method:translate}. | * Add translations which will be later used by @{method:translate}. | ||||
* The parameter is an array of strings (for simple translations) or arrays | * The parameter is an array of strings (for simple translations) or arrays | ||||
* (for translastions with variants). The number of items in the array is | * (for translations with variants). The number of items in the array is | ||||
* language specific. It is `array($singular, $plural)` for English. | * language specific. It is `array($singular, $plural)` for English. | ||||
* | * | ||||
* array( | * array( | ||||
* 'color' => 'colour', | * 'color' => 'colour', | ||||
* '%d beer(s)' => array('%d beer', '%d beers'), | * '%d beer(s)' => array('%d beer', '%d beers'), | ||||
* ); | * ); | ||||
* | * | ||||
* The arrays can be nested for strings with more variant parts: | * The arrays can be nested for strings with more variant parts: | ||||
Show All 16 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 /* , ... */) { | ||||
$args = func_get_args(); | |||||
if ($this->willTranslateCallback) { | |||||
call_user_func_array($this->willTranslateCallback, $args); | |||||
} | |||||
if (isset($this->translations[$text])) { | if (isset($this->translations[$text])) { | ||||
$translation = $this->translations[$text]; | $translation = $this->translations[$text]; | ||||
} else { | } else { | ||||
$translation = $text; | $translation = $text; | ||||
} | } | ||||
$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 174 Lines • Show Last 20 Lines |