Source for file jLocale.class.php

Documentation is available at jLocale.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage core
  5. @author     Laurent Jouanneau
  6. @author     Gerald Croes
  7. @contributor Julien Issler, Yannick Le Guédart
  8. @copyright  2001-2005 CopixTeam, 2005-2009 Laurent Jouanneau
  9. *  Some parts of this file are took from Copix Framework v2.3dev20050901, CopixI18N.class.php, http://www.copix.org.
  10. *  copyrighted by CopixTeam and released under GNU Lesser General Public Licence.
  11. *  initial authors : Gerald Croes, Laurent Jouanneau.
  12. *  enhancement by Laurent Jouanneau for Jelix.
  13. @copyright 2008 Julien Issler, 2008 Yannick Le Guédart
  14. @link        http://www.jelix.org
  15. @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  16. */
  17.  
  18. /**
  19.  * static class to get a localized string
  20.  * @package  jelix
  21.  * @subpackage core
  22.  */
  23. class jLocale {
  24.     /**
  25.      *
  26.      */
  27.     static $bundles array();
  28.  
  29.     /**
  30.      * static class...
  31.      */
  32.     private function __construct(){}
  33.  
  34.     /**
  35.      * gets the current lang
  36.      * @return string 
  37.      */
  38.     static function getCurrentLang(){
  39.         $s=$GLOBALS['gJConfig']->locale;
  40.         return substr($s,0strpos($s,'_'));
  41.     }
  42.     /**
  43.      * gets the current country.
  44.      * @return string 
  45.      */
  46.     static function getCurrentCountry (){
  47.         $s=$GLOBALS['gJConfig']->locale;
  48.         return substr($s,strpos($s,'_')+1);
  49.     }
  50.  
  51.     /**
  52.     * gets the correct string, for a given language.
  53.     *   if it can't get the correct language, it will try to gets the string
  54.     *   from the default language.
  55.     *   if both fails, it will raise an exception.
  56.     * @param string $key the key of the localized string
  57.     * @param array $args arguments to apply to the localized string with sprintf
  58.     * @param string $locale  the lang code. if null, use the default language
  59.     * @param string $charset the charset code. if null, use the default charset
  60.     * @return string the localized string
  61.     */
  62.     static function get ($key$args=null$locale=null$charset=null{
  63.         global $gJConfig;
  64.         try {
  65.             $file new jSelectorLoc($key$locale$charset);
  66.         }
  67.         catch (jExceptionSelector $e{
  68.             // the file is not found
  69.             if ($e->getCode(== 12throw $e;
  70.             if ($locale === null)  $locale $gJConfig->locale;
  71.             if ($charset === null$charset $gJConfig->charset;
  72.             if ($locale != $gJConfig->fallbackLocale && $gJConfig->fallbackLocale{
  73.                 return jLocale::get ($key$args$gJConfig->fallbackLocale$charset);
  74.             }
  75.             else
  76.                 throw new Exception('(200)The given locale key "'.$key
  77.                                 .'" is invalid (for charset '.$charset
  78.                                 .', lang '.$locale.')');
  79.         }
  80.  
  81.         $locale $file->locale;
  82.         $keySelector $file->module.'~'.$file->fileKey;
  83.  
  84.         if (!isset (self::$bundles[$keySelector][$locale])) {
  85.             self::$bundles[$keySelector][$locale=  new jBundle ($file$locale);
  86.         }
  87.  
  88.         $bundle self::$bundles[$keySelector][$locale];
  89.  
  90.         //try to get the message from the bundle.
  91.         $string $bundle->get ($file->messageKey$file->charset);
  92.         if ($string === null{
  93.             if ($locale == $gJConfig->fallbackLocale{
  94.                 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');
  95.             }
  96.             // if the message was not found, we're gonna
  97.             //use the default language and country.
  98.             else if ($locale == $gJConfig->locale{
  99.                 if ($gJConfig->fallbackLocale)
  100.                     return jLocale::get ($key$args$gJConfig->fallbackLocale$charset);
  101.                 throw new Exception('(210)The given locale key "'.$file->toString().'" does not exists in the default lang for the '.$file->charset.' charset');
  102.             }
  103.             return jLocale::get ($key$args$gJConfig->locale);
  104.         }
  105.         else {
  106.             //here, we know the message
  107.             if ($args !== null && $args !== array()) {
  108.                 $string call_user_func_array('sprintf'array_merge (array ($string)is_array ($args$args array ($args)));
  109.             }
  110.             return $string;
  111.         }
  112.     }
  113. }

Documentation generated on Mon, 19 Sep 2011 14:13:14 +0200 by phpDocumentor 1.4.3