3 * TGlobalization class file.
5 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
14 * TGlobalization contains settings for Culture, Charset
15 * and TranslationConfiguration.
17 * TGlobalization can be subclassed to change how the Culture, Charset
18 * are determined. See TGlobalizationAutoDetect for example of
19 * setting the Culture based on browser settings.
21 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
22 * @version $Revision: 1.66 $ $Date: ${DATE} ${TIME} $
23 * @package System.I18N
26 class TGlobalization extends TModule
29 * Default character set is 'UTF-8'.
32 private $_defaultCharset = 'UTF-8';
35 * Default culture is 'en'.
38 private $_defaultCulture = 'en';
41 * The current charset.
44 private $_charset=null;
47 * The current culture.
50 private $_culture=null;
53 * Translation source parameters.
56 private $_translation;
59 * @var boolean whether we should translate the default culture
61 private $_translateDefaultCulture=true;
64 * Initialize the Culture and Charset for this application.
65 * You should override this method if you want a different way of
66 * setting the Culture and/or Charset for your application.
67 * If you override this method, call parent::init($xml) first.
68 * @param mixed application configuration
70 public function init($config)
72 if($this->_charset===null)
73 $this->_charset=$this->getDefaultCharset();
74 if($this->_culture===null)
75 $this->_culture=$this->getDefaultCulture();
79 if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
80 $translation = isset($config['translate'])?$config['translate']:null;
83 $t = $config->getElementByTagName('translation');
84 $translation = ($t)?$t->getAttributes():null;
87 $this->setTranslationConfiguration($translation);
89 $this->getApplication()->setGlobalization($this);
93 * @return string default culture
95 public function getTranslateDefaultCulture()
97 return $this->_translateDefaultCulture;
101 * @param bool default culture, e.g. <tt>en_US</tt> for American English
103 public function setTranslateDefaultCulture($value)
105 $this->_translateDefaultCulture = TPropertyValue::ensureBoolean($value);
109 * @return string default culture
111 public function getDefaultCulture()
113 return $this->_defaultCulture;
117 * @param string default culture, e.g. <tt>en_US</tt> for American English
119 public function setDefaultCulture($culture)
121 $this->_defaultCulture = str_replace('-','_',$culture);
125 * @return string default charset set
127 public function getDefaultCharset()
129 return $this->_defaultCharset;
133 * @param string default localization charset, e.g. <tt>UTF-8</tt>
135 public function setDefaultCharset($charset)
137 $this->_defaultCharset = $charset;
141 * @return string current application culture
143 public function getCulture()
145 return $this->_culture;
149 * @param string culture, e.g. <tt>en_US</tt> for American English
151 public function setCulture($culture)
153 $this->_culture = str_replace('-','_',$culture);
157 * @return string localization charset
159 public function getCharset()
161 return $this->_charset;
165 * @param string localization charset, e.g. <tt>UTF-8</tt>
167 public function setCharset($charset)
169 $this->_charset = $charset;
173 * @return TMap translation source configuration.
175 public function getTranslationConfiguration()
177 return (!$this->_translateDefaultCulture && ($this->getDefaultCulture() == $this->getCulture()))
179 : $this->_translation;
183 * Sets the translation configuration. Example configuration:
185 * $config['type'] = 'XLIFF'; //XLIFF, gettext, Database or MySQL (deprecated)
186 * $config['source'] = 'Path.to.directory'; // for types XLIFF and gettext
187 * $config['source'] = 'connectionId'; // for type Database
188 * $config['source'] = 'mysql://user:pw@host/db'; // for type MySQL (deprecated)
189 * $config['catalogue'] = 'messages'; //default catalog
190 * $config['autosave'] = 'true'; //save untranslated message
191 * $config['cache'] = 'true'; //cache translated message
192 * $config['marker'] = '@@'; // surround untranslated text with '@@'
194 * Throws exception is source is not found.
195 * @param TMap|array configuration options
197 protected function setTranslationConfiguration($config)
199 if($config['type'] == 'XLIFF' || $config['type'] == 'gettext')
201 if($config['source'])
203 $config['source'] = Prado::getPathOfNamespace($config['source']);
204 if(!is_dir($config['source']))
206 if(@mkdir($config['source'])===false)
207 throw new TConfigurationException('globalization_source_path_failed',
209 chmod($config['source'], PRADO_CHMOD); //make it deletable
214 throw new TConfigurationException("invalid source dir '{$config['source']}'");
219 $config['cache'] = $this->getApplication()->getRunTimePath().'/i18n';
220 if(!is_dir($config['cache']))
222 if(@mkdir($config['cache'])===false)
223 throw new TConfigurationException('globalization_cache_path_failed',
225 chmod($config['cache'], PRADO_CHMOD); //make it deletable
228 $this->_translation = $config;
232 * @return string current translation catalogue.
234 public function getTranslationCatalogue()
236 return $this->_translation['catalogue'];
240 * @param string update the translation catalogue.
242 public function setTranslationCatalogue($value)
244 $this->_translation['catalogue'] = $value;
248 * Gets all the variants of a specific culture. If the parameter
249 * $culture is null, the current culture is used.
250 * @param string $culture the Culture string
251 * @return array variants of the culture.
253 public function getCultureVariants($culture=null)
255 if($culture===null) $culture = $this->getCulture();
256 $variants = explode('_', $culture);
258 for(; count($variants) > 0; array_pop($variants))
259 $result[] = implode('_', $variants);
264 * Returns a list of possible localized files. Example
266 * $files = $app->getLocalizedResource("path/to/Home.page","en_US");
271 * 0 => 'path/to/en_US/Home.page'
272 * 1 => 'path/to/en/Home.page'
273 * 2 => 'path/to/Home.en_US.page'
274 * 3 => 'path/to/Home.en.page'
275 * 4 => 'path/to/Home.page'
277 * Note that you still need to verify the existance of these files.
278 * @param string filename
279 * @param string culture string, null to use current culture
280 * @return array list of possible localized resource files.
282 public function getLocalizedResource($file,$culture=null)
285 $variants = $this->getCultureVariants($culture);
286 $path = pathinfo($file);
287 foreach($variants as $variant)
288 $files[] = $path['dirname'].DIRECTORY_SEPARATOR.$variant.DIRECTORY_SEPARATOR.$path['basename'];
289 $filename = substr($path['basename'],0,strrpos($path['basename'],'.'));
290 foreach($variants as $variant)
291 $files[] = $path['dirname'].DIRECTORY_SEPARATOR.$filename.'.'.$variant.'.'.$path['extension'];