/** * Create the locale-service * * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator * @return \Zork\I18n\Locale\Locale */ public function createService(ServiceLocatorInterface $serviceLocator) { // Configure the locale $config = $serviceLocator->get('Configuration'); $srvConfig = isset($config['locale']) ? $config['locale'] : array(); return Locale::factory($srvConfig); }
/** * Test accept from http */ public function testAccesptFromHttp() { $locale = Locale::factory(array('default' => 'en', 'available' => array('en' => true, 'fr' => true, 'de' => true))); $this->assertEquals('en', $locale->acceptFromHttp(null)); $this->assertEquals('fr', $locale->acceptFromHttp('fr')); $this->assertEquals('fr', $locale->acceptFromHttp('fr, *;q=0.1')); $this->assertEquals('fr', $locale->acceptFromHttp('fr, de;q=0.8, *;q=0.1')); $this->assertEquals('de', $locale->acceptFromHttp('fr;q=0.8, de, *;q=0.1')); $this->assertEquals('de', $locale->acceptFromHttp('hu-HU, de;q=0.8, *;q=0.1')); $this->assertEquals('de', $locale->acceptFromHttp('hu, de-DE;q=0.8, *;q=0.1')); $this->assertEquals('en', $locale->acceptFromHttp('hu, no;q=0.8, *;q=0.1')); }
/** * Get the most suitable locale from HTTP Accept-Language header * * @param string $header * @param array $available * @return string */ public function acceptFromHttp($header, array $available = null) { if (empty($available)) { $available = $this->getAvailableLocales(); } while (!empty($header)) { $locale = IntlLocale::acceptFromHttp($header); // @codeCoverageIgnoreStart if (empty($locale)) { break; } // @codeCoverageIgnoreEnd $locale = preg_replace('/[_-]POSIX$/i', '', $locale); $normalized = static::normalizeLocale($locale); if (in_array($normalized, $available)) { return $normalized; } $primary = IntlLocale::getPrimaryLanguage($normalized); if (in_array($primary, $available)) { return $primary; } $newHeader = preg_replace(array('/\\s+/', '/(^|,)(' . static::pregQuote($locale, '/') . '[^;,]*|' . static::pregQuote($primary, '/') . '|\\*)' . '(;[^,]*)?(,|$)/i', '/[,;]$/'), array('', '$1', ''), $header); if ($newHeader == $header) { break; } $header = $newHeader; } return $this->getDefault(); }
/** * Sets up the fixture, for example, open a network connection. * This method is called before a test is executed. */ public function setUp() { $this->previousLocale = IntlLocale::getDefault(); static::$locale->setCurrent('en_US'); parent::setUp(); }