/** * @link http://stackoverflow.com/a/10375234/403165 * @param string $countryCode * @param string $languageCode * * @return string */ public static function countryCodeToLocale($countryCode, $languageCode = '') { $locale = 'en-' . $countryCode; $localeRegion = locale_get_region($locale); $localeLanguage = locale_get_primary_language($locale); $localeArray = ['language' => $localeLanguage, 'region' => $localeRegion]; if (strtoupper($countryCode) === $localeRegion && ($languageCode === '' || strtolower($languageCode) === $localeLanguage)) { return locale_compose($localeArray); } return NULL; }
/** * Handles the language chooser * * @param \Symfony\Component\HttpFoundation\Request $request * @return Response the request response */ public function indexAction(Request $request) { $enableAutodetect = $this->container->getParameter('kunstmaan_language_chooser.autodetectlanguage'); $enableSplashpage = $this->container->getParameter('kunstmaan_language_chooser.showlanguagechooser'); $defaultLocale = $this->container->getParameter('defaultlocale'); if ($enableAutodetect) { $localeGuesserManager = $this->get('lunetics_locale.guesser_manager'); $locale = $localeGuesserManager->runLocaleGuessing($request); if ($locale) { //locale returned will in the form of en_US, we need en $locale = locale_get_primary_language($locale); } // locale has been found, redirect if ($locale) { return $this->redirect($this->generateUrl('_slug', array('_locale' => $locale)), 302); } else { // no locale could be guessed, if splashpage is not enabled fallback to default and redirect if (!$enableSplashpage) { return $this->redirect($this->generateUrl('_slug', array('_locale' => $defaultLocale)), 302); } } } if ($enableSplashpage) { $viewPath = $this->container->getParameter('kunstmaan_language_chooser.languagechoosertemplate'); return $this->render($viewPath); } }
/** * */ public function initialize() { // Define locale (first from session, then from HTTP header) $this->_localeCode = \z\request()->session('culture/locale'); if (empty($this->_localeCode) === true) { $this->_localeCode = locale_accept_from_http(\z\request()->header('Accept-Language')); } if (empty($this->_localeCode) === true) { $this->_localeCode = \z\pref('culture/locale'); } // Define fallback $this->_fallbackCode = \z\pref('culture/fallback'); // Canonicalize locales $this->_localeCode = locale_canonicalize($this->_localeCode); $this->_fallbackCode = locale_canonicalize($this->_fallbackCode); // List locales available $locales = \z\service('helper/file')->listFiles(PATH_APP . '/Config/Strings', null, false, true, false); // Is the locale available? if (in_array($this->_localeCode, $locales) === false) { // Grab the parent code (e.g. en for en_US) $primaryCode = locale_get_primary_language($this->_localeCode); // Find locales belonging to this parent $locales = array_filter($locales, function ($key) use($primaryCode) { return strpos($key, $primaryCode) === 0; }); // Sort locales A-Z sort($locales); // Grab the very first locale $this->_localeCode = array_shift($locales); } // Load strings $this->loadStrings(); }
/** * Returns a locale from a country code that is provided. * @link http://stackoverflow.com/a/10375234/403165 * @param string $country_code ISO 3166-2-alpha 2 country code * @param string $language_code ISO 639-1-alpha 2 language code * @returns string a locale, formatted like en_US, or null if not found */ public static function country_code_to_locale($country_code, $language_code = '') { $locale = 'en-' . $country_code; $locale_region = locale_get_region($locale); $locale_language = locale_get_primary_language($locale); $locale_array = array('language' => $locale_language, 'region' => $locale_region); if (strtoupper($country_code) == $locale_region && $language_code == '') { return locale_compose($locale_array); } elseif (strtoupper($country_code) == $locale_region && strtolower($language_code) == $locale_language) { return locale_compose($locale_array); } return null; }
public static function applyLocale($newlocale) { self::$locale = $newlocale; //TODO Allow locale to be overriden by GET request? //if($_GET['lang']) $locale = $_GET['lang']; locale_set_default(self::$locale); //$language = locale_get_display_language(self::$locale, 'en'); $lang = locale_get_primary_language(self::$locale); //$region = locale_get_display_region(self::$locale); T_setlocale(LC_MESSAGES, $lang); T_bindtextdomain("grase", "/usr/share/grase/locale"); T_bind_textdomain_codeset("grase", "UTF-8"); T_textdomain("grase"); }
/** * Renders a date time widget. * * @param array $data Data to render with. * @param \Cake\View\Form\ContextInterface $context The current form context. * @return string A generated select box. * @throws \RuntimeException When option data is invalid. */ public function render(array $data, ContextInterface $context) { $id = $data['id']; $name = $data['name']; $val = $data['val']; $type = $data['type']; $required = $data['required'] ? 'required' : ''; $role = isset($data['role']) ? $data['role'] : 'datetime-picker'; $format = null; $timestamp = null; $locale = locale_get_primary_language(I18n::locale()); if (isset($data['data-format'])) { $format = $this->_convertPHPToMomentFormat($data['data-format']); } if (!$val instanceof DateTime && !empty($val)) { $val = $type === 'date' ? Time::parseDate($val) : Time::parseDateTime($val); } if ($val) { $timestamp = $val->format('U'); $val = $val->format($type === 'date' ? 'Y-m-d' : 'Y-m-d H:i:s'); } if (!$format) { $format = $type === 'date' ? 'L' : 'L LT'; } $widget = <<<html <div class="input-group {$type}"> <input type="text" class="form-control" name="{$name}" value="{$val}" id="{$id}" role="{$role}" data-locale="{$locale}" data-format="{$format}" data-timestamp="{$timestamp}" {$required} /> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> html; return $widget; }
/** /* Returns a locale from a country code that is provided. /* * @link http://stackoverflow.com/a/10375234/403165 /* @param $country_code ISO 3166-2-alpha 2 country code /* @param $language_code ISO 639-1-alpha 2 language code /* @returns a locale, formatted like en_US, or null if not found /**/ public static function country_code_to_locale($country_code, $language_code = '') { // Locale list taken from: // http://stackoverflow.com/questions/3191664/ // list-of-all-locales-and-their-short-codes $locales = array('af-ZA', 'am-ET', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'arn-CL', 'ar-OM', 'ar-QA', 'ar-SA', 'ar-SY', 'ar-TN', 'ar-YE', 'as-IN', 'az-Cyrl-AZ', 'az-Latn-AZ', 'ba-RU', 'be-BY', 'bg-BG', 'bn-BD', 'bn-IN', 'bo-CN', 'br-FR', 'bs-Cyrl-BA', 'bs-Latn-BA', 'ca-ES', 'co-FR', 'cs-CZ', 'cy-GB', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LI', 'de-LU', 'dsb-DE', 'dv-MV', 'el-GR', 'en-029', 'en-AU', 'en-BS', 'en-BZ', 'en-CA', 'en-GB', 'en-IE', 'en-IN', 'en-JM', 'en-MY', 'en-NZ', 'en-PH', 'en-SG', 'en-TT', 'en-US', 'en-ZA', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-DO', 'es-EC', 'es-ES', 'es-GT', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PR', 'es-PY', 'es-SV', 'es-US', 'es-UY', 'es-VE', 'et-EE', 'eu-ES', 'fa-IR', 'fi-FI', 'fil-PH', 'fo-FO', 'fr-BE', 'fr-CA', 'fr-CH', 'fr-FR', 'fr-LU', 'fr-MC', 'fy-NL', 'ga-IE', 'gd-GB', 'gl-ES', 'gsw-FR', 'gu-IN', 'ha-Latn-NG', 'he-IL', 'hi-IN', 'hr-BA', 'hr-HR', 'hsb-DE', 'hu-HU', 'hy-AM', 'id-ID', 'ig-NG', 'ii-CN', 'is-IS', 'it-CH', 'it-IT', 'iu-Cans-CA', 'iu-Latn-CA', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'km-KH', 'kn-IN', 'kok-IN', 'ko-KR', 'ky-KG', 'lb-LU', 'lo-LA', 'lt-LT', 'lv-LV', 'mi-NZ', 'mk-MK', 'ml-IN', 'mn-MN', 'mn-Mong-CN', 'moh-CA', 'mr-IN', 'ms-BN', 'ms-MY', 'mt-MT', 'nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'nso-ZA', 'oc-FR', 'or-IN', 'pa-IN', 'pl-PL', 'prs-AF', 'ps-AF', 'pt-BR', 'pt-PT', 'qut-GT', 'quz-BO', 'quz-EC', 'quz-PE', 'rm-CH', 'ro-RO', 'ru-RU', 'rw-RW', 'sah-RU', 'sa-IN', 'se-FI', 'se-NO', 'se-SE', 'si-LK', 'sk-SK', 'sl-SI', 'sma-NO', 'sma-SE', 'smj-NO', 'smj-SE', 'smn-FI', 'sms-FI', 'sq-AL', 'sr-Cyrl-BA', 'sr-Cyrl-CS', 'sr-Cyrl-ME', 'sr-Cyrl-RS', 'sr-Latn-BA', 'sr-Latn-CS', 'sr-Latn-ME', 'sr-Latn-RS', 'sv-FI', 'sv-SE', 'sw-KE', 'syr-SY', 'ta-IN', 'te-IN', 'tg-Cyrl-TJ', 'th-TH', 'tk-TM', 'tn-ZA', 'tr-TR', 'tt-RU', 'tzm-Latn-DZ', 'ug-CN', 'uk-UA', 'ur-PK', 'uz-Cyrl-UZ', 'uz-Latn-UZ', 'vi-VN', 'wo-SN', 'xh-ZA', 'yo-NG', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-SG', 'zh-TW', 'zu-ZA'); foreach ($locales as $locale) { $locale_region = locale_get_region($locale); $locale_language = locale_get_primary_language($locale); $locale_array = array('language' => $locale_language, 'region' => $locale_region); if (strtoupper($country_code) == $locale_region && $language_code == '') { return locale_compose($locale_array); } elseif (strtoupper($country_code) == $locale_region && strtolower($language_code) == $locale_language) { return locale_compose($locale_array); } } return null; }
/** * Registers a FullCalendar plugin */ protected function registerScript() { $view = $this->getView(); $asset = FullCalendarAsset::register($view); PrintAsset::register($view); if ($this->googleCalendar) { GoogleCalendarAsset::register($view); } if ($this->clientOptions !== false) { $language = isset($this->clientOptions['lang']) ? $this->clientOptions['lang'] : Yii::$app->language; $language = strtolower($language); $basePath = "{$asset->basePath}/lang"; if (!file_exists($basePath . "/{$language}.js")) { $language = locale_get_primary_language($language); } if (file_exists($basePath . "/{$language}.js")) { $view->registerJsFile("{$asset->baseUrl}/lang/{$language}.js", ['depends' => ['lav45\\widget\\FullCalendarAsset']]); } elseif (isset($this->clientOptions['lang'])) { unset($this->clientOptions['lang']); } $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions); $view->registerJs("jQuery('#{$this->options['id']}').fullCalendar({$options});"); } }
/** * Converts a locale Id to a language Id. * A language ID consists of only the first group of letters before an underscore or dash. * @param string $id the locale ID to be converted * @return string the language ID */ public function getLanguage($locale = null) { if ($locale === null) { $locale = P::$app->language; } return locale_get_primary_language($locale); }
/** * Set locale among all localizable contexts. * * @param string $posixLocale * * @return void */ function setGlobalLocale($posixLocale) { app()->setLocale($posixLocale); setlocale(LC_TIME, $posixLocale); Carbon::setLocale(locale_get_primary_language($posixLocale)); }
/** * Find closest matching name for a currency based on currently set locale. * Default to 'en' entry if none more suitable found * * @param \SimpleXMLElement $currency * * @return string */ protected static function createName(\SimpleXMLElement $currency) { $locale = self::getLocale(); //first - see if we have an exact locale match $nodes = $currency->xpath("name[@lang='{$locale}']"); if (count($nodes) > 0) { return (string) $nodes[0]; } //next, see if we have a name for the language part of the locale $lang = \locale_get_primary_language($locale()); $nodes = $currency->xpath("name[@lang='{$lang}']"); if (count($nodes) > 0) { return (string) $nodes[0]; } //else default to using 'en' $nodes = $currency->xpath("name[@lang='en']"); return (string) $nodes[0]; }
/** * Translate a language using INTL extension * * @param string $locale * @param string $language */ function translate_locale($locale, $language) { if (is_null($language)) { return null; } elseif (is_array($language)) { foreach ($language as $lang) { $out = translate_locale($locale, $lang); if (!empty($out)) { return $out; } } } elseif (is_string($language)) { $languageName = locale_get_display_language($locale, $language); $languageName = trim($languageName); if (empty($languageName) || strcasecmp($languageName, $locale) === 0) { return null; } if (strcasecmp($language, 'en') !== 0) { $englishName = locale_get_display_language($locale, 'en'); if (empty($englishName) || strcasecmp($englishName, $locale) === 0 || strcasecmp($englishName, $languageName) === 0) { return null; } } $defaultLanguage = locale_get_primary_language(__DEFAULT_LOCALE__); if (strcasecmp($language, $defaultLanguage) !== 0) { $defaultName = locale_get_display_language($locale, __DEFAULT_LOCALE__); if (empty($defaultName) || strcasecmp($defaultName, $locale) === 0 || strcasecmp($defaultName, $languageName) === 0) { return null; } } $defaultLocale = locale_get_default(); $defaultLanguage = locale_get_primary_language($defaultLocale); if (strcasecmp($language, $defaultLanguage) !== 0) { $defaultName = locale_get_display_language($locale, $defaultLocale); if (empty($defaultName) || strcasecmp($defaultName, $locale) === 0 || strcasecmp($defaultName, $languageName) === 0) { return null; } } return $languageName; } else { echo "Error: \$language must be a string or array.\n"; exit(1); } return null; }