Exemple #1
0
 /**
  * Initialise the translation adapter with a locale setting.
  *
  * @param string|null $code Use this locale/language code, or choose one automatically
  *
  * @return string $string
  */
 public static function init($code = null)
 {
     global $WT_TREE;
     mb_internal_encoding('UTF-8');
     if ($code !== null) {
         // Create the specified locale
         self::$locale = Locale::create($code);
     } else {
         // Negotiate a locale, but if we can't then use a failsafe
         self::$locale = new LocaleEnUs();
         if (Session::has('locale')) {
             // Previously used
             self::$locale = Locale::create(Session::get('locale'));
         } else {
             // Browser negotiation
             $default_locale = new LocaleEnUs();
             try {
                 if ($WT_TREE) {
                     $default_locale = Locale::create($WT_TREE->getPreference('LANGUAGE'));
                 }
             } catch (\Exception $ex) {
             }
             self::$locale = Locale::httpAcceptLanguage($_SERVER, self::installedLocales(), $default_locale);
         }
     }
     $cache_dir_exists = File::mkdir(WT_DATA_DIR . 'cache');
     $cache_file = WT_DATA_DIR . 'cache/language-' . self::$locale->languageTag() . '-cache.php';
     if (file_exists($cache_file)) {
         $filemtime = filemtime($cache_file);
     } else {
         $filemtime = 0;
     }
     // Load the translation file(s)
     // Note that glob() returns false instead of an empty array when open_basedir_restriction
     // is in force and no files are found.  See PHP bug #47358.
     if (defined('GLOB_BRACE')) {
         $translation_files = array_merge(array(WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'), glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array(), glob(WT_DATA_DIR . 'language/' . self::$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
         $translation_files = array_merge(array(WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'), glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.csv') ?: array(), glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.php') ?: array(), glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.mo') ?: array(), glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.csv') ?: array(), glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.php') ?: array(), glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.mo') ?: array());
     }
     // Rebuild files after one hour
     $rebuild_cache = time() > $filemtime + 3600;
     // Rebuild files if any translation file has been updated
     foreach ($translation_files as $translation_file) {
         if (filemtime($translation_file) > $filemtime) {
             $rebuild_cache = true;
             break;
         }
     }
     if ($rebuild_cache) {
         $translations = array();
         foreach ($translation_files as $translation_file) {
             $translation = new Translation($translation_file);
             $translations = array_merge($translations, $translation->asArray());
         }
         if ($cache_dir_exists) {
             // During setup, we may not have been able to create it.
             file_put_contents($cache_file, '<' . '?php return ' . var_export($translations, true) . ';');
         }
     } else {
         $translations = (include $cache_file);
     }
     // Create a translator
     self::$translator = new Translator($translations, self::$locale->pluralRule());
     // Alphabetic sorting sequence (upper-case letters), used by webtrees to sort strings
     list(, self::$alphabet_upper) = explode('=', self::$translator->translate('ALPHABET_upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
     // Alphabetic sorting sequence (lower-case letters), used by webtrees to sort strings
     list(, self::$alphabet_lower) = explode('=', self::$translator->translate('ALPHABET_lower=abcdefghijklmnopqrstuvwxyz'));
     self::$list_separator = self::translate(', ');
     return self::$locale->languageTag();
 }
 /**
  * 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];
         }
     }
 }