Пример #1
0
 /**
  * gets the correct string, for a given language.
  *   if it can't get the correct language, it will try to gets the string
  *   from the default language.
  *   if both fails, it will raise an exception.
  * @param string $key the key of the localized string
  * @param array $args arguments to apply to the localized string with sprintf
  * @param string $locale  the lang code. if null, use the default language
  * @param string $charset the charset code. if null, use the default charset
  * @param boolean $tryOtherLocales if true and if the method does not find
  *                   the locale file or the key, it will try with the default
  *                   locale, the fallback local or similar locale
  * @return string the localized string
  */
 static function get($key, $args = null, $locale = null, $charset = null, $tryOtherLocales = true)
 {
     $config = App::config();
     try {
         $file = new LocaleSelector($key, $locale, $charset);
     } catch (\Jelix\Core\Selector\Exception $e) {
         // the file is not found
         if ($e->getCode() == 12) {
             // unknown module..
             throw $e;
         }
         if ($locale === null) {
             $locale = $config->locale;
         }
         if ($charset === null) {
             $charset = $config->charset;
         }
         if (!$tryOtherLocales) {
             throw new Exception('(211)No locale file found for the given locale key "' . $key . '" (charset ' . $charset . ', lang ' . $locale . ')');
         }
         $words = self::tryOtherLocales($key, $args, $locale, $charset, $config);
         if ($words === null) {
             throw new Exception('(212)No locale file found for the given locale key "' . $key . '" in any other default languages (charset ' . $charset . ')');
         }
         return $words;
     }
     $locale = $file->locale;
     $keySelector = $file->module . '~' . $file->fileKey;
     if (!isset(self::$bundles[$keySelector][$locale])) {
         self::$bundles[$keySelector][$locale] = new Bundle($file, $locale);
     }
     $bundle = self::$bundles[$keySelector][$locale];
     //try to get the message from the bundle.
     $string = $bundle->get($file->messageKey, $file->charset);
     if ($string === null) {
         // locale key has not been found
         if (!$tryOtherLocales) {
             throw new Exception('(210)The given locale key "' . $file->toString() . '" does not exists (lang:' . $file->locale . ', charset:' . $file->charset . ')');
         }
         $words = self::tryOtherLocales($key, $args, $locale, $charset, $config);
         if ($words === null) {
             throw new Exception('(213)The given locale key "' . $file->toString() . '" does not exists in any default languages for the ' . $file->charset . ' charset');
         }
         return $words;
     } else {
         //here, we know the message
         if ($args !== null && $args !== array()) {
             $string = call_user_func_array('sprintf', array_merge(array($string), is_array($args) ? $args : array($args)));
         }
         return $string;
     }
 }
Пример #2
0
 /**
  * gets the correct string, for a given language.
  *   if it can't get the correct language, it will try to gets the string
  *   from the default language.
  *   if both fails, it will raise an exception.
  * @param string $key the key of the localized string
  * @param array $args arguments to apply to the localized string with sprintf
  * @param string $locale  the lang code. if null, use the default language
  * @param string $charset the charset code. if null, use the default charset
  * @return string the localized string
  */
 static function get($key, $args = null, $locale = null, $charset = null)
 {
     $config = App::config();
     try {
         $file = new LocaleSelector($key, $locale, $charset);
     } catch (\Jelix\Core\Selector\Exception $e) {
         // the file is not found
         if ($e->getCode() == 12) {
             throw $e;
         }
         if ($locale === null) {
             $locale = $config->locale;
         }
         if ($charset === null) {
             $charset = $config->charset;
         }
         if ($locale != $config->fallbackLocale && $config->fallbackLocale) {
             return self::get($key, $args, $config->fallbackLocale, $charset);
         } else {
             throw new Exception('(200)The given locale key "' . $key . '" is invalid (for charset ' . $charset . ', lang ' . $locale . ')');
         }
     }
     $locale = $file->locale;
     $keySelector = $file->module . '~' . $file->fileKey;
     if (!isset(self::$bundles[$keySelector][$locale])) {
         self::$bundles[$keySelector][$locale] = new Bundle($file, $locale);
     }
     $bundle = self::$bundles[$keySelector][$locale];
     //try to get the message from the bundle.
     $string = $bundle->get($file->messageKey, $file->charset);
     if ($string === null) {
         if ($locale == $config->fallbackLocale) {
             throw new Exception('(210)The given locale key "' . $file->toString() . '" does not exists in the default lang and in the fallback lang for the ' . $file->charset . ' charset');
         } else {
             if ($locale == $config->locale) {
                 if ($config->fallbackLocale) {
                     return self::get($key, $args, $config->fallbackLocale, $charset);
                 }
                 throw new Exception('(210)The given locale key "' . $file->toString() . '" does not exists in the default lang for the ' . $file->charset . ' charset');
             }
         }
         return self::get($key, $args, $config->locale);
     } else {
         //here, we know the message
         if ($args !== null && $args !== array()) {
             $string = call_user_func_array('sprintf', array_merge(array($string), is_array($args) ? $args : array($args)));
         }
         return $string;
     }
 }