/** * {@inheritdoc} * * @uses Titon\Common\Registry * * @throws \Titon\G11n\Exception\MissingPatternException */ public static function get($key, $fallback = null) { $pattern = G11n::registry()->current()->getValidationRules($key) ?: $fallback; if (!$pattern) { throw new MissingPatternException(sprintf('Validation rule %s does not exist', $key)); } return $pattern; }
/** * {@inheritdoc} * * @uses Titon\Common\Registry */ public static function relativeTime($time, array $options = array()) { $g11n = G11n::registry(); $msg = function ($key) use($g11n) { return $g11n->translate('core.format.relativeTime.' . $key); }; // TODO Find a more optimized way to do this. $options = $options + array('seconds' => array($msg('sec'), $msg('second'), $msg('seconds')), 'minutes' => array($msg('min'), $msg('minute'), $msg('minutes')), 'hours' => array($msg('hr'), $msg('hour'), $msg('hours')), 'days' => array($msg('dy'), $msg('day'), $msg('days')), 'weeks' => array($msg('wk'), $msg('week'), $msg('weeks')), 'months' => array($msg('mon'), $msg('month'), $msg('months')), 'years' => array($msg('yr'), $msg('year'), $msg('years')), 'now' => $msg('now'), 'in' => $msg('in'), 'ago' => $msg('ago')); return parent::relativeTime($time, $options); }
/** * {@inheritdoc} * * @uses Titon\Common\Registry * @uses Titon\Utility\String */ public function bindDomains($domain, $catalog) { bind_textdomain_codeset($catalog, Config::encoding()); return $this->cache([__METHOD__, $domain, $catalog], function () use($domain, $catalog) { $locations = G11n::registry()->current()->getMessageBundle()->getPaths(); foreach ($locations as $location) { bindtextdomain($catalog, $location); } return true; }); }
/** * {@inheritdoc} * * @uses Titon\Common\Registry */ public static function percentage($number, $options = []) { if (is_numeric($options)) { $options = ['places' => $options]; } $g11n = G11n::registry(); if ($g11n->isEnabled()) { $options = array_merge($g11n->current()->getFormatPatterns('number'), $options); } return parent::percentage($number, $options); }
/** * Prepend the locale to the front of the route before compilation. * * @return string */ public function compile() { if ($this->isCompiled()) { return $this->_compiled; } $g11n = G11n::registry(); if ($g11n->isEnabled()) { if (mb_substr($this->getPath(), 0, 9) !== '/<locale>') { $this->prepend('/<locale>'); } $this->setConfig('patterns.locale', self::LOCALE); $this->setConfig('locale', G11n::canonicalize($g11n->getFallback()->getCode())); } return parent::compile(); }
/** * {@inheritdoc} * * @uses Titon\G11n\G11n * * @throws \Titon\G11n\Exception\MissingMessageException */ public function getMessage($key) { if ($cache = $this->getCache($key)) { return $cache; } list($domain, $catalog, $id) = $this->parseKey($key); $g11n = G11n::registry(); $locales = $g11n->getLocales(); // Cycle through each locale till a message is found foreach ($g11n->cascade() as $locale) { $cacheKey = sprintf('g11n.%s.%s.%s', $domain, $catalog, $locale); $messages = []; // Check within the cache first if ($storage = $this->getStorage()) { $messages = $storage->get($cacheKey); } // Else check within the bundle if (!$messages) { /** @type \Titon\Io\Bundle $bundle */ $bundle = clone $locales[G11n::canonicalize($locale)]->getMessageBundle(); $bundle->addReader($this->getReader()); if ($data = $bundle->loadResource($domain, $catalog)) { $messages = $data; if ($storage = $this->getStorage()) { $storage->set($cacheKey, $messages); } // If the catalog doesn't exist, try the next locale } else { continue; } } // Return message if it exists, else continue cycle if (isset($messages[$id])) { return $this->setCache($key, $messages[$id]); } } throw new MissingMessageException(sprintf('Message key %s does not exist in %s', $key, implode(', ', array_keys($locales)))); }
/** * Test that decomposing a locale returns the correct array of tags. */ public function testDecompose() { $this->assertEquals(['language' => 'en'], G11n::decompose('en')); $this->assertEquals(['language' => 'en', 'region' => 'US'], G11n::decompose('en_US')); $this->assertEquals(['language' => 'en', 'region' => 'US', 'script' => 'Hans'], G11n::decompose('en_Hans_US')); $this->assertEquals(['language' => 'en', 'script' => 'Hans', 'region' => 'US', 'variant0' => 'NEDIS', 'private0' => 'prv1'], G11n::decompose('en_Hans_US_nedis_x_prv1')); }
/** * {@inheritdoc} * * @uses Titon\Common\Registry */ public static function transliterate($string) { $g11n = G11n::registry(); if (!$g11n->isEnabled()) { return $string; } return static::cache([__METHOD__, $string], function () use($string, $g11n) { $inflections = $g11n->current()->getInflectionRules(); if (!$inflections || empty($inflections['transliteration'])) { return $string; } // Replace with ASCII characters $transliterations = $inflections['transliteration']; $string = str_replace(array_keys($transliterations), array_values($transliterations), $string); // Remove any left over non 7bit ASCII return preg_replace('/[^\\x09\\x0A\\x0D\\x20-\\x7E]/', '', $string); }); }
function __($id, $catalog = 'default', $domain = 'core', array $params = []) { return G11n::registry()->translate(sprintf('%s.%s.%s', $domain, $catalog, $id), $params); }