/** * A list of languages for which special characters are available. * * @return string[] */ public static function allLanguages() { $array = array(); foreach (self::$languages as $language) { $array[$language] = Locale::create($language)->endonym(); } uasort($array, '\\Fisharebest\\Webtrees\\I18N::strCaseCmp'); return $array; }
/** * Load translations defined in the MyArtJaub modules. */ protected function loadMajModulesTranslations() { if (defined('GLOB_BRACE')) { $maj_translations_files = glob(WT_MODULES_DIR . 'myartjaub_*/language/' . $this->locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array(); } else { // Some servers do not have GLOB_BRACE - see http://php.net/manual/en/function.glob.php $maj_translations_files = array_merge(glob(WT_MODULES_DIR . 'myartjaub_*/language/' . $this->locale->languageTag() . '.csv') ?: array(), glob(WT_MODULES_DIR . 'myartjaub_*/language/' . $this->locale->languageTag() . '.php') ?: array(), glob(WT_MODULES_DIR . 'myartjaub_*/language/' . $this->locale->languageTag() . '.mo') ?: array()); } foreach ($maj_translations_files as $translation_file) { $translation = new Translation($translation_file); foreach (array_keys($translation->asArray()) as $msg) { $msgid = md5($msg); if (!isset($this->maj_translations[$msgid])) { $this->maj_translations[$msgid] = array('text' => $msg, 'references' => array()); } $path_parts = explode(DIRECTORY_SEPARATOR, substr($translation_file, strlen(WT_MODULES_DIR))); $path_parts = explode('/', $path_parts[0]); $this->maj_translations[$msgid]['references'][] = $path_parts[0]; } } }
/** * Create a locale from a language tag (or locale code). * * @param string[] $server The $_SERVER array * @param LocaleInterface[] $available All locales supported by the application * @param LocaleInterface $default Locale to show in no matching locales * * @return LocaleInterface */ public static function httpAcceptLanguage(array $server, array $available, LocaleInterface $default) { if (!empty($server['HTTP_ACCEPT_LANGUAGE'])) { $http_accept_language = strtolower(str_replace(' ', '', $server['HTTP_ACCEPT_LANGUAGE'])); preg_match_all('/(?:([a-z][a-z0-9_-]+)(?:;q=([0-9.]+))?)/', $http_accept_language, $match); $preferences = array_map(function ($x) { return $x === '' ? 1.0 : (double) $x; }, array_combine($match[1], $match[2])); // Need a stable sort, as the original order is significant $preferences = array_map(function ($x) { static $n = 0; return array($x, --$n); }, $preferences); arsort($preferences); $preferences = array_map(function ($x) { return $x[0]; }, $preferences); // If "de-DE" requested, but not "de", then add it at a lower priority foreach ($preferences as $code => $priority) { if (preg_match('/^([a-z]+)[^a-z]/', $code, $match) && !isset($preferences[$match[1]])) { $preferences[$match[1]] = $priority * 0.5; } } foreach (array_keys($preferences) as $code) { try { $locale = Locale::create($code); if (in_array($locale, $available)) { return $locale; } } catch (\DomainException $ex) { // An unknown locale? Ignore it. } } } return $default; }
/** * Return the script used by a given language * * @param string $locale * * @return string */ public static function languageScript($locale) { return Locale::create($locale)->script()->code(); }