| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/i18n/src/translation/lmbI18NDictionary.class.php'); 10 lmb_require('limb/fs/src/exception/lmbFileNotFoundException.class.php'); 11 12 @define('LIMB_TRANSLATIONS_INCLUDE_PATH', 'i18n/translations;limb/*/i18n/translations'); 13 14 /** 15 * class lmbQtDictionaryBackend. 16 * 17 * @package i18n 18 * @version $Id$ 19 */ 20 class lmbQtDictionaryBackend //extends lmbDictionaryBackend ??? 21 { 22 protected $use_cache = false; 23 protected $cache_dir; 24 25 function __construct() 26 { 27 $this->search_path = LIMB_TRANSLATIONS_INCLUDE_PATH; 28 } 29 30 function setCacheDir($dir) 31 { 32 $this->cache_dir = $dir; 33 } 34 35 function useCache($flag = true) 36 { 37 $this->use_cache = $flag; 38 } 39 40 function setSearchPath($path) 41 { 42 $this->search_path = $path; 43 } 44 45 function load($locale, $domain) 46 { 47 $file = $this->mapToFile($locale, $domain); 48 return $this->loadFromFile($file); 49 } 50 51 function save($locale, $domain, $dict) 52 { 53 $file = $this->mapToFile($locale, $domain); 54 return $this->saveToFile($file, $dict); 55 } 56 57 function loadAll() 58 { 59 $locator = lmbToolkit :: instance()->getFileLocator($this->search_path, 'i18n'); 60 $dicts = array(); 61 $files = $locator->locateAll('*.ts'); 62 63 foreach($files as $file) 64 { 65 list($domain, $locale, ) = explode('.', basename($file)); 66 $dicts[$locale][$domain] = $this->loadFromFile($file); 67 } 68 69 return $dicts; 70 } 71 72 function info($locale, $domain) 73 { 74 $file = $this->mapToFile($locale, $domain); 75 return "Qt dictionary contained in '$file', locale '$locale', domain '$domain'"; 76 } 77 78 function mapToFile($locale, $domain) 79 { 80 return lmbToolkit :: instance()->findFileAlias($domain . '.' . $locale . '.ts', $this->search_path, 'i18n'); 81 } 82 83 function getDOMDocument($dictionary) 84 { 85 $doc = new DOMDocument('1.0', 'utf-8'); 86 $doc->formatOutput = true; // pretty printing 87 88 $ts_node = $doc->createElement('TS'); 89 $doc->appendChild($ts_node); 90 91 $translations = $dictionary->getTranslations(); 92 $context_node = $doc->createElement('context'); 93 94 foreach($translations as $text => $translation) 95 { 96 $message_node = $doc->createElement('message'); 97 $text_node = $doc->createElement('source'); 98 $translation_node = $doc->createElement('translation'); 99 100 $text_node->appendChild($doc->createTextNode($text)); 101 102 if(empty($translation)) 103 $translation_node->setAttribute('type', 'unfinished'); 104 else 105 $translation_node->appendChild($doc->createTextNode($translation)); 106 107 $message_node->appendChild($text_node); 108 $message_node->appendChild($translation_node); 109 110 $context_node->appendChild($message_node); 111 112 $ts_node->appendChild($context_node); 113 } 114 return $doc; 115 } 116 117 function loadFromXML($xml) 118 { 119 $dictionary = new lmbI18NDictionary(); 120 $this->_parseXML($dictionary, $xml); 121 return $dictionary; 122 } 123 124 function loadFromFile($file) 125 { 126 if(!file_exists($file)) 127 throw new lmbFileNotFoundException($file, "translations file $file not found"); 128 129 $dictionary = new lmbI18NDictionary(); 130 131 if(!$this->_loadFromCache($dictionary, $file)) 132 { 133 try 134 { 135 $this->_parseXML($dictionary, file_get_contents($file)); 136 } 137 catch(lmbException $e) 138 { 139 throw new lmbException($e->getMessage() . " at file '" . $file . "'"); 140 } 141 $this->_saveToCache($dictionary, $file); 142 } 143 return $dictionary; 144 } 145 146 protected function _parseXML($dictionary, $xml) 147 { 148 if(!$xml_doc = simplexml_load_string($xml)) 149 { 150 throw new lmbException('SimpleXML parsing error'); 151 } 152 153 foreach($xml_doc->context as $context) 154 { 155 foreach($context->message as $message) 156 { 157 if($translation = trim((string)$message->translation)) 158 $dictionary->add((string)$message->source, $translation); 159 else 160 $dictionary->add((string)$message->source); 161 } 162 } 163 return true; 164 } 165 166 function saveToFile($file, $dictionary) 167 { 168 $this->getDOMDocument($dictionary)->save($file); 169 } 170 171 protected function _isFileCachingOn() 172 { 173 return $this->use_cache && $this->cache_dir; 174 } 175 176 protected function _loadFromCache($dictionary, $file) 177 { 178 if(!$this->_isFileCachingOn()) 179 return false; 180 181 if(!file_exists($cache = $this->_getCacheFile($file))) 182 return false; 183 184 $dictionary->setTranslations(unserialize(file_get_contents($cache))); 185 return true; 186 } 187 188 protected function _saveToCache($dictionary, $file) 189 { 190 if(!$this->_isFileCachingOn()) 191 return; 192 193 $cache = $this->_getCacheFile($file); 194 if(!is_dir($dir = dirname($cache))) 195 mkdir($dir); 196 file_put_contents($this->_getCacheFile($file), serialize($dictionary->getTranslations()), LOCK_EX); 197 } 198 199 protected function _getCacheFile($file) 200 { 201 return $this->cache_dir . '/i18n-qt/' . md5(realpath($file)); 202 } 203 } 204 205 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Oct 14 04:47:40 2008 | Cross-referenced by PHPXref 0.7 |