Example #1
0
 /**
  * Initialise the translation adapter with a locale setting.
  *
  * @param string|null $locale If no locale specified, choose one automatically
  *
  * @return string $string
  */
 public static function init($locale = null)
 {
     global $WT_SESSION;
     // The translation libraries only work with a cache.
     $cache_options = array('automatic_serialization' => true, 'cache_id_prefix' => md5(WT_SERVER_NAME . WT_SCRIPT_PATH));
     if (ini_get('apc.enabled')) {
         self::$cache = Zend_Cache::factory('Core', 'Apc', $cache_options, array());
     } elseif (WT_File::mkdir(WT_DATA_DIR . 'cache')) {
         self::$cache = Zend_Cache::factory('Core', 'File', $cache_options, array('cache_dir' => WT_DATA_DIR . 'cache'));
     } else {
         self::$cache = Zend_Cache::factory('Core', 'Zend_Cache_Backend_BlackHole', $cache_options, array(), false, true);
     }
     Zend_Locale::setCache(self::$cache);
     Zend_Translate::setCache(self::$cache);
     $installed_languages = self::installed_languages();
     if (is_null($locale) || !array_key_exists($locale, $installed_languages)) {
         // Automatic locale selection.
         $locale = WT_Filter::get('lang');
         if ($locale && array_key_exists($locale, $installed_languages)) {
             // Requested in the URL?
             if (Auth::id()) {
                 Auth::user()->setSetting('language', $locale);
             }
         } elseif (array_key_exists($WT_SESSION->locale, $installed_languages)) {
             // Rembered from a previous visit?
             $locale = $WT_SESSION->locale;
         } else {
             // Browser preference takes priority over gedcom default
             if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
                 $prefs = explode(',', str_replace(' ', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
             } else {
                 $prefs = array();
             }
             if (WT_GED_ID) {
                 // Add the tree’s default language as a low-priority
                 $locale = get_gedcom_setting(WT_GED_ID, 'LANGUAGE');
                 $prefs[] = $locale . ';q=0.2';
             }
             $prefs2 = array();
             foreach ($prefs as $pref) {
                 list($l, $q) = explode(';q=', $pref . ';q=1.0');
                 $l = preg_replace_callback('/_[a-z][a-z]$/', function ($x) {
                     return strtoupper($x[0]);
                 }, str_replace('-', '_', $l));
                 // en-gb => en_GB
                 if (array_key_exists($l, $prefs2)) {
                     $prefs2[$l] = max((double) $q, $prefs2[$l]);
                 } else {
                     $prefs2[$l] = (double) $q;
                 }
             }
             // Ensure there is a fallback.
             if (!array_key_exists('en_US', $prefs2)) {
                 $prefs2['en_US'] = 0.01;
             }
             arsort($prefs2);
             foreach (array_keys($prefs2) as $pref) {
                 if (array_key_exists($pref, $installed_languages)) {
                     $locale = $pref;
                     break;
                 }
             }
         }
     }
     // Load the translation file
     self::$translation_adapter = new Zend_Translate('gettext', WT_ROOT . 'language/' . $locale . '.mo', $locale);
     // Deprecated - some custom modules use this to add translations
     Zend_Registry::set('Zend_Translate', self::$translation_adapter);
     // Load any local user translations
     if (is_dir(WT_DATA_DIR . 'language')) {
         if (file_exists(WT_DATA_DIR . 'language/' . $locale . '.mo')) {
             self::addTranslation(new Zend_Translate('gettext', WT_DATA_DIR . 'language/' . $locale . '.mo', $locale));
         }
         if (file_exists(WT_DATA_DIR . 'language/' . $locale . '.php')) {
             self::addTranslation(new Zend_Translate('array', WT_DATA_DIR . 'language/' . $locale . '.php', $locale));
         }
         if (file_exists(WT_DATA_DIR . 'language/' . $locale . '.csv')) {
             self::addTranslation(new Zend_Translate('csv', WT_DATA_DIR . 'language/' . $locale . '.csv', $locale));
         }
     }
     // Extract language settings from the translation file
     global $DATE_FORMAT;
     // I18N: This is the format string for full dates.  See http://php.net/date for codes
     $DATE_FORMAT = self::noop('%j %F %Y');
     global $TIME_FORMAT;
     // I18N: This is the format string for the time-of-day.  See http://php.net/date for codes
     $TIME_FORMAT = self::noop('%H:%i:%s');
     // Alphabetic sorting sequence (upper-case letters), used by webtrees to sort strings
     list(, self::$alphabet_upper) = explode('=', self::noop('ALPHABET_upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
     // Alphabetic sorting sequence (lower-case letters), used by webtrees to sort strings
     list(, self::$alphabet_lower) = explode('=', self::noop('ALPHABET_lower=abcdefghijklmnopqrstuvwxyz'));
     global $WEEK_START;
     // I18N: This is the first day of the week on calendars. 0=Sunday, 1=Monday...
     list(, $WEEK_START) = explode('=', self::noop('WEEK_START=0'));
     global $TEXT_DIRECTION;
     $TEXT_DIRECTION = self::scriptDirection(self::languageScript($locale));
     self::$locale = $locale;
     self::$dir = $TEXT_DIRECTION;
     // I18N: This punctuation is used to separate lists of items.
     self::$list_separator = self::translate(', ');
     // I18N: This is the name of the MySQL collation that applies to your language.  A list is available at http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html
     self::$collation = self::translate('utf8_unicode_ci');
     // Non-latin numbers may require non-latin digits
     try {
         self::$numbering_system = Zend_Locale_Data::getContent($locale, 'defaultnumberingsystem');
     } catch (Zend_Locale_Exception $ex) {
         // The latest CLDR database omits some languges such as Tatar (tt)
         self::$numbering_system = 'latin';
     }
     return $locale;
 }