| 1 |
lars |
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 © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TGlobalization.php 2482 2008-07-30 02:07:13Z knut $
|
|
|
10 |
* @package System.I18N
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* TGlobalization contains settings for Culture, Charset
|
|
|
16 |
* and TranslationConfiguration.
|
|
|
17 |
*
|
|
|
18 |
* TGlobalization can be subclassed to change how the Culture, Charset
|
|
|
19 |
* are determined. See TGlobalizationAutoDetect for example of
|
|
|
20 |
* setting the Culture based on browser settings.
|
|
|
21 |
*
|
|
|
22 |
* @author Wei Zhuo<weizhuo[at]gmail[dot]com>
|
|
|
23 |
* @version $Revision: 1.66 $ $Date: ${DATE} ${TIME} $
|
|
|
24 |
* @package System.I18N
|
|
|
25 |
* @since 3.0
|
|
|
26 |
*/
|
|
|
27 |
class TGlobalization extends TModule
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* Default character set is 'UTF-8'.
|
|
|
31 |
* @var string
|
|
|
32 |
*/
|
|
|
33 |
private $_defaultCharset = 'UTF-8';
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Default culture is 'en'.
|
|
|
37 |
* @var string
|
|
|
38 |
*/
|
|
|
39 |
private $_defaultCulture = 'en';
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The current charset.
|
|
|
43 |
* @var string
|
|
|
44 |
*/
|
|
|
45 |
private $_charset=null;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* The current culture.
|
|
|
49 |
* @var string
|
|
|
50 |
*/
|
|
|
51 |
private $_culture=null;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Translation source parameters.
|
|
|
55 |
* @var TMap
|
|
|
56 |
*/
|
|
|
57 |
private $_translation;
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Initialize the Culture and Charset for this application.
|
|
|
62 |
* You should override this method if you want a different way of
|
|
|
63 |
* setting the Culture and/or Charset for your application.
|
|
|
64 |
* If you override this method, call parent::init($xml) first.
|
|
|
65 |
* @param TXmlElement application configuration
|
|
|
66 |
*/
|
|
|
67 |
public function init($xml)
|
|
|
68 |
{
|
|
|
69 |
if($this->_charset===null)
|
|
|
70 |
$this->_charset=$this->getDefaultCharset();
|
|
|
71 |
if($this->_culture===null)
|
|
|
72 |
$this->_culture=$this->getDefaultCulture();
|
|
|
73 |
|
|
|
74 |
if($xml!==null)
|
|
|
75 |
{
|
|
|
76 |
$translation = $xml->getElementByTagName('translation');
|
|
|
77 |
if($translation)
|
|
|
78 |
$this->setTranslationConfiguration($translation->getAttributes());
|
|
|
79 |
}
|
|
|
80 |
$this->getApplication()->setGlobalization($this);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* @return string default culture
|
|
|
85 |
*/
|
|
|
86 |
public function getDefaultCulture()
|
|
|
87 |
{
|
|
|
88 |
return $this->_defaultCulture;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* @param string default culture, e.g. <tt>en_US</tt> for American English
|
|
|
93 |
*/
|
|
|
94 |
public function setDefaultCulture($culture)
|
|
|
95 |
{
|
|
|
96 |
$this->_defaultCulture = str_replace('-','_',$culture);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* @return string default charset set
|
|
|
101 |
*/
|
|
|
102 |
public function getDefaultCharset()
|
|
|
103 |
{
|
|
|
104 |
return $this->_defaultCharset;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* @param string default localization charset, e.g. <tt>UTF-8</tt>
|
|
|
109 |
*/
|
|
|
110 |
public function setDefaultCharset($charset)
|
|
|
111 |
{
|
|
|
112 |
$this->_defaultCharset = $charset;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* @return string current application culture
|
|
|
117 |
*/
|
|
|
118 |
public function getCulture()
|
|
|
119 |
{
|
|
|
120 |
return $this->_culture;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* @param string culture, e.g. <tt>en_US</tt> for American English
|
|
|
125 |
*/
|
|
|
126 |
public function setCulture($culture)
|
|
|
127 |
{
|
|
|
128 |
$this->_culture = str_replace('-','_',$culture);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* @return string localization charset
|
|
|
133 |
*/
|
|
|
134 |
public function getCharset()
|
|
|
135 |
{
|
|
|
136 |
return $this->_charset;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* @param string localization charset, e.g. <tt>UTF-8</tt>
|
|
|
141 |
*/
|
|
|
142 |
public function setCharset($charset)
|
|
|
143 |
{
|
|
|
144 |
$this->_charset = $charset;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* @return TMap translation source configuration.
|
|
|
149 |
*/
|
|
|
150 |
public function getTranslationConfiguration()
|
|
|
151 |
{
|
|
|
152 |
return $this->_translation;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Sets the translation configuration. Example configuration:
|
|
|
157 |
* <code>
|
|
|
158 |
* $config['type'] = 'XLIFF'; //XLIFF, gettext, Database or MySQL (deprecated)
|
|
|
159 |
* $config['source'] = 'Path.to.directory'; // for types XLIFF and gettext
|
|
|
160 |
* $config['source'] = 'connectionId'; // for type Database
|
|
|
161 |
* $config['source'] = 'mysql://user:pw@host/db'; // for type MySQL (deprecated)
|
|
|
162 |
* $config['catalogue'] = 'messages'; //default catalog
|
|
|
163 |
* $config['autosave'] = 'true'; //save untranslated message
|
|
|
164 |
* $config['cache'] = 'true'; //cache translated message
|
|
|
165 |
* $config['marker'] = '@@'; // surround untranslated text with '@@'
|
|
|
166 |
* </code>
|
|
|
167 |
* Throws exception is source is not found.
|
|
|
168 |
* @param TMap configuration options
|
|
|
169 |
*/
|
|
|
170 |
protected function setTranslationConfiguration(TMap $config)
|
|
|
171 |
{
|
|
|
172 |
if($config['type'] == 'XLIFF' || $config['type'] == 'gettext')
|
|
|
173 |
{
|
|
|
174 |
if($config['source'])
|
|
|
175 |
{
|
|
|
176 |
$config['source'] = Prado::getPathOfNamespace($config['source']);
|
|
|
177 |
if(!is_dir($config['source']))
|
|
|
178 |
{
|
|
|
179 |
if(@mkdir($config['source'])===false)
|
|
|
180 |
throw new TConfigurationException('globalization_source_path_failed',
|
|
|
181 |
$config['source']);
|
|
|
182 |
chmod($config['source'], PRADO_CHMOD); //make it deletable
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
else
|
|
|
186 |
{
|
|
|
187 |
throw new TConfigurationException("invalid source dir '{$config['source']}'");
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
if($config['cache'])
|
|
|
191 |
{
|
|
|
192 |
$config['cache'] = $this->getApplication()->getRunTimePath().'/i18n';
|
|
|
193 |
if(!is_dir($config['cache']))
|
|
|
194 |
{
|
|
|
195 |
if(@mkdir($config['cache'])===false)
|
|
|
196 |
throw new TConfigurationException('globalization_cache_path_failed',
|
|
|
197 |
$config['cache']);
|
|
|
198 |
chmod($config['cache'], PRADO_CHMOD); //make it deletable
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
$this->_translation = $config;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* @return string current translation catalogue.
|
|
|
206 |
*/
|
|
|
207 |
public function getTranslationCatalogue()
|
|
|
208 |
{
|
|
|
209 |
return $this->_translation['catalogue'];
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
/**
|
|
|
213 |
* @param string update the translation catalogue.
|
|
|
214 |
*/
|
|
|
215 |
public function setTranslationCatalogue($value)
|
|
|
216 |
{
|
|
|
217 |
$this->_translation['catalogue'] = $value;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Gets all the variants of a specific culture. If the parameter
|
|
|
222 |
* $culture is null, the current culture is used.
|
|
|
223 |
* @param string $culture the Culture string
|
|
|
224 |
* @return array variants of the culture.
|
|
|
225 |
*/
|
|
|
226 |
public function getCultureVariants($culture=null)
|
|
|
227 |
{
|
|
|
228 |
if(is_null($culture)) $culture = $this->getCulture();
|
|
|
229 |
$variants = explode('_', $culture);
|
|
|
230 |
$result = array();
|
|
|
231 |
for(; count($variants) > 0; array_pop($variants))
|
|
|
232 |
$result[] = implode('_', $variants);
|
|
|
233 |
return $result;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Returns a list of possible localized files. Example
|
|
|
238 |
* <code>
|
|
|
239 |
* $files = $app->getLocalizedResource("path/to/Home.page","en_US");
|
|
|
240 |
* </code>
|
|
|
241 |
* will return
|
|
|
242 |
* <pre>
|
|
|
243 |
* array
|
|
|
244 |
* 0 => 'path/to/en_US/Home.page'
|
|
|
245 |
* 1 => 'path/to/en/Home.page'
|
|
|
246 |
* 2 => 'path/to/Home.en_US.page'
|
|
|
247 |
* 3 => 'path/to/Home.en.page'
|
|
|
248 |
* 4 => 'path/to/Home.page'
|
|
|
249 |
* </pre>
|
|
|
250 |
* Note that you still need to verify the existance of these files.
|
|
|
251 |
* @param string filename
|
|
|
252 |
* @param string culture string, null to use current culture
|
|
|
253 |
* @return array list of possible localized resource files.
|
|
|
254 |
*/
|
|
|
255 |
public function getLocalizedResource($file,$culture=null)
|
|
|
256 |
{
|
|
|
257 |
$files = array();
|
|
|
258 |
$variants = $this->getCultureVariants($culture);
|
|
|
259 |
$path = pathinfo($file);
|
|
|
260 |
foreach($variants as $variant)
|
|
|
261 |
$files[] = $path['dirname'].DIRECTORY_SEPARATOR.$variant.DIRECTORY_SEPARATOR.$path['basename'];
|
|
|
262 |
$filename = substr($path['basename'],0,strrpos($path['basename'],'.'));
|
|
|
263 |
foreach($variants as $variant)
|
|
|
264 |
$files[] = $path['dirname'].DIRECTORY_SEPARATOR.$filename.'.'.$variant.'.'.$path['extension'];
|
|
|
265 |
$files[] = $file;
|
|
|
266 |
return $files;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
?>
|