/** * Determines which locale to use, based on the HTTP Accept-Language header. * @param zibo\library\i18n\locale\LocaleManager $manager The locale manager * @return null|zibo\library\i18n\locale\Locale the locale */ public function getLocale(LocaleManager $manager) { $request = $this->zibo->getRequest(); if (!$request) { return null; } $fallbackLanguages = array(); $acceptedLanguages = $request->getAcceptLanguage(); foreach ($acceptedLanguages as $acceptedLanguage => $null) { if (strpos($acceptedLanguage, self::SEPARATOR_TERRITORY) === false) { $locale = strtolower($acceptedLanguage); } else { list($language, $territory) = explode(self::SEPARATOR_TERRITORY, $acceptedLanguage); $language = strtolower($language); $locale = $language . '_' . strtoupper($territory); $fallbackLanguages[$language] = true; } if ($manager->hasLocale($locale)) { return $manager->getLocale($locale); } } foreach ($fallbackLanguages as $locale => $null) { if ($manager->hasLocale($locale)) { return $manager->getLocale($locale); } } return null; }
/** * Gets the locale. * * @param string $code the locale code, if not specified then the current locale is assumed * @return zibo\library\i18n\locale\Locale * * @throws zibo\library\i18n\exception\LocaleNotFoundException if the locale with the specified code could not be found * @throws zibo\ZiboException when $code is not specified and no locales could be found */ public function getLocale($code = null) { return $this->localeManager->getLocale($code); }