Ejemplo n.º 1
0
 /**
  * @param array $subtags
  *
  * @return Locale
  *
  * @throws \InvalidArgumentException
  */
 private static function build(array $subtags)
 {
     if (!isset($subtags['language'])) {
         throw new \InvalidArgumentException('Invalid locale.');
     }
     if (preg_match('/^[a-zA-Z]{2,8}$/', $subtags['language']) == 0) {
         throw new \InvalidArgumentException('Invalid language subtag.');
     }
     if (isset($subtags['script'])) {
         if (preg_match('/^[a-zA-Z]{4}$/', $subtags['script']) == 0) {
             throw new \InvalidArgumentException('Invalid script subtag.');
         }
     }
     if (isset($subtags['region'])) {
         if (preg_match('/^[a-zA-Z]{2}|[0-9]{3}$/', $subtags['region']) == 0) {
             throw new \InvalidArgumentException('Invalid region subtag.');
         }
     }
     for ($i = 0; isset($subtags[$key = 'variant' . $i]); $i++) {
         if (preg_match('/^[0-9][0-9a-zA-Z]{3}|[0-9a-zA-Z]{5,8}$/', $subtags[$key]) == 0) {
             throw new \InvalidArgumentException('Invalid variant subtag.');
         }
     }
     $locale = \Locale::composeLocale($subtags);
     // Another round trip to canonicalize the locale with parseLocale().
     $subtags = \Locale::parseLocale($locale);
     $locale = \Locale::composeLocale($subtags);
     return new Locale($locale, $subtags);
 }
Ejemplo n.º 2
0
 public function getLocale()
 {
     if (!isset($this->locale)) {
         if (isset(self::$LOCALE__DEFAULT)) {
             $this->locale = self::$LOCALE__DEFAULT;
         } else {
             global $conf;
             global $language;
             if (!isset($conf['site_default_country'])) {
                 $conf['site_default_country'] = 'us';
             }
             $country = isset($conf['site_default_country']) ? trim($conf['site_default_country']) : '';
             if ($country === '') {
                 throw new IllegalStateException(t("Default country was not defined. Go to 'Configuration' | 'Regional and language' | 'Regional settings' to set default country"));
             }
             $localeSubTags = array('language' => $language->language, 'region' => $country);
             $locale = Locale::composeLocale($localeSubTags);
             if ($this->isWindows()) {
                 // the following line was not needed on Windows XP
                 // if we do not include it on Windows 7 localeconv() would return empty value for almost all properties
                 $uselessLocale = setlocale(LC_ALL, NULL);
                 // It is expected to get en-US instead of en_US on Windows
                 $locale = str_replace('_', '-', $locale);
             }
             $this->locale = $locale;
         }
     }
     return $this->locale;
 }
Ejemplo n.º 3
0
 public function __construct($locale)
 {
     $this->localeArray = Locale::parseLocale($locale);
     $this->localeString = Locale::composeLocale($this->localeArray);
     $this->initMonths();
     $this->initDays();
     $this->initPattern();
 }
 public function convert($locale)
 {
     $parts = \Locale::parseLocale($locale);
     if (!isset($parts['region'])) {
         $parts['region'] = $this->country;
     }
     $locale = \Locale::canonicalize(\Locale::composeLocale($parts));
     return $locale;
 }
Ejemplo n.º 5
0
 /**
  * Clean locale leaving only language and script
  *
  * @param string $locale
  * @return string
  */
 protected function cleanLocale($locale)
 {
     $localeParts = \Locale::parseLocale($locale);
     $cleanLocaleParts = [];
     if (isset($localeParts['language'])) {
         $cleanLocaleParts['language'] = $localeParts['language'];
     }
     if (isset($localeParts['script'])) {
         $cleanLocaleParts['script'] = $localeParts['script'];
     }
     return \Locale::composeLocale($cleanLocaleParts);
 }
Ejemplo n.º 6
0
 /**
  * @Route("/available-translations", name="oro_translation_available_translations")
  * @Template
  */
 public function availableTranslationsAction()
 {
     $statisticProvider = $this->get('oro_translation.statistic_provider');
     $cm = $this->get('oro_config.global');
     $stats = $statisticProvider->get();
     $defaultValue = $cm->get(LanguageType::CONFIG_KEY, true);
     // @TODO find better solution
     if ($defaultValue == 'en') {
         $defaultValue = \Locale::composeLocale(['language' => $defaultValue, 'region' => 'US']);
     }
     $configValues = $cm->get(TranslationStatusInterface::CONFIG_KEY);
     $localeChoices = Intl::getLocaleBundle()->getLocaleNames();
     return ['statistic' => $stats, 'defaultLanguage' => $defaultValue, 'config' => (array) $configValues, 'locale' => $localeChoices];
 }
    protected function prepareLocale() {
        global $conf;
        global $language;

        $country = isset($conf['site_default_country']) ? trim($conf['site_default_country']) : '';
        if ($country === '') {
            $country = 'us';
            LogHelper::log_warn(t("Default country was not defined. Go to 'Configuration' | 'Regional and language' | 'Regional settings' to set default country"));
        }

        $localeSubTags = array('language' => $language->language, 'region' => $country);

        return Locale::composeLocale($localeSubTags);
    }
Ejemplo n.º 8
0
 /**
  * Creates a locale from a language code and, optionally, a country/region code, a script code, and variants.
  *
  * If the locale is to indicate any variants, the variants follow the script code's argument when calling this
  * method.
  *
  * @param  string $language The two-letter ISO 639 language code (case-insensitive).
  * @param  string $region **OPTIONAL. Default is** *none*. The two-letter ISO 3166 (or ISO 639) country/region
  * code (case-insensitive).
  * @param  string $script **OPTIONAL. Default is** *none*. The four-letter script code (case-insensitive).
  *
  * @return CULocale The new locale.
  */
 public static function fromComponents($language, $region = null, $script = null)
 {
     assert('is_cstring($language) && (!isset($region) || is_cstring($region)) && ' . '(!isset($script) || is_cstring($script))', vs(isset($this), get_defined_vars()));
     assert('CRegex::find($language, "/^[a-z]{2,3}\\\\z/i")', vs(isset($this), get_defined_vars()));
     assert('!isset($region) || CRegex::find($region, "/^[a-z]{2,3}\\\\z/i")', vs(isset($this), get_defined_vars()));
     assert('!isset($script) || CRegex::find($script, "/^[a-z]{4}\\\\z/i")', vs(isset($this), get_defined_vars()));
     $funcNumArgs = func_num_args();
     assert('$funcNumArgs - 3 <= 15', vs(isset($this), get_defined_vars()));
     // the variants are limited by 15
     $subtags = CMap::make();
     $subtags[Locale::LANG_TAG] = $language;
     if (isset($region)) {
         $subtags[Locale::REGION_TAG] = $region;
     }
     if (isset($script)) {
         $subtags[Locale::SCRIPT_TAG] = $script;
     }
     $variantIdx = 0;
     for ($i = 3; $i < func_num_args(); $i++) {
         $variant = func_get_arg($i);
         assert('is_cstring($variant)', vs(isset($this), get_defined_vars()));
         $subtags[Locale::VARIANT_TAG . $variantIdx] = $variant;
         $variantIdx++;
     }
     $localeName = Locale::composeLocale($subtags);
     return new self($localeName);
 }
Ejemplo n.º 9
0
Archivo: G11n.php Proyecto: titon/g11n
 /**
  * Takes an array of key-values and returns a correctly ordered and delimited locale ID.
  *
  * @uses Locale
  *
  * @param array $tags
  * @return string
  */
 public static function compose(array $tags)
 {
     return \Locale::composeLocale($tags);
 }
Ejemplo n.º 10
0
 public static function of($language, $region = '')
 {
     return new Locale(\Locale::composeLocale(['language' => $language, 'region' => $region]));
 }
Ejemplo n.º 11
0
function updateLocaleSetting($locale)
{
    global $error, $Settings, $success;
    if ($Settings->getSetting('locale') == $locale) {
        return true;
    }
    $newLocale = Locale::parseLocale($locale);
    // If ['language'] isn't set, then we can't pick a language, so whole Locale is invalid. Region part of Locale isn't as important as Language is. Could default to English if no langauge, so Region would work, but they could just append en_ to the locale themself
    if (isset($newLocale['language'])) {
        $locale = Locale::composeLocale($newLocale);
        if ($Settings->setSetting('locale', $locale)) {
            // Apply new locale so language displays correctly from now on
            \Grase\Locale::applyLocale($locale);
            $success[] = T_("Locale updated");
            AdminLog::getInstance()->log(T_("Locale updated to") . $locale);
        } else {
            $error[] = T_("Error updating Locale");
        }
    } else {
        $error[] = T_("Invalid Locale");
    }
}
Ejemplo n.º 12
0
 public static function normalize($locale)
 {
     return str_replace('_', '-', \Locale::composeLocale(\Locale::parseLocale($locale)));
 }
Ejemplo n.º 13
0
 /**
  * Create a correctly ordered and delimited locale from subtags
  *
  * @param  array        $subtags an array containing a list of key-value
  *     pairs, where the keys identify the particular locale ID subtags, and
  *     the values are the associated subtag values.
  * @return LocaleFacade
  */
 public static function composeLocale(array $subtags)
 {
     $locale = IntlLocale::composeLocale($subtags);
     return new LocaleFacade($locale);
 }
Ejemplo n.º 14
0
 /**
  * Sets the locale to the given locale
  *
  * This function will also set the "old" setlocale, mainly timezone support for PHP5.3 - PHP5.5
  *
  * @param string $locale Locale we want to set in RFC 4646 format
  * @return string Returns the setted locale
  */
 public function setDefault($locale)
 {
     $result = '';
     if (!empty($locale)) {
         $locale = \Locale::parseLocale($locale);
         $locale = \Locale::composeLocale($locale);
         \Locale::setDefault($locale);
         $result = $this->getDefault();
         $this->_setOptions();
     }
     return $result;
 }