]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/I18N/TGlobalization.php
9a57ec3b6157eb200a32cb540494e245940b4b7d
[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 https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
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(isset($config['cache']) && TPropertyValue::ensureBoolean($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                 else
229                 {
230                         unset($config['cache']);
231                 }
232                 $this->_translation = $config;
233         }
234
235         /**
236          * @return string current translation catalogue.
237          */
238         public function getTranslationCatalogue()
239         {
240                 return $this->_translation['catalogue'];
241         }
242
243         /**
244          * @param string update the translation catalogue.
245          */
246         public function setTranslationCatalogue($value)
247         {
248                 $this->_translation['catalogue'] = $value;
249         }
250
251         /**
252          * Gets all the variants of a specific culture. If the parameter
253          * $culture is null, the current culture is used.
254          * @param string $culture the Culture string
255          * @return array variants of the culture.
256          */
257         public function getCultureVariants($culture=null)
258         {
259                 if($culture===null) $culture = $this->getCulture();
260                 $variants = explode('_', $culture);
261                 $result = array();
262                 for(; count($variants) > 0; array_pop($variants))
263                         $result[] = implode('_', $variants);
264                 return $result;
265         }
266
267         /**
268          * Returns a list of possible localized files. Example
269          * <code>
270          * $files = $app->getLocalizedResource("path/to/Home.page","en_US");
271          * </code>
272          * will return
273          * <pre>
274          * array
275          *   0 => 'path/to/en_US/Home.page'
276          *   1 => 'path/to/en/Home.page'
277          *   2 => 'path/to/Home.en_US.page'
278          *   3 => 'path/to/Home.en.page'
279          *   4 => 'path/to/Home.page'
280          * </pre>
281          * Note that you still need to verify the existance of these files.
282          * @param string filename
283          * @param string culture string, null to use current culture
284          * @return array list of possible localized resource files.
285          */
286         public function getLocalizedResource($file,$culture=null)
287         {
288                 $files = array();
289                 $variants = $this->getCultureVariants($culture);
290                 $path = pathinfo($file);
291                 foreach($variants as $variant)
292                         $files[] = $path['dirname'].DIRECTORY_SEPARATOR.$variant.DIRECTORY_SEPARATOR.$path['basename'];
293                 $filename = substr($path['basename'],0,strrpos($path['basename'],'.'));
294                 foreach($variants as $variant)
295                         $files[] = $path['dirname'].DIRECTORY_SEPARATOR.$filename.'.'.$variant.'.'.$path['extension'];
296                 $files[] = $file;
297                 return $files;
298         }
299
300 }