Changeset 4576
- Timestamp:
- 07/11/07 14:46:02 (1 year ago)
- Files:
-
- trunk/lib/addon/creole/i18n/sfMessageSource_Creole.class.php (modified) (3 diffs)
- trunk/lib/i18n/extract/sfI18nExtract.class.php (modified) (4 diffs)
- trunk/lib/i18n/sfI18N.class.php (modified) (11 diffs)
- trunk/lib/i18n/sfMessageFormat.class.php (modified) (4 diffs)
- trunk/lib/i18n/sfMessageSource.class.php (modified) (5 diffs)
- trunk/lib/i18n/sfMessageSource_Aggregate.class.php (added)
- trunk/lib/i18n/sfMessageSource_Database.class.php (modified) (1 diff)
- trunk/lib/i18n/sfMessageSource_File.class.php (modified) (4 diffs)
- trunk/lib/i18n/sfMessageSource_MySQL.class.php (modified) (3 diffs)
- trunk/lib/i18n/sfMessageSource_SQLite.class.php (modified) (10 diffs)
- trunk/lib/i18n/sfMessageSource_XLIFF.class.php (modified) (2 diffs)
- trunk/lib/i18n/sfMessageSource_gettext.class.php (modified) (1 diff)
- trunk/test/unit/i18n/fixtures (added)
- trunk/test/unit/i18n/fixtures/messages.fr.xml (added)
- trunk/test/unit/i18n/fixtures/messages_bis.fr.xml (added)
- trunk/test/unit/i18n/sfMessageSourceTest.php (added)
- trunk/test/unit/i18n/sfMessageSource_AggregateTest.php (added)
- trunk/test/unit/i18n/sfMessageSource_FileTest.php (added)
- trunk/test/unit/i18n/sfMessageSource_SQLiteTest.php (added)
- trunk/test/unit/i18n/sfMessageSource_XLIFFTest.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/addon/creole/i18n/sfMessageSource_Creole.class.php
r4343 r4576 101 101 * @return array translation messages. 102 102 */ 103 p rotectedfunction &loadData($variant)103 public function &loadData($variant) 104 104 { 105 105 $sql = 'SELECT t.source, t.target, t.comments '. … … 152 152 * @return boolean true if the catalogue+variant is in the database, false otherwise. 153 153 */ 154 p rotectedfunction isValidSource($variant)154 public function isValidSource($variant) 155 155 { 156 156 $sql = 'SELECT COUNT(*) FROM catalogue WHERE name = ?'; … … 163 163 164 164 return $result; 165 }166 167 /**168 * Get all the variants of a particular catalogue.169 *170 * @param string catalogue name171 * @return array list of all variants for this catalogue.172 */173 protected function getCatalogueList($catalogue)174 {175 $variants = explode('_', $this->culture);176 177 $catalogues = array($catalogue);178 179 $variant = null;180 181 for ($i = 0, $max = count($variants); $i < $max; $i++)182 {183 if (strlen($variants[$i]) > 0)184 {185 $variant .= ($variant) ? '_'.$variants[$i] : $variants[$i];186 $catalogues[] = $catalogue.'.'.$variant;187 }188 }189 190 return array_reverse($catalogues);191 165 } 192 166 trunk/lib/i18n/extract/sfI18nExtract.class.php
r4370 r4576 75 75 public function saveNewMessages() 76 76 { 77 $messageSource = $this->i18n->get LastMessageSource();77 $messageSource = $this->i18n->getMessageSource(); 78 78 foreach ($this->getNewMessages() as $message) 79 79 { … … 92 92 public function deleteOldMessages() 93 93 { 94 $messageSource = $this->i18n->get LastMessageSource();94 $messageSource = $this->i18n->getMessageSource(); 95 95 foreach ($this->getOldMessages() as $message) 96 96 { … … 147 147 protected function loadMessageSources() 148 148 { 149 foreach ($this->i18n->getMessageSources() as $messageSource) 150 { 151 $messageSource->setCulture($this->culture); 152 $messageSource->load(); 153 } 149 $this->i18n->getMessageSource()->setCulture($this->culture); 150 $this->i18n->getMessageSource()->load(); 154 151 } 155 152 … … 160 157 { 161 158 $this->currentMessages = array(); 162 foreach ($this->i18n->getMessageSource s() as $messageSource)159 foreach ($this->i18n->getMessageSource()->read() as $catalogue => $translations) 163 160 { 164 foreach ($ messageSource->read() as $catalogue => $translations)161 foreach ($translations as $key => $values) 165 162 { 166 foreach ($translations as $key => $values) 167 { 168 $this->currentMessages[] = $key; 169 } 163 $this->currentMessages[] = $key; 170 164 } 171 165 } trunk/lib/i18n/sfI18N.class.php
r4350 r4576 20 20 { 21 21 protected 22 $context = null,23 $culture = null,24 $messageSource s = array(),25 $messageFormat s = array();22 $context = null, 23 $culture = null, 24 $messageSource = null, 25 $messageFormat = null; 26 26 27 27 /** … … 33 33 { 34 34 $this->context = $context; 35 36 $this->setMessageSource(sfLoader::getI18NGlobalDirs(), $context->getUser()->getCulture());37 35 } 38 36 … … 40 38 * Sets the message sources. 41 39 * 42 * @param mixed An array of i18n directories if message source configurationis XLIFF or gettext, null otherwise40 * @param mixed An array of i18n directories if message source is XLIFF or gettext, null otherwise 43 41 * @param string The culture 44 42 */ 45 43 public function setMessageSource($dirs, $culture) 46 44 { 47 $this->messageSources = array();48 $this->messageFormats = array();49 50 45 if (is_null($dirs)) 51 46 { 52 $this->messageSource s= $this->createMessageSource();47 $this->messageSource = $this->createMessageSource(); 53 48 } 54 49 else 55 50 { 56 foreach ($dirs as $dir) 57 { 58 $this->messageSources[] = $this->createMessageSource($dir); 59 } 51 $this->messageSource = sfMessageSource::factory('Aggregate', array_map(array($this, 'createMessageSource'), $dirs)); 60 52 } 61 53 62 54 $this->setCulture($culture); 55 $this->messageFormat = null; 63 56 } 64 57 … … 67 60 * 68 61 * @param mixed An array of i18n directories to create a XLIFF or gettext message source, null otherwise 62 * 69 63 * @return sfMessageSource A sfMessageSource object 70 64 */ … … 98 92 99 93 /** 100 * Returns a new message format for the given message source.101 *102 * @param sfIMessageSource A sfMessageSource object103 * @return sfMessageFormat A sfMessageFormat object104 */105 public function createMessageFormat(sfIMessageSource $source)106 {107 $messageFormat = new sfMessageFormat($source, sfConfig::get('sf_charset'));108 109 if (sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug'))110 {111 $messageFormat->setUntranslatedPS(array(sfConfig::get('sf_i18n_untranslated_prefix'), sfConfig::get('sf_i18n_untranslated_suffix')));112 }113 114 return $messageFormat;115 }116 117 /**118 94 * Sets the current culture for i18n format objects. 119 95 * … … 122 98 public function setCulture($culture) 123 99 { 124 if ($ culture != $this->culture)100 if ($this->messageSource) 125 101 { 126 102 $this->culture = $culture; 127 128 $this->messageFormats = array(); 129 } 130 131 foreach ($this->messageSources as $messageSource) 132 { 133 $messageSource->setCulture($culture); 134 } 135 } 136 137 /** 138 * Gets all current message sources. 139 * 140 * @return array An array of sfMessageSource objects 141 */ 142 public function getMessageSources() 143 { 144 return $this->messageSources; 145 } 146 147 /** 148 * Gets the message source for the given index. 149 * 150 * @param integer The indice (1 based) 103 $this->messageSource->setCulture($culture); 104 } 105 } 106 107 /** 108 * Gets the message source. 109 * 151 110 * @return sfMessageSource A sfMessageSource object 152 111 */ 153 public function getMessageSource($i = 1) 154 { 155 if (!isset($this->messageSources[$i - 1])) 156 { 157 throw new sfException(sprintf('The "$i" message source does not exist.', $i)); 158 } 159 160 return $this->messageSources[$i - 1]; 161 } 162 163 /** 164 * Gets the last message source. 165 */ 166 public function getLastMessageSource() 167 { 168 return $this->getMessageSource(count($this->messageSources)); 169 } 170 171 /** 172 * Gets all current message formats. 173 * 174 * @return array An array of sfMessageFormat objects 175 */ 176 public function getMessageFormats() 177 { 178 for ($i = 0, $count = count($this->messageSources); $i < $count; $i++) 179 { 180 $this->getMessageFormat($i); 181 } 182 183 return $this->messageFormats; 184 } 185 186 /** 187 * Gets the message format for the given index. 188 * 189 * @param integer The indice (1 based) 112 public function getMessageSource() 113 { 114 if (!isset($this->messageSource)) 115 { 116 $this->setMessageSource(sfLoader::getI18NGlobalDirs(), $this->context->getUser()->getCulture()); 117 } 118 119 return $this->messageSource; 120 } 121 122 /** 123 * Gets the message format. 124 * 190 125 * @return sfMessageFormat A sfMessageFormat object 191 126 */ 192 public function getMessageFormat($i = 1) 193 { 194 if (!isset($this->messageFormats[$i - 1])) 195 { 196 $this->messageFormats[$i - 1] = $this->createMessageFormat($this->getMessageSource($i)); 197 } 198 199 return $this->messageFormats[$i - 1]; 200 } 201 202 /** 203 * Gets the last message format. 204 */ 205 public function getLastMessageFormat() 206 { 207 return $this->getMessageFormat(count($this->messageSources)); 127 public function getMessageFormat() 128 { 129 if (!isset($this->messageFormat)) 130 { 131 $this->messageFormat = new sfMessageFormat($this->getMessageSource(), sfConfig::get('sf_charset')); 132 133 if (sfConfig::get('sf_debug') && sfConfig::get('sf_i18n_debug')) 134 { 135 $this->messageFormat->setUntranslatedPS(array(sfConfig::get('sf_i18n_untranslated_prefix'), sfConfig::get('sf_i18n_untranslated_suffix'))); 136 } 137 } 138 139 return $this->messageFormat; 208 140 } 209 141 … … 214 146 * @param array An array of arguments for the translation 215 147 * @param string The catalogue name 148 * 216 149 * @return string The translated string 217 150 */ 218 151 public function __($string, $args = array(), $catalogue = 'messages') 219 152 { 220 for ($i = 0, $count = count($this->messageSources); $i < $count; $i++) 221 { 222 if ($retval = $this->getMessageFormat($i + 1)->formatExists($string, $args, $catalogue)) 223 { 224 return $retval; 225 } 226 } 227 228 return $this->getLastMessageFormat()->format($string, $args, $catalogue); 153 return $this->getMessageFormat()->format($string, $args, $catalogue); 229 154 } 230 155 … … 234 159 * @param string The ISO code 235 160 * @param string The culture 161 * 236 162 * @return string The country name 237 163 */ … … 248 174 * 249 175 * @param string The culture 176 * 250 177 * @return string The culture name 251 178 */ … … 262 189 * @param string The formatted date as string 263 190 * @param string The culture 191 * 264 192 * @return integer The timestamp 265 193 */ … … 276 204 * @param string The formatted date as string 277 205 * @param string The culture 206 * 278 207 * @return array An array with the day, month and year 279 208 */ trunk/lib/i18n/sfMessageFormat.class.php
r4343 r4576 79 79 * @var string 80 80 */ 81 public $ Catalogue;81 public $catalogue; 82 82 83 83 /** … … 168 168 } 169 169 170 public function formatExists($string, $args = array(), $catalogue = null, $charset = null)171 {172 if (empty($charset))173 {174 $charset = $this->getCharset();175 }176 177 $s = $this->getFormattedString(sfToolkit::I18N_toUTF8($string, $charset), $args, $catalogue);178 179 return sfToolkit::I18N_toEncoding($s, $charset);180 }181 182 170 /** 183 171 * Do string translation. … … 190 178 protected function formatString($string, $args = array(), $catalogue = null) 191 179 { 180 if (empty($catalogue)) 181 { 182 $catalogue = empty($this->catalogue) ? 'messages' : $this->catalogue; 183 } 184 185 $this->loadCatalogue($catalogue); 186 192 187 if (empty($args)) 193 188 { 194 189 $args = array(); 195 190 } 196 197 $target = $this->getFormattedString($string, $args, $catalogue);198 199 // well we did not find the translation string.200 if (!$target)201 {202 $this->source->append($string);203 $target = $this->postscript[0].$this->replaceArgs($string, $args).$this->postscript[1];204 }205 206 return $target;207 }208 209 protected function getFormattedString($string, $args = array(), $catalogue = null)210 {211 if (empty($catalogue))212 {213 $catalogue = empty($this->catalogue) ? 'messages' : $this->catalogue;214 }215 216 if (empty($args))217 {218 $args = array();219 }220 221 $this->loadCatalogue($catalogue);222 191 223 192 foreach ($this->messages[$catalogue] as $variant) … … 245 214 } 246 215 247 return null; 216 // well we did not find the translation string. 217 $this->source->append($string); 218 219 return $this->postscript[0].$this->replaceArgs($string, $args).$this->postscript[1]; 248 220 } 249 221 trunk/lib/i18n/sfMessageSource.class.php
r4341 r4576 101 101 * Factory method to instantiate a new sfMessageSource depending on the 102 102 * source type. The built-in source types are 'XLIFF', 'SQLite', 103 * 'MySQL', 'gettext' and Creole. The source parameter is dependent on the 104 * source type. For 'gettext' and 'XLIFF', it should point to the directory 103 * 'MySQL', 'gettext', 'Creole' and 'Aggregate'. 104 * The source parameter is dependent on the source type. 105 * For 'gettext' and 'XLIFF', it should point to the directory 105 106 * where the messages are stored. For database types, e.g. 'SQLite' and 106 107 * 'MySQL', it should be a PEAR DB style DSN string. … … 285 286 * @return array of translation messages. 286 287 */ 287 p rotectedfunction &loadData($variant)288 public function &loadData($variant) 288 289 { 289 290 return array(); … … 296 297 * @return string the resource key 297 298 */ 298 p rotectedfunction getSource($variant)299 public function getSource($variant) 299 300 { 300 301 return $variant; … … 307 308 * @return boolean true if valid, false otherwise. 308 309 */ 309 p rotectedfunction isValidSource($source)310 public function isValidSource($source) 310 311 { 311 312 return false; … … 319 320 * @return array list of all variants for this catalogue. 320 321 */ 321 p rotectedfunction getCatalogueList($catalogue)322 public function getCatalogueList($catalogue) 322 323 { 323 324 return array(); trunk/lib/i18n/sfMessageSource_Database.class.php
r4343 r4576 185 185 return $parsed; 186 186 } 187 188 /** 189 * Gets all the variants of a particular catalogue. 190 * 191 * @param string catalogue name 192 * @return array list of all variants for this catalogue. 193 */ 194 public function getCatalogueList($catalogue) 195 { 196 $variants = explode('_', $this->culture); 197 198 $catalogues = array($catalogue); 199 200 $variant = null; 201 202 for ($i = 0, $max = count($variants); $i < $max; $i++) 203 { 204 if (strlen($variants[$i]) > 0) 205 { 206 $variant .= $variant ? '_'.$variants[$i] : $variants[$i]; 207 $catalogues[] = $catalogue.'.'.$variant; 208 } 209 } 210 211 return array_reverse($catalogues); 212 } 187 213 } trunk/lib/i18n/sfMessageSource_File.class.php
r4342 r4576 54 54 * @return int last modified in unix-time format. 55 55 */ 56 p rotectedfunction getLastModified($source)56 public function getLastModified($source) 57 57 { 58 58 return is_file($source) ? filemtime($source) : 0; … … 65 65 * @return string full path to the message file. 66 66 */ 67 p rotectedfunction getSource($variant)67 public function getSource($variant) 68 68 { 69 69 return $this->source.'/'.$variant; … … 76 76 * @return boolean true if valid, false otherwise. 77 77 */ 78 p rotectedfunction isValidSource($source)78 public function isValidSource($source) 79 79 { 80 80 return is_file($source); … … 87 87 * @return array list of all variants for this catalogue. 88 88 */ 89 p rotectedfunction getCatalogueList($catalogue)89 public function getCatalogueList($catalogue) 90 90 { 91 91 $variants = explode('_', $this->culture); trunk/lib/i18n/sfMessageSource_MySQL.class.php
r4343 r4576 211 211 * @return array translation messages. 212 212 */ 213 p rotectedfunction &loadData($variant)213 public function &loadData($variant) 214 214 { 215 215 $variant = mysql_real_escape_string($variant, $this->db); … … 261 261 * @return boolean true if the catalogue+variant is in the database, false otherwise. 262 262 */ 263 p rotectedfunction isValidSource($variant)263 public function isValidSource($variant) 264 264 { 265 265 $variant = mysql_real_escape_string ($variant, $this->db); … … 272 272 273 273 return $result; 274 }275 276 /**277 * Gets all the variants of a particular catalogue.278 *279 * @param string catalogue name280 * @return array list of all variants for this catalogue.281 */282 protected function getCatalogueList($catalogue)283 {284 $variants = explode('_', $this->culture);285 286 $catalogues = array($catalogue);287 288 $variant = null;289 290 for ($i = 0, $max = count($variants); $i < $max; $i++)291 {292 if (strlen($variants[$i]) > 0)293 {294 $variant .= $variant ? '_'.$variants[$i] : $variants[$i];295 $catalogues[] = $catalogue.'.'.$variant;296 }297 }298 299 return array_reverse($catalogues);300 274 } 301 275 trunk/lib/i18n/sfMessageSource_SQLite.class.php
r4343 r4576 31 31 * cat_id INTEGER PRIMARY KEY, 32 32 * name VARCHAR NOT NULL, 33 * source_lang VARCHAR ,34 * target_lang VARCHAR ,33 * source_lang VARCHAR, 34 * target_lang VARCHAR, 35 35 * date_created INT, 36 36 * date_modified INT, … … 111 111 * @return array translation messages. 112 112 */ 113 p rotectedfunction &loadData($variant)113 public function &loadData($variant) 114 114 { 115 115 $variant = sqlite_escape_string($variant); 116 116 117 $statement = 117 $statement = 118 118 "SELECT t.id, t.source, t.target, t.comments 119 119 FROM trans_unit t, catalogue c 120 120 WHERE c.cat_id = t.cat_id 121 AND c.name = '{$variant}' 121 AND c.name = '{$variant}' 122 122 ORDER BY id ASC"; 123 123 … … 168 168 * @return boolean true if the catalogue+variant is in the database, false otherwise. 169 169 */ 170 p rotectedfunction isValidSource($variant)170 public function isValidSource($variant) 171 171 { 172 172 $variant = sqlite_escape_string($variant); … … 180 180 181 181 /** 182 * Gets all the variants of a particular catalogue.183 *184 * @param string catalogue name185 * @return array list of all variants for this catalogue.186 */187 protected function getCatalogueList($catalogue)188 {189 $variants = explode('_', $this->culture);190 191 $catalogues = array($catalogue);192 193 $variant = null;194 195 for ($i = 0, $max = count($variants); $i < $max; $i++)196 {197 if (strlen($variants[$i]) > 0)198 {199 $variant .= ($variant) ? '_'.$variants[$i] : $variants[$i];200 $catalogues[] = $catalogue.'.'.$variant;201 }202 }203 204 return array_reverse($catalogues);205 }206 207 /**208 182 * Retrieves catalogue details, array($cat_id, $variant, $count). 209 183 * … … 233 207 $cat_id = intval(sqlite_fetch_single($rs)); 234 208 235 // first get the catalogue ID209 // first get the catalogue ID 236 210 $rs = sqlite_query("SELECT count(msg_id) FROM trans_unit WHERE cat_id = {$cat_id}", $db); 237 211 … … 270 244 * @return boolean true if saved successfuly, false otherwise. 271 245 */ 272 function save($catalogue ='messages')246 function save($catalogue = 'messages') 273 247 { 274 248 $messages = $this->untranslated; … … 302 276 { 303 277 $message = sqlite_escape_string($message); 304 $statement = "INSERT INTO trans_unit (cat_id,id,source,date_added) VALUES ({$cat_id}, {$count},'{$message}',$time)"; 305 if (sqlite_query($statement, $db)) 278 if (sqlite_query("INSERT INTO trans_unit (cat_id, id, source, date_added) VALUES ({$cat_id}, {$count}, '{$message}', $time)", $db)) 306 279 { 307 280 $count++; … … 348 321 $db = sqlite_open($this->source); 349 322 350 $statement = "UPDATE trans_unit SET target = '{$target}', comments = '{$comments}', date_modified = '{$time}' WHERE cat_id = {$cat_id} AND source = '{$text}'"; 351 352 $updated = false; 353 354 if (sqlite_query($statement, $db)) 355 { 356 $updated = $this->updateCatalogueTime($cat_id, $variant, $db); 323 sqlite_query("UPDATE trans_unit SET target = '{$target}', comments = '{$comments}', date_modified = '{$time}' WHERE cat_id = {$cat_id} AND source = '{$text}'", $db); 324 325 if (sqlite_changes($db)) 326 { 327 $this->updateCatalogueTime($cat_id, $variant, $db); 328 $updated = true; 329 } 330 else 331 { 332 $updated = false; 357 333 } 358 334 … … 384 360 $text = sqlite_escape_string($message); 385 361 386 $statement = "DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'"; 387 $deleted = false; 388 389 if (sqlite_query($statement, $db)) 390 { 391 $deleted = $this->updateCatalogueTime($cat_id, $variant, $db); 362 sqlite_query("DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'", $db); 363 364 if (sqlite_changes($db)) 365 { 366 $this->updateCatalogueTime($cat_id, $variant, $db); 367 $deleted = true; 368 } 369 else 370 { 371 $deleted = false; 392 372 } 393 373 … … 410 390 while ($row = sqlite_fetch_array($rs, SQLITE_NUM)) 411 391 { 412 $details = explode('.', $row[0]);392 $details = explode('.', $row[0]); 413 393 if (!isset($details[1])) 414 394 { trunk/lib/i18n/sfMessageSource_XLIFF.class.php
r4342 r4576 48 48 * @return array of messages. 49 49 */ 50 protected function &loadData($filename) 51 { 52 //load it. 53 50 public function &loadData($filename) 51 { 54 52 $XML = simplexml_load_file($filename); 55 53 … … 67 65 $source = (string) $unit->source; 68 66 $translations[$source][] = (string) $unit->target; 69 $translations[$source][] = (string) $unit['id'];70 $translations[$source][] = (string) $unit->note;67 $translations[$source][] = (string) $unit['id']; 68 $translations[$source][] = (string) $unit->note; 71 69 } 72 70 trunk/lib/i18n/sfMessageSource_gettext.class.php
r4342 r4576 51 51 * @return array of messages. 52 52 */ 53 p rotectedfunction &loadData($filename)53 public function &loadData($filename) 54 54 { 55 55 $mo = TGettext::factory('MO',$filename);