/** * Parse header * * @param null|\Zend\Http\Header\AcceptLanguage $header * @return array */ public function parseHeader($header) { $locales = array(); $controller = $this->getController(); if ($controller instanceof ServiceLocatorAwareInterface) { $availables = $controller->getServiceLocator()->get('Locale')->getAvailableLocales(); } else { $availables = array(IntlLocale::getDefault()); } if ($header instanceof AcceptLanguage) { foreach ($header->getPrioritized() as $part) { if ($part instanceof LanguageFieldValuePart) { $locale = IntlLocale::parseLocale($part->getLanguage()); $key = $locale['language']; if (isset($locale['region'])) { $key .= '_' . $locale['region']; } if ($availables) { $key = IntlLocale::lookup($availables, $key, false, ''); } if ($key) { $locales[$key] = max($part->getPriority(), empty($locales[$key]) ? 0 : $locales[$key]); } } } } return $locales; }
/** * @param Headers $headers * * @return string|null */ private function getBrowseAcceptLanguage(Headers $headers) { if ($headers->has('Accept-Language')) { /** @var AcceptLanguage $acceptLanguageHeader */ $acceptLanguageHeader = $headers->get('Accept-Language'); $locales = $acceptLanguageHeader->getPrioritized(); $languages = $this->config->getAvailableLanguages(); /** @var LanguageFieldValuePart $locale */ foreach ($locales as $locale) { // Loop through all locales, highest priority first if ($browserLang = \Locale::lookup($languages, $locale->getRaw())) { return $browserLang; } } } return null; }
/** * {@inheritDoc} */ public function detect(Event $event) { if (!($authService = $this->getAuthenticationService())) { return; } if ($lookup = $event->hasLocales()) { $locales = $event->getLocales(); } if ($authService->hasIdentity()) { $identity = $authService->getIdentity(); if ($identity instanceof LocalizableInterface && $identity->getLocale()) { $locale = $identity->getLocale()->getCanonicalName(); if (!$lookup) { return $locale; } if (\Locale::lookup($locales, $locale)) { return $locale; } } } }
/** * {@inheritDoc} */ public function detect(Event $event) { $request = $event->getRequest(); if (!$this->isHttpRequest($request)) { return; } $lookup = $event->hasLocales(); if ($lookup) { $locales = $event->getLocales(); } $headers = $request->getHeaders(); if ($headers->has('Accept-Language')) { foreach ($headers->get('Accept-Language')->getPrioritized() as $locale) { $locale = $locale->getLanguage(); if (!$lookup) { return $locale; } if (\Locale::lookup($locales, $locale)) { return $locale; } } } }
{ public function getI18NGlobalDirs() { return array(dirname(__FILE__) . '/fixtures'); } } $configuration = new TestConfiguration('test', true, sfConfig::get('sf_test_cache_dir', sys_get_temp_dir())); $dispatcher = $configuration->getEventDispatcher(); $cache = new sfNoCache(); // ->initialize() $t->diag('->initialize()'); $i18n = new sfI18N($configuration, $cache); $dispatcher->notify(new sfEvent(null, 'user.change_culture', array('culture' => 'fr'))); $t->is($i18n->getCulture(), 'fr', '->initialize() connects to the user.change_culture event'); // passing a "culture" option to initialize() should set PHP locale if (version_compare(PHP_VERSION, '5.3', '<') && class_exists('Locale') && ($en = Locale::lookup(array('en-US'), 'en-US', true)) && ($fr = Locale::lookup(array('fr-FR'), 'fr-FR', true))) { $i18n = new sfI18N($configuration, $cache, array('culture' => $fr)); $frLocale = localeconv(); $i18n = new sfI18N($configuration, $cache, array('culture' => $en)); $enLocale = localeconv(); $t->isnt(serialize($frLocale), serialize($enLocale), '->initialize() sets the PHP locale when a "culture" option is provided'); } else { $t->skip('PHP version > 5.2 or Locale class or English and French locales are not installed'); } // ->getCulture() ->setCulture() $t->diag('->getCulture() ->setCulture()'); $i18n = new sfI18N($configuration, $cache); $t->is($i18n->getCulture(), 'en', '->getCulture() returns the current culture'); $i18n->setCulture('fr'); $t->is($i18n->getCulture(), 'fr', '->setCulture() sets the current culture'); // ->__()
{ public function getI18NGlobalDirs() { return array(dirname(__FILE__) . '/fixtures'); } } $configuration = new TestConfiguration('test', true, sfConfig::get('sf_test_cache_dir', sys_get_temp_dir())); $dispatcher = $configuration->getEventDispatcher(); $cache = new sfNoCache(); // ->initialize() $t->diag('->initialize()'); $i18n = new sfI18N($configuration, $cache); $dispatcher->notify(new sfEvent(null, 'user.change_culture', array('culture' => 'fr'))); $t->is($i18n->getCulture(), 'fr', '->initialize() connects to the user.change_culture event'); // passing a "culture" option to initialize() should set PHP locale if (class_exists('Locale') && ($en = Locale::lookup(array('en-US'), 'en-US', true)) && ($fr = Locale::lookup(array('fr-FR'), 'fr-FR', true))) { $i18n = new sfI18N($configuration, $cache, array('culture' => $fr)); $frLocale = localeconv(); $i18n = new sfI18N($configuration, $cache, array('culture' => $en)); $enLocale = localeconv(); $t->isnt(serialize($frLocale), serialize($enLocale), '->initialize() sets the PHP locale when a "culture" option is provided'); } else { $t->skip('Locale class or English and French locales are not installed'); } // ->getCulture() ->setCulture() $t->diag('->getCulture() ->setCulture()'); $i18n = new sfI18N($configuration, $cache); $t->is($i18n->getCulture(), 'en', '->getCulture() returns the current culture'); $i18n->setCulture('fr'); $t->is($i18n->getCulture(), 'fr', '->setCulture() sets the current culture'); // ->__()
protected function detectMatchingLocale() { $this->logger->debug(__FUNCTION__); // Now iterate through all `detectedLanguages` and stop at the first // that matches an entry on the `supported_locales` list: foreach ($this->detectedLanguages as $language) { // $matchedLocale will contain an empty string if current // $language did not match any locale in $supportedLocales. $matchedLocale = \Locale::lookup($this->cfg['supported_locales'], $language); // Found match: if (!empty($matchedLocale)) { $this->locale = $matchedLocale; break; } } // If no matching locale was found at all: if (empty($matchedLocale)) { // falling back to the configured default locale $this->locale = $this->cfg['default_locale']; } $this->logger->debug($this->locale); // return $this; }
// Register events foreach (config()->events as $event => $class) { event($event, NULL, $class); } /* if(preg_match_all('/[\-a-z]{2,}/i', getenv('HTTP_ACCEPT_LANGUAGE'), $locales)) { $locales = $locales[0]; } */ // Get locale from user agent if (isset($_COOKIE['lang'])) { $preference = $_COOKIE['lang']; } else { $preference = Locale::acceptFromHttp(getenv('HTTP_ACCEPT_LANGUAGE')); } // Match preferred language to those available, defaulting to generic English $locale = Locale::lookup(config()->languages, $preference, false, 'en'); // Default Locale Locale::setDefault($locale); setlocale(LC_ALL, $locale . '.utf-8'); //putenv("LC_ALL", $locale); // Default timezone of server date_default_timezone_set('UTC'); // iconv encoding iconv_set_encoding("internal_encoding", "UTF-8"); // multibyte encoding mb_internal_encoding('UTF-8'); // Enable global error handling set_error_handler(array('\\Micro\\Error', 'handler')); register_shutdown_function(array('\\Micro\\Error', 'fatal'));
<?php var_dump(Locale::lookup(new stdclass(), 'foo')); var_dump(Locale::lookup(STDIN, 'foo'));
/** * Searches the language tag list for the best match to the language * * @param array $langtag An array containing a list of language tags to compare to locale. * Maximum 100 items allowed. * @param array $canonicalize If true, the arguments will be converted to canonical form before matching. * @param array $default The locale to use if no match is found. * @return string The closest matching language tag or default value. */ public function lookup(array $langtag, $canonicalize = false, $default = '') { return IntlLocale::lookup($langtag, $this->getLocale(), $canonicalize, $default); }
private function detectN2nLocale(array $n2nLocales) { $n2nLocale = null; if (!empty($n2nLocales)) { $n2nLocale = reset($n2nLocales); } else { $n2nLocale = N2nLocale::getDefault(); } if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { return $n2nLocale; } if (null !== ($n2nLocaleId = N2nLocale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))) { if (isset($n2nLocales[$n2nLocaleId])) { return $n2nLocales[$n2nLocaleId]; } $n2nLocaleId = \Locale::lookup(array_keys($n2nLocales), $n2nLocaleId); if ($n2nLocaleId) { return $n2nLocales[$n2nLocaleId]; } } return $n2nLocale; }
/** * Set the default locale for this request * * @param string $locale The locale desired */ public static function setLocale($locale) { // Match preferred language to those available, defaulting to generic English $locale = Locale::lookup(config()->languages, $locale, false, 'en'); Locale::setDefault($locale); setlocale(LC_ALL, $locale . '.utf-8'); //putenv("LC_ALL", $locale); }
protected function findLocale($locale) { return \Locale::lookup(array_keys($this->moment_locales), $locale, false, 'en-US'); }
/** * Sets the locale of the application, if this locale is available. * @param string $locale the new locale. */ public function setLocale($locale = null) { if (class_exists('\\Locale')) { $this->locale = \Locale::lookup($this->getAvailableLocales(), $locale, true, $this->getSettings()->locale->default); } if (empty($this->locale)) { $this->locale = $this->getSettings()->locale->default; } // Set up locale environment for gettext. bindtextdomain(self::TEXT_DOMAIN_NAME, self::TEXT_DOMAIN_LOCATION); bind_textdomain_codeset(self::TEXT_DOMAIN_NAME, 'UTF-8'); textdomain(self::TEXT_DOMAIN_NAME); putenv('LANG=' . $this->locale); putenv('LC_MESSAGES=' . $this->locale); $locale = setlocale(LC_MESSAGES, $this->locale); Logger::get()->info(sprintf('Locale is now %s [%s] (domain "%s" at %s)', $locale, $this->locale, self::TEXT_DOMAIN_NAME, realpath(self::TEXT_DOMAIN_LOCATION))); }