Ejemplo n.º 1
0
 /**
  * Set the current active locale incl. localization parameters.
  *
  * @param string|Locale $locale
  *
  * @return $this
  * @throws Exception
  */
 public function setActive($locale)
 {
     $active = $this->findByLocale($locale);
     if (!$active) {
         throw new Exception(sprintf("Locale '%s' not in list.", $locale));
     }
     // Mark as current locale
     $this->active = $active;
     // Set as global default for other classes
     \Locale::setDefault($active->getLocale());
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Sanitize given locale.
  *
  * Must be a simple locale without code set, currency or similar (e. g. "de_DE" or "de-de").
  *
  * @param string|Locale $locale
  * @param string|Locale $default [optional] The default locale to return if $locale is invalid
  *
  * @return null|string
  */
 public static function sanitizeLocale($locale, $default = null)
 {
     if ($locale instanceof Locale) {
         return $locale->getLocale();
     }
     if ($default) {
         $default = Locale::sanitizeLocale($default, null);
     }
     $locale = (string) $locale;
     $locale = trim($locale);
     if (!$locale || !preg_match("/^" . Locale::REGEX_LOCALE . "\$/ui", $locale, $found)) {
         return $default;
     }
     $found['language'] = Text::toLower($found['language']);
     $found['country'] = Text::toUpper($found['country']);
     return sprintf("%s_%s", $found['language'], $found['country']);
 }
Ejemplo n.º 3
0
 /**
  * Get map for specified name.
  *
  * @param string|Locale $locale
  * @param string        $key
  *
  * @return array|null
  */
 public function getMap($locale, $key = null)
 {
     // Get sanitized locale
     $locale = Locale::sanitizeLocale($locale);
     if ($key) {
         return $this->maps[$locale][$key] ?? null;
     }
     return $this->maps[$locale] ?? null;
 }