]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/I18N/TGlobalization.php
3fbaa2b49df9a4e422d9e784bf7188644c142362
[bacula/bacula] / gui / baculum / framework / I18N / TGlobalization.php
1 <?php
2 /**
3  * TGlobalization class file.
4  *
5  * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.I18N
10  */
11
12
13 /**
14  * TGlobalization contains settings for Culture, Charset
15  * and TranslationConfiguration.
16  *
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.
20  *
21  * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
22  * @version $Revision: 1.66 $  $Date: ${DATE} ${TIME} $
23  * @package System.I18N
24  * @since 3.0
25  */
26 class TGlobalization extends TModule
27 {
28         /**
29          * Default character set is 'UTF-8'.
30          * @var string
31          */
32         private $_defaultCharset = 'UTF-8';
33
34         /**
35          * Default culture is 'en'.
36          * @var string
37          */
38         private $_defaultCulture = 'en';
39
40         /**
41          * The current charset.
42          * @var string
43          */
44         private $_charset=null;
45
46         /**
47          * The current culture.
48          * @var string
49          */
50         private $_culture=null;
51
52         /**
53          * Translation source parameters.
54          * @var TMap
55          */
56         private $_translation;
57
58         /**
59          * @var boolean whether we should translate the default culture
60          */
61         private $_translateDefaultCulture=true;
62
63         /**
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
69          */
70         public function init($config)
71         {
72                 if($this->_charset===null)
73                         $this->_charset=$this->getDefaultCharset();
74                 if($this->_culture===null)
75                         $this->_culture=$this->getDefaultCulture();
76
77                 if($config!==null)
78                 {
79                         if($this->getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
80                                 $translation = isset($config['translate'])?$config['translate']:null;
81                         else
82                         {
83                                 $t = $config->getElementByTagName('translation');
84                                 $translation = ($t)?$t->getAttributes():null;
85                         }
86                         if($translation)
87                                 $this->setTranslationConfiguration($translation);
88                 }
89                 $this->getApplication()->setGlobalization($this);
90         }
91
92         /**
93          * @return string default culture
94          */
95         public function getTranslateDefaultCulture()
96         {
97                 return $this->_translateDefaultCulture;
98         }
99
100         /**
101          * @param bool default culture, e.g. <tt>en_US</tt> for American English
102          */
103         public function setTranslateDefaultCulture($value)
104         {
105                 $this->_translateDefaultCulture = TPropertyValue::ensureBoolean($value);
106         }
107
108         /**
109          * @return string default culture
110          */
111         public function getDefaultCulture()
112         {
113                 return $this->_defaultCulture;
114         }
115
116         /**
117          * @param string default culture, e.g. <tt>en_US</tt> for American English
118          */
119         public function setDefaultCulture($culture)
120         {
121                 $this->_defaultCulture = str_replace('-','_',$culture);
122         }
123
124         /**
125          * @return string default charset set
126          */
127         public function getDefaultCharset()
128         {
129                 return $this->_defaultCharset;
130         }
131
132         /**
133          * @param string default localization charset, e.g. <tt>UTF-8</tt>
134          */
135         public function setDefaultCharset($charset)
136         {
137                 $this->_defaultCharset = $charset;
138         }
139
140         /**
141          * @return string current application culture
142          */
143         public function getCulture()
144         {
145                 return $this->_culture;
146         }
147
148         /**
149          * @param string culture, e.g. <tt>en_US</tt> for American English
150          */
151         public function setCulture($culture)
152         {
153                 $this->_culture = str_replace('-','_',$culture);
154         }
155
156         /**
157          * @return string localization charset
158          */
159         public function getCharset()
160         {
161                 return $this->_charset;
162         }
163
164         /**
165          * @param string localization charset, e.g. <tt>UTF-8</tt>
166          */
167         public function setCharset($charset)
168         {
169                 $this->_charset = $charset;
170         }
171
172         /**
173          * @return TMap translation source configuration.
174          */
175         public function getTranslationConfiguration()
176         {
177                 return (!$this->_translateDefaultCulture && ($this->getDefaultCulture() == $this->getCulture()))
178                         ? null
179                         : $this->_translation;
180         }
181
182         /**
183          * Sets the translation configuration. Example configuration:
184          * <code>
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 '@@'
193          * </code>
194          * Throws exception is source is not found.
195          * @param TMap|array configuration options
196          */
197         protected function setTranslationConfiguration($config)
198         {
199                 if($config['type'] == 'XLIFF' || $config['type'] == 'gettext')
200                 {
201                         if($config['source'])
202                         {
203                                 $config['source'] = Prado::getPathOfNamespace($config['source']);
204                                 if(!is_dir($config['source']))
205                                 {
206                                         if(@mkdir($config['source'])===false)
207                                         throw new TConfigurationException('globalization_source_path_failed',
208                                                 $config['source']);
209                                         chmod($config['source'], PRADO_CHMOD); //make it deletable
210                                 }
211                         }
212                         else
213                         {
214                                 throw new TConfigurationException("invalid source dir '{$config['source']}'");
215                         }
216                 }
217                 if($config['cache'])
218                 {
219                         $config['cache'] = $this->getApplication()->getRunTimePath().'/i18n';
220                         if(!is_dir($config['cache']))
221                         {
222                                 if(@mkdir($config['cache'])===false)
223                                         throw new TConfigurationException('globalization_cache_path_failed',
224                                                 $config['cache']);
225                                 chmod($config['cache'], PRADO_CHMOD); //make it deletable
226                         }
227                 }
228                 $this->_translation = $config;
229         }
230
231         /**
232          * @return string current translation catalogue.
233          */
234         public function getTranslationCatalogue()
235         {
236                 return $this->_translation['catalogue'];
237         }
238
239         /**
240          * @param string update the translation catalogue.
241          */
242         public function setTranslationCatalogue($value)
243         {
244                 $this->_translation['catalogue'] = $value;
245         }
246
247         /**
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.
252          */
253         public function getCultureVariants($culture=null)
254         {
255                 if($culture===null) $culture = $this->getCulture();
256                 $variants = explode('_', $culture);
257                 $result = array();
258                 for(; count($variants) > 0; array_pop($variants))
259                         $result[] = implode('_', $variants);
260                 return $result;
261         }
262
263         /**
264          * Returns a list of possible localized files. Example
265          * <code>
266          * $files = $app->getLocalizedResource("path/to/Home.page","en_US");
267          * </code>
268          * will return
269          * <pre>
270          * array
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'
276          * </pre>
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.
281          */
282         public function getLocalizedResource($file,$culture=null)
283         {
284                 $files = array();
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'];
292                 $files[] = $file;
293                 return $files;
294         }
295
296 }
297