public function actionZoneData($locale = '') { if ($locale) { $locale = \Locale::canonicalize($locale); } return $this->render('zone-data', ['locale' => $locale]); }
public function actionIndex($locale = '') { if ($locale) { $locale = \Locale::canonicalize($locale); } return $this->render('index', ['locale' => $locale, 'spelloutRules' => $this->getRules($locale, \NumberFormatter::SPELLOUT), 'ordinalRules' => $this->getRules($locale, \NumberFormatter::ORDINAL), 'durationRules' => $this->getRules($locale, \NumberFormatter::DURATION), 'pluralCardinalRules' => $this->getPluralCardinalRules($locale), 'pluralCardinalExample' => $this->getPluralCardinalExample($locale), 'pluralOrdinalRules' => $this->getPluralOrdinalRules($locale), 'pluralOrdinalExample' => $this->getPluralOrdinalExample($locale)]); }
public function convert($locale) { $parts = \Locale::parseLocale($locale); if (!isset($parts['region'])) { $parts['region'] = $this->country; } $locale = \Locale::canonicalize(\Locale::composeLocale($parts)); return $locale; }
/** * @return string */ public static function getCanonicalLocale() { try { $locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); return \Locale::canonicalize($locale); } catch (\Exception $e) { return static::getDefaultLocale(); } }
private function getBestLocale(Request $request) { $acceptLanguage = $request->server->get('HTTP_ACCEPT_LANGUAGE'); if (!$acceptLanguage) { return $this->defaultLocale; } $negotiator = new \Negotiation\LanguageNegotiator(); $locale = $negotiator->getBest($acceptLanguage); if (!$locale) { return $this->defaultLocale; } return \Locale::canonicalize($locale->getValue()); }
protected function getLocale(ServiceLocatorInterface $services) { if ($services->has('LocaleManager')) { $localeManager = $services->get('LocaleManager'); if ($localeManager instanceof LocaleManagerInterface) { return $localeManager->getLocale(); } } // Try to get the locale through the translator if ($services->has('Translator')) { $translator = $services->get('Translator'); if ($translator instanceof \Zend\Mvc\I18n\Translator) { $translator = $translator->getTranslator(); } if (method_exists($translator, 'getLocale')) { return \Locale::canonicalize($translator->getLocale()); } } // If everything went wrong get the default locale return \Locale::getDefault(); }
/** * Creates a locale from a locale name. * * @param string $localeName The name of the locale (case-insensitive). */ public function __construct($localeName) { assert('is_cstring($localeName)', vs(isset($this), get_defined_vars())); assert('self::isValid($localeName)', vs(isset($this), get_defined_vars())); $this->m_name = Locale::canonicalize($localeName); }
/** * * {@inheritDoc} * * @see \Thunderhawk\API\Component\Translator\TranslatorInterface::localeExists() */ public function localeExists($locale) { $locale = \Locale::canonicalize($locale); return Languages::findFirstByLocale($locale) !== false; }
/** * Returns translated string * * @param string $key The key to translate * @param array $params The string values to passe in * @param string $target The target locale string if diferent than current * * @return string The resulting string */ public function translate($key, $params = array(), $target = null) { // Load defauts $current = $this->current; $directory = $this->directory . DIRECTORY_SEPARATOR . $current; $params = (array) $params; // Validate and load different $target if (!empty($target) && $target != $current) { $current = $target; $directory = $this->directory . DIRECTORY_SEPARATOR . $current; // Validate locale and translations directory if (\Locale::canonicalize($current) === null || !is_dir($this->directory . DIRECTORY_SEPARATOR . $current)) { throw new DualityException("Error Locale: target code ", DualityException::E_LOCALE_NOTFOUND); } } // Finally, return result $storage = new Storage(); $storage->importArray(include $directory . DIRECTORY_SEPARATOR . 'messages.php'); return \MessageFormatter::formatMessage($current, $storage->get($key), $params); }
/** * Translate the $resource * * @param string $locale * @param TranslatableResource $resource * * @throws \IntlException * @return string */ public function translate($locale, TranslatableResource $resource) { $canonicalLocale = \Locale::canonicalize($locale); $messageFormatter = new \MessageFormatter($canonicalLocale, $this->retrievePattern($canonicalLocale, $resource->getKey())); return $messageFormatter->format($resource->getParameters()); }
/** * Check whether locale is available * * @param string $locale */ public function hasLocale($locale) { return in_array(\Locale::canonicalize($locale), $this->getLocales()); }