4 * MessageSource_gettext class file.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the BSD License.
9 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
11 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12 * The latest version of PRADO can be obtained from:
13 * {@link http://prado.sourceforge.net/}
15 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
16 * @version $Revision: 1.7 $ $Date: 2005/12/17 06:11:28 $
17 * @package System.I18N.core
21 * Get the MessageSource class file.
23 require_once(dirname(__FILE__).'/MessageSource.php');
26 * Get the Gettext class.
28 require_once(dirname(__FILE__).'/Gettext/TGettext.php');
31 * MessageSource_gettext class.
33 * Using Gettext MO format as the message source for translation.
34 * The gettext classes are based on PEAR's gettext MO and PO classes.
36 * See the MessageSource::factory() method to instantiate this class.
38 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
39 * @version v1.0, last update on Fri Dec 24 16:18:44 EST 2004
40 * @package System.I18N.core
42 class MessageSource_gettext extends MessageSource
45 * Message data filename extension.
48 protected $dataExt = '.mo';
51 * PO data filename extension
54 protected $poExt = '.po';
57 * Separator between culture name and source.
60 protected $dataSeparator = '.';
62 function __construct($source)
64 $this->source = (string)$source;
69 * Load the messages from a MO file.
70 * @param string MO file.
71 * @return array of messages.
73 protected function &loadData($filename)
75 $mo = TGettext::factory('MO',$filename);
77 $result = $mo->toArray();
81 foreach($result['strings'] as $source => $target)
83 $results[$source][] = $target; //target
84 $results[$source][] = $count++; //id
85 $results[$source][] = ''; //comments
91 * Determin if the MO file source is valid.
92 * @param string MO file
93 * @return boolean true if valid, false otherwise.
95 protected function isValidSource($filename)
97 return is_file($filename);
101 * Get the MO file for a specific message catalogue and cultural
103 * @param string message catalogue
104 * @return string full path to the MO file.
106 protected function getSource($variant)
108 return $this->source.'/'.$variant;
112 * Get the last modified unix-time for this particular catalogue+variant.
113 * Just use the file modified time.
114 * @param string catalogue+variant
115 * @return int last modified in unix-time format.
117 protected function getLastModified($source)
120 return filemtime($source);
126 * Get all the variants of a particular catalogue.
127 * @param string catalogue name
128 * @return array list of all variants for this catalogue.
130 protected function getCatalogueList($catalogue)
132 $variants = explode('_',$this->culture);
133 $source = $catalogue.$this->dataExt;
135 $catalogues = array($source);
139 for($i = 0, $k = count($variants); $i < $k; ++$i)
141 if(isset($variants[$i]{0}))
143 $variant .= ($variant)?'_'.$variants[$i]:$variants[$i];
144 $catalogues[] = $catalogue.$this->dataSeparator.
145 $variant.$this->dataExt;
148 $byDir = $this->getCatalogueByDir($catalogue);
149 $catalogues = array_merge($byDir,array_reverse($catalogues));
155 * Traverse through the directory structure to find the catalogues.
156 * This should only be called by getCatalogueList()
157 * @param string a particular catalogue.
158 * @return array a list of catalogues.
159 * @see getCatalogueList()
161 private function getCatalogueByDir($catalogue)
163 $variants = explode('_',$this->culture);
164 $catalogues = array();
168 for($i = 0, $k = count($variants); $i < $k; ++$i)
170 if(isset($variants[$i]{0}))
172 $variant .= ($variant)?'_'.$variants[$i]:$variants[$i];
173 $catalogues[] = $variant.'/'.$catalogue.$this->dataExt;
176 return array_reverse($catalogues);
180 * Get the variant for a catalogue depending on the current culture.
181 * @param string catalogue
182 * @return string the variant.
187 private function getVariants($catalogue='messages')
189 if($catalogue === null) {
190 $catalogue = 'messages';
193 foreach($this->getCatalogueList($catalogue) as $variant)
195 $file = $this->getSource($variant);
196 $po = $this->getPOFile($file);
197 if(is_file($file) || is_file($po))
198 return array($variant, $file, $po);
203 private function getPOFile($MOFile)
205 $filebase = substr($MOFile, 0, strlen($MOFile)-strlen($this->dataExt));
206 return $filebase.$this->poExt;
210 * Save the list of untranslated blocks to the translation source.
211 * If the translation was not found, you should add those
212 * strings to the translation source via the <b>append()</b> method.
213 * @param string the catalogue to add to
214 * @return boolean true if saved successfuly, false otherwise.
216 function save($catalogue='messages')
218 $messages = $this->untranslated;
220 if(count($messages) <= 0) return false;
222 $variants = $this->getVariants($catalogue);
225 list($variant, $MOFile, $POFile) = $variants;
227 list($variant, $MOFile, $POFile) = $this->createMessageTemplate($catalogue);
229 if(is_writable($MOFile) == false)
230 throw new TIOException("Unable to save to file {$MOFile}, file must be writable.");
231 if(is_writable($POFile) == false)
232 throw new TIOException("Unable to save to file {$POFile}, file must be writable.");
234 //set the strings as untranslated.
236 foreach($messages as $message)
237 $strings[$message] = '';
240 $po = TGettext::factory('PO',$POFile);
242 $result = $po->toArray();
244 $existing = count($result['strings']);
246 //add to strings to the existing message list
247 $result['strings'] = array_merge($result['strings'],$strings);
249 $new = count($result['strings']);
253 //change the date 2004-12-25 12:26
254 $result['meta']['PO-Revision-Date'] = @date('Y-m-d H:i:s');
256 $po->fromArray($result);
258 if($po->save() && $mo->save($MOFile))
260 if(!empty($this->cache))
261 $this->cache->clean($variant, $this->culture);
271 * Delete a particular message from the specified catalogue.
272 * @param string the source message to delete.
273 * @param string the catalogue to delete from.
274 * @return boolean true if deleted, false otherwise.
276 function delete($message, $catalogue='messages')
278 $variants = $this->getVariants($catalogue);
280 list($variant, $MOFile, $POFile) = $variants;
284 if(is_writable($MOFile) == false)
285 throw new TIOException("Unable to modify file {$MOFile}, file must be writable.");
286 if(is_writable($POFile) == false)
287 throw new TIOException("Unable to modify file {$POFile}, file must be writable.");
289 $po = TGettext::factory('PO',$POFile);
291 $result = $po->toArray();
293 foreach($result['strings'] as $string => $value)
295 if($string == $message)
297 $result['meta']['PO-Revision-Date'] = @date('Y-m-d H:i:s');
298 unset($result['strings'][$string]);
300 $po->fromArray($result);
302 if($po->save() && $mo->save($MOFile))
304 if(!empty($this->cache))
305 $this->cache->clean($variant, $this->culture);
317 * Update the translation.
318 * @param string the source string.
319 * @param string the new translation string.
320 * @param string comments
321 * @param string the catalogue of the translation.
322 * @return boolean true if translation was updated, false otherwise.
324 function update($text, $target, $comments, $catalogue='messages')
326 $variants = $this->getVariants($catalogue);
328 list($variant, $MOFile, $POFile) = $variants;
332 if(is_writable($MOFile) == false)
333 throw new TIOException("Unable to update file {$MOFile}, file must be writable.");
334 if(is_writable($POFile) == false)
335 throw new TIOException("Unable to update file {$POFile}, file must be writable.");
338 $po = TGettext::factory('PO',$POFile);
340 $result = $po->toArray();
342 foreach($result['strings'] as $string => $value)
346 $result['strings'][$string] = $target;
347 $result['meta']['PO-Revision-Date'] = @date('Y-m-d H:i:s');
349 $po->fromArray($result);
352 if($po->save() && $mo->save($MOFile))
354 if(!empty($this->cache))
355 $this->cache->clean($variant, $this->culture);
368 * Returns a list of catalogue as key and all it variants as value.
369 * @return array list of catalogues
371 function catalogues()
373 return $this->getCatalogues();
377 * Returns a list of catalogue and its culture ID. This takes care
378 * of directory structures.
379 * E.g. array('messages','en_AU')
380 * @return array list of catalogues
382 protected function getCatalogues($dir=null,$variant=null)
384 $dir = $dir?$dir:$this->source;
385 $files = scandir($dir);
387 $catalogue = array();
389 foreach($files as $file)
391 if(is_dir($dir.'/'.$file)
392 && preg_match('/^[a-z]{2}(_[A-Z]{2,3})?$/',$file))
395 $catalogue = array_merge($catalogue,
396 $this->getCatalogues($dir.'/'.$file, $file));
399 $pos = strpos($file,$this->dataExt);
402 && substr($file,-1*strlen($this->dataExt)) == $this->dataExt)
404 $name = substr($file,0,$pos);
405 $dot = strrpos($name,$this->dataSeparator);
410 $culture = substr($name, $dot+1,strlen($name));
411 $cat = substr($name,0,$dot);
414 $details[1] = $culture;
417 $catalogue[] = $details;
425 protected function createMessageTemplate($catalogue)
427 if($catalogue === null) {
428 $catalogue = 'messages';
430 $variants = $this->getCatalogueList($catalogue);
431 $variant = array_shift($variants);
432 $mo_file = $this->getSource($variant);
433 $po_file = $this->getPOFile($mo_file);
435 $dir = dirname($mo_file);
439 @chmod($dir,PRADO_CHMOD);
442 throw new TException("Unable to create directory $dir");
444 $po = TGettext::factory('PO',$po_file);
445 $result['meta']['PO-Revision-Date'] = @date('Y-m-d H:i:s');
446 $result['strings'] = array();
448 $po->fromArray($result);
450 if($po->save() && $mo->save($mo_file))
451 return array($variant, $mo_file, $po_file);
453 throw new TException("Unable to create file $po_file and $mo_file");