Ticket #95: ticket_95.diff
| File ticket_95.diff, 13.4 kB (added by laurentj, 1 year ago) |
|---|
-
lib/jelix/core/jSelector.class.php
old new 13 13 * @contributor Loic Mathaud 14 14 * @contributor Rahal 15 15 * @contributor Thibault PIRONT < nuKs > 16 * @contributor Julien Issler 16 17 * @copyright 2005-2007 Laurent Jouanneau, 2007 Loic Mathaud, 2007 Rahal 17 18 * @copyright 2007 Thibault PIRONT 19 * @copyright 2007 Julien Issler 18 20 * @link http://www.jelix.org 19 21 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 20 22 */ … … 456 458 if(strpos($locale,'_') === false){ 457 459 $locale.='_'.strtoupper($locale); 458 460 } 459 $this->locale = $locale;460 461 $this->charset = $charset; 461 $this->_dirname = 'locales/' .$locale.'/'; 462 $this->_suffix = '.'.$charset.'.properties'; 463 $this->_cacheSuffix = '.'.$charset.'.php'; 462 $this->_setLocale($locale); 464 463 $this->_compilerPath=JELIX_LIB_CORE_PATH.'jLocalesCompiler.class.php'; 465 464 466 465 #if ENABLE_PHP_JELIX … … 486 485 } 487 486 } 488 487 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 489 496 protected function _createPath(){ 490 497 global $gJConfig; 491 498 if(!isset($gJConfig->_modulesPathList[$this->module])){ … … 503 510 $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.$this->resource.$this->_suffix; 504 511 505 512 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 } 507 529 } 508 530 $this->_where = 'modules/'; 509 531 } … … 725 747 return 'cForm_'.$this->module.'_Jx_'.$this->resource; 726 748 } 727 749 728 729 750 protected function _createPath(){ 730 751 global $gJConfig; 731 752 if(!isset($gJConfig->_modulesPathList[$this->module])){ -
lib/jelix/core/jLocale.class.php
old new 4 4 * @subpackage core 5 5 * @author Laurent Jouanneau 6 6 * @author Gerald Croes 7 * @contributor 7 * @contributor Julien Issler 8 8 * @copyright 2001-2005 CopixTeam, 2005-2007 Laurent Jouanneau 9 9 * Some parts of this file are took from Copix Framework v2.3dev20050901, CopixI18N.class.php, http://www.copix.org. 10 10 * copyrighted by CopixTeam and released under GNU Lesser General Public Licence. 11 11 * initial authors : Gerald Croes, Laurent Jouanneau. 12 12 * enhancement by Laurent Jouanneau for Jelix. 13 * @copyright 2007 Julien Issler 13 14 * @link http://www.jelix.org 14 15 * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 15 16 */ … … 108 109 */ 109 110 protected function _loadResources ($fichier, $charset){ 110 111 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)); 140 113 141 $this->_strings[$charset][$key] =$value; 114 $multiline=false; 115 $linenumber=0; 116 $key=''; 142 117 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 = ' '; 147 140 } 148 141 } 142 else{ 143 $value=''; 144 } 145 146 $this->_strings[$charset][$key] =$value; 147 149 148 } 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 } 153 152 } 153 154 154 } 155 155 } 156 156 … … 201 201 */ 202 202 static function get ($key, $args=null, $locale=null, $charset=null) { 203 203 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; 204 281 try{ 205 282 $file = new jSelectorLoc($key, $locale, $charset); 206 }catch(jExceptionSelector $e){ 283 return $file; 284 } 285 catch(jExceptionSelector $e){ 207 286 if($e->getCode() == 12) throw $e; 208 287 if ($locale === null) $locale = $gJConfig->locale; 209 288 if ($charset === null) $charset = $gJConfig->charset; 210 289 throw new Exception('(200)The given locale key "'.$key.'" is invalid (for charset '.$charset.', lang '.$locale.')'); 211 290 } 291 } 212 292 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; 215 301 if (!isset (self::$bundles[$keySelector][$locale])){ 216 self::$bundles[$keySelector][$locale] = new jBundle ($ file, $locale);302 self::$bundles[$keySelector][$locale] = new jBundle ($selector, $locale); 217 303 } 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]; 236 305 } 237 306 } 238 ?> 307 ?> -
lib/jelix/core/defaultconfig.ini.php
old new 7 7 charset = "UTF-8" 8 8 timeZone = "Europe/Paris" 9 9 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) 13 internalLocale = en_EN 14 10 15 checkTrustedModules = off 11 16 12 17 ; list of modules : module,module,module
