/** * Test that formatting locale keys return the correct formats. */ public function testCanonicalize() { $this->assertEquals('en-us', G11n::canonicalize('en-us', G11n::FORMAT_1)); $this->assertEquals('en-US', G11n::canonicalize('en-us', G11n::FORMAT_2)); $this->assertEquals('en_US', G11n::canonicalize('en-us', G11n::FORMAT_3)); $this->assertEquals('en-us', G11n::canonicalize('en-US', G11n::FORMAT_1)); $this->assertEquals('en-US', G11n::canonicalize('en-US', G11n::FORMAT_2)); $this->assertEquals('en_US', G11n::canonicalize('en-US', G11n::FORMAT_3)); $this->assertEquals('en-us', G11n::canonicalize('en_US', G11n::FORMAT_1)); $this->assertEquals('en-US', G11n::canonicalize('en_US', G11n::FORMAT_2)); $this->assertEquals('en_US', G11n::canonicalize('en_US', G11n::FORMAT_3)); }
/** * 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)))); }