Ticket #95: ticket_95.diff

File ticket_95.diff, 13.4 kB (added by laurentj, 1 year ago)

The same patch updated for the latest trunk

  • lib/jelix/core/jSelector.class.php

    old new  
    1313* @contributor Loic Mathaud 
    1414* @contributor Rahal 
    1515* @contributor Thibault PIRONT < nuKs > 
     16* @contributor Julien Issler 
    1617* @copyright   2005-2007 Laurent Jouanneau, 2007 Loic Mathaud, 2007 Rahal 
    1718* @copyright   2007 Thibault PIRONT 
     19* @copyright   2007 Julien Issler 
    1820* @link        http://www.jelix.org 
    1921* @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
    2022*/ 
     
    456458        if(strpos($locale,'_') === false){ 
    457459            $locale.='_'.strtoupper($locale); 
    458460        } 
    459         $this->locale = $locale; 
    460461        $this->charset = $charset; 
    461         $this->_dirname =  'locales/' .$locale.'/'; 
    462         $this->_suffix = '.'.$charset.'.properties'; 
    463         $this->_cacheSuffix = '.'.$charset.'.php'; 
     462        $this->_setLocale($locale); 
    464463        $this->_compilerPath=JELIX_LIB_CORE_PATH.'jLocalesCompiler.class.php'; 
    465464 
    466465#if ENABLE_PHP_JELIX 
     
    486485        } 
    487486    } 
    488487 
     488 
     489    protected function _setLocale($locale){ 
     490        $this->locale = $locale; 
     491        $this->_dirname =  'locales/' .$locale.'/'; 
     492        $this->_suffix = '.'.$this->charset.'.properties'; 
     493        $this->_cacheSuffix = '.'.$locale.'.'.$this->charset.'.php'; // added $locale in the cache filename 
     494    } 
     495 
    489496    protected function _createPath(){ 
    490497        global $gJConfig; 
    491498        if(!isset($gJConfig->_modulesPathList[$this->module])){ 
     
    503510        $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; 
    504511 
    505512        if (!is_readable ($this->_path)){ 
    506             throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), "locale")); 
     513             
     514            if(!jLocale::isGenericLocale($this->locale)){ 
     515                $this->_setLocale(jLocale::getGenericLocaleForLocale($this->locale)); 
     516                $this->_createPath(); 
     517            } 
     518            else if($this->locale !== jLocale::getGenericLocaleForLocale($gJConfig->locale)){ 
     519                $this->_setLocale($gJConfig->locale); 
     520                $this->_createPath(); 
     521            } 
     522            else if($this->locale !== $gJConfig->internalLocale){ 
     523                $this->_setLocale($gJConfig->internalLocale); 
     524                $this->_createPath(); 
     525            } 
     526            else{ 
     527                throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), "locale")); 
     528            } 
    507529        } 
    508530        $this->_where = 'modules/'; 
    509531    } 
     
    725747        return 'cForm_'.$this->module.'_Jx_'.$this->resource; 
    726748    } 
    727749 
    728  
    729750    protected function _createPath(){ 
    730751        global $gJConfig; 
    731752        if(!isset($gJConfig->_modulesPathList[$this->module])){ 
  • lib/jelix/core/jLocale.class.php

    old new  
    44* @subpackage core 
    55* @author     Laurent Jouanneau 
    66* @author     Gerald Croes 
    7 * @contributor 
     7* @contributor Julien Issler 
    88* @copyright  2001-2005 CopixTeam, 2005-2007 Laurent Jouanneau 
    99* Some parts of this file are took from Copix Framework v2.3dev20050901, CopixI18N.class.php, http://www.copix.org. 
    1010* copyrighted by CopixTeam and released under GNU Lesser General Public Licence. 
    1111* initial authors : Gerald Croes, Laurent Jouanneau. 
    1212* enhancement by Laurent Jouanneau for Jelix. 
     13* @copyright   2007 Julien Issler 
    1314* @link        http://www.jelix.org 
    1415* @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
    1516*/ 
     
    108109    */ 
    109110    protected function _loadResources ($fichier, $charset){ 
    110111 
    111         if (($f = @fopen ($fichier, 'r')) !== false) { 
    112             $multiline=false; 
    113             $linenumber=0; 
    114             $key=''; 
    115             while (!feof($f)) { 
    116                 if($line=fgets($f)){ 
    117                     $linenumber++; 
    118                     $line=rtrim($line); 
    119                     if($multiline){ 
    120                         if(preg_match("/^\s*(.*)\s*(\\\\?)$/U", $line, $match)){ 
    121                             $sp = preg_split('/(?<!\\\\)\#/', $match[1], -1 ,PREG_SPLIT_NO_EMPTY); 
    122                             $multiline= ($match[2] =="\\"); 
    123                             $this->_strings[$charset][$key].=' '.trim(str_replace('\#','#',$sp[0])); 
    124                         }else{ 
    125                             throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber,210); 
    126                         } 
    127                     }elseif(preg_match("/^\s*(.+)\s*=\s*(.*)\s*(\\\\?)$/U",$line, $match)){ 
    128                         // on a bien un cle=valeur 
    129                         $key=$match[1]; 
    130                         $multiline= ($match[3] =="\\"); 
    131                         $sp = preg_split('/(?<!\\\\)\#/', $match[2], -1 ,PREG_SPLIT_NO_EMPTY); 
    132                         if(count($sp)){ 
    133                             $value=trim(str_replace('\#','#',$sp[0])); 
    134                             if($value == '\w'){ 
    135                                 $value = ' '; 
    136                             } 
    137                         }else{ 
    138                             $value=''; 
    139                         } 
     112        $resource = explode("\n",file_get_contents($fichier)); 
    140113 
    141                         $this->_strings[$charset][$key] =$value; 
     114        $multiline=false; 
     115        $linenumber=0; 
     116        $key=''; 
    142117 
    143                     }elseif(preg_match("/^\s*(\#.*)?$/",$line, $match)){ 
    144                         // ok, juste un commentaire 
    145                     }else { 
    146                         throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber,211); 
     118        foreach($resource as $line){ 
     119            $linenumber++; 
     120            $line=rtrim($line); 
     121            if($multiline){ 
     122                if(preg_match("/^\s*(.*)\s*(\\\\?)$/U", $line, $match)){ 
     123                    $sp = preg_split('/(?<!\\\\)\#/', $match[1], -1 ,PREG_SPLIT_NO_EMPTY); 
     124                    $multiline= ($match[2] =="\\"); 
     125                    $this->_strings[$charset][$key].=' '.trim(str_replace('\#','#',$sp[0])); 
     126                } 
     127                else{ 
     128                    throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber,210); 
     129                } 
     130            } 
     131            else if(preg_match("/^\s*(.+)\s*=\s*(.*)\s*(\\\\?)$/U",$line, $match)){ 
     132                // on a bien un cle=valeur 
     133                $key=$match[1]; 
     134                $multiline= ($match[3] =="\\"); 
     135                $sp = preg_split('/(?<!\\\\)\#/', $match[2], -1 ,PREG_SPLIT_NO_EMPTY); 
     136                if(count($sp)){ 
     137                    $value=trim(str_replace('\#','#',$sp[0])); 
     138                    if($value == '\w'){ 
     139                        $value = ' '; 
    147140                    } 
    148141                } 
     142                else{ 
     143                    $value=''; 
     144                } 
     145 
     146                $this->_strings[$charset][$key] =$value; 
     147 
    149148            } 
    150             fclose ($f); 
    151         }else{ 
    152             throw new Exception('Cannot load the resource '.$fichier,212); 
     149            else if(!preg_match("/^\s*(\#.*)?$/",$line, $match)){ 
     150                throw new Exception('Syntaxe error in file properties '.$fichier.' line '.$linenumber,211); 
     151            } 
    153152        } 
     153 
    154154    } 
    155155} 
    156156 
     
    201201    */ 
    202202    static function get ($key, $args=null, $locale=null, $charset=null) { 
    203203        global $gJConfig; 
     204 
     205        $selector = self::_getSelector($key, $locale, $charset); 
     206        $bundle = self::_getBundle($selector); 
     207        $string = $bundle->get($selector->messageKey, $selector->charset); 
     208 
     209        if(is_null($string)){  
     210            if(self::isGenericLocale($locale)){ 
     211                if($selector->locale === $gJConfig->locale){ 
     212                    throw new Exception('(210)The given locale key "'.$selector->toString().'" does not exists in the default lang ('.$gJConfig->locale.') for the '.$selector->charset.' charset '); 
     213                } 
     214                else{ 
     215                    $string = self::getStringFromDefaultLocale($key); 
     216                } 
     217            } 
     218            else{ 
     219                $generic_locale = self::getGenericLocaleForLocale($selector->locale); 
     220                $string = self::get($key, $args, $generic_locale, $selector->charset); 
     221            } 
     222        } 
     223 
     224        if(is_null($args)) return $string; 
     225 
     226        return vsprintf($string, is_array ($args) ? $args : array ($args)); 
     227    } 
     228 
     229    /** 
     230     * Gets the generic locale for the given locale (en_US -> en_EN, fr_CA -> fr_FR, ...) 
     231     * @param string $locale The locale to get the generic locale from 
     232     * @return string The generic locale 
     233     */ 
     234    static public function getGenericLocaleForLocale($locale){ 
     235        global $gJConfig; 
     236        if ($locale === null)  $locale = $gJConfig->locale; 
     237        return substr($locale,0,2).'_'.strtoupper(substr($locale,0,2)); 
     238    } 
     239 
     240    /** 
     241     * Checks if the given locale is "generic" (en_EN, fr_FR, xx_XX, ...) 
     242     * @param string $locale The locale to check 
     243     * @return boolean 
     244     */ 
     245    static public function isGenericLocale($locale){ 
     246        global $gJConfig; 
     247        if ($locale === null)  $locale = $gJConfig->locale; 
     248        return $locale === substr($locale,0,2).'_'.strtoupper(substr($locale,0,2)); 
     249    } 
     250 
     251    /** 
     252     * Gets the string from the default locale with default charset 
     253     * @param string $key The selector for the string 
     254     * @return string The string 
     255     * @throws Exception An exception if failure 
     256     */ 
     257    static public function getStringFromDefaultLocale($key){ 
     258        global $gJConfig; 
     259        $selector = self::_getSelector($key, $gJConfig->locale, $gJConfig->charset);         
     260        $bundle = self::_getBundle($selector); 
     261 
     262        $string = $bundle->get($selector->messageKey, $selector->charset); 
     263 
     264        if(is_null($string)){ 
     265            throw new Exception('(210)The given locale key "'.$selector->toString().'" does not exists in the default lang ('.$gJConfig->locale.') for the '.$selector->charset.' charset '); 
     266        } 
     267 
     268        return $string; 
     269    } 
     270 
     271    /** 
     272     * Gets the jSelectorLoc object for the bundle 
     273     * @param string $key The selector string 
     274     * @param string $locale The wanted locale 
     275     * @param string $charset The wanted charset 
     276     * @return jSelectorLoc The jSelectorLoc object for the bundle 
     277     * @throws jExceptionSelector An exception if failure 
     278     */ 
     279    static protected function _getSelector($key, $locale, $charset){ 
     280        global $gJConfig; 
    204281        try{ 
    205282            $file = new jSelectorLoc($key, $locale, $charset); 
    206         }catch(jExceptionSelector $e){ 
     283            return $file; 
     284        } 
     285        catch(jExceptionSelector $e){ 
    207286            if($e->getCode() == 12) throw $e; 
    208287            if ($locale === null)  $locale = $gJConfig->locale; 
    209288            if ($charset === null) $charset = $gJConfig->charset; 
    210289            throw new Exception('(200)The given locale key "'.$key.'" is invalid  (for charset '.$charset.', lang '.$locale.')'); 
    211290        } 
     291    } 
    212292 
    213         $locale = $file->locale; 
    214         $keySelector = $file->module.'~'.$file->fileKey; 
     293    /** 
     294     * Gets the bundle for the given jSelectorLoc 
     295     * @param jSelectorLoc The selector for the bundle 
     296     * @return jBundle The bundle for the given jSelectorLoc 
     297     */ 
     298    static protected function _getBundle($selector){ 
     299        $locale = $selector->locale; 
     300        $keySelector = $selector->module.'~'.$selector->fileKey; 
    215301        if (!isset (self::$bundles[$keySelector][$locale])){ 
    216             self::$bundles[$keySelector][$locale] =  new jBundle ($file, $locale); 
     302            self::$bundles[$keySelector][$locale] =  new jBundle ($selector, $locale); 
    217303        } 
    218         $bundle = self::$bundles[$keySelector][$locale]; 
    219  
    220         //try to get the message from the bundle. 
    221         $string = $bundle->get ($file->messageKey, $file->charset); 
    222         if ($string === null){ 
    223             //if the message was not found, we're gonna 
    224             //use the default language and country. 
    225             if ($locale    == $gJConfig->locale){ 
    226                 throw new Exception('(210)The given locale key "'.$file->toString().'" does not exists in the default lang for the '.$file->charset.' charset '); 
    227             } 
    228             return jLocale::get ($key, $args, $gJConfig->locale); 
    229         }else{ 
    230             //here, we know the message 
    231             if ($args!==null){ 
    232                 $string = call_user_func_array('sprintf', array_merge (array ($string), is_array ($args) ? $args : array ($args))); 
    233             } 
    234             return $string; 
    235         } 
     304        return self::$bundles[$keySelector][$locale]; 
    236305    } 
    237306} 
    238 ?> 
     307?> 
  • lib/jelix/core/defaultconfig.ini.php

    old new  
    77charset = "UTF-8" 
    88timeZone = "Europe/Paris" 
    99 
     10; internal locale, for jelix localized errors, when no locale can be found at all  
     11; (specific (de_AT) > generic (de_DE) > default-specific (es_AR) > default generic (es_ES)).  
     12; Must be en_EN of fr_FR (one the the native jelix generic locales) 
     13internalLocale = en_EN 
     14 
    1015checkTrustedModules = off 
    1116 
    1217; list of modules : module,module,module 
Download in other formats: Original Format