Beispiel #1
0
 /**
  * Add locale to available locales.
  *
  * @param string|Locale $locale
  *
  * @return $this
  */
 public function addLocale($locale)
 {
     // Get sanitized locale
     $locale = Locale::sanitizeLocale($locale);
     if (!isset($this->available[$locale])) {
         $locale = new Locale($locale);
         $this->available[$locale->getLocale()] = $locale;
     }
     return $this;
 }
Beispiel #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']);
 }