| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* MessageFormat class file.
|
|
|
5 |
*
|
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
|
7 |
* it under the terms of the BSD License.
|
|
|
8 |
*
|
|
|
9 |
* Copyright(c) 2004 by Qiang Xue. All rights reserved.
|
|
|
10 |
*
|
|
|
11 |
* To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
|
|
|
12 |
* The latest version of PRADO can be obtained from:
|
|
|
13 |
* {@link http://prado.sourceforge.net/}
|
|
|
14 |
*
|
|
|
15 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
16 |
* @version $Revision: 1.5 $ $Date: 2005/08/27 03:21:12 $
|
|
|
17 |
* @package System.I18N.core
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Get the MessageSource classes.
|
|
|
22 |
*/
|
|
|
23 |
require_once(dirname(__FILE__).'/MessageSource.php');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Get the encoding utilities
|
|
|
27 |
*/
|
|
|
28 |
require_once(dirname(__FILE__).'/util.php');
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* MessageFormat class.
|
|
|
32 |
*
|
|
|
33 |
* Format a message, that is, for a particular message find the
|
|
|
34 |
* translated message. The following is an example using
|
|
|
35 |
* a SQLite database to store the translation message.
|
|
|
36 |
* Create a new message format instance and echo "Hello"
|
|
|
37 |
* in simplified Chinese. This assumes that the world "Hello"
|
|
|
38 |
* is translated in the database.
|
|
|
39 |
*
|
|
|
40 |
* <code>
|
|
|
41 |
* $source = MessageSource::factory('SQLite', 'sqlite://messages.db');
|
|
|
42 |
* $source->setCulture('zh_CN');
|
|
|
43 |
* $source->setCache(new MessageCache('./tmp'));
|
|
|
44 |
*
|
|
|
45 |
* $formatter = new MessageFormat($source);
|
|
|
46 |
*
|
|
|
47 |
* echo $formatter->format('Hello');
|
|
|
48 |
* </code>
|
|
|
49 |
*
|
|
|
50 |
* @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
51 |
* @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004
|
|
|
52 |
* @package System.I18N.core
|
|
|
53 |
*/
|
|
|
54 |
class MessageFormat
|
|
|
55 |
{
|
|
|
56 |
/**
|
|
|
57 |
* The message source.
|
|
|
58 |
* @var MessageSource
|
|
|
59 |
*/
|
|
|
60 |
protected $source;
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* A list of loaded message catalogues.
|
|
|
64 |
* @var array
|
|
|
65 |
*/
|
|
|
66 |
protected $catagloues = array();
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* The translation messages.
|
|
|
70 |
* @var array
|
|
|
71 |
*/
|
|
|
72 |
protected $messages = array();
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* A list of untranslated messages.
|
|
|
76 |
* @var array
|
|
|
77 |
*/
|
|
|
78 |
protected $untranslated = array();
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* The prefix and suffix to append to untranslated messages.
|
|
|
82 |
* @var array
|
|
|
83 |
*/
|
|
|
84 |
protected $postscript = array('','');
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Set the default catalogue.
|
|
|
88 |
* @var string
|
|
|
89 |
*/
|
|
|
90 |
public $Catalogue;
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Output encoding charset
|
|
|
94 |
* @var string
|
|
|
95 |
*/
|
|
|
96 |
protected $charset = 'UTF-8';
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Constructor.
|
|
|
100 |
* Create a new instance of MessageFormat using the messages
|
|
|
101 |
* from the supplied message source.
|
|
|
102 |
* @param MessageSource the source of translation messages.
|
|
|
103 |
* @param string charset for the message output.
|
|
|
104 |
*/
|
|
|
105 |
function __construct(IMessageSource $source, $charset='UTF-8')
|
|
|
106 |
{
|
|
|
107 |
$this->source = $source;
|
|
|
108 |
$this->setCharset($charset);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Sets the charset for message output.
|
|
|
113 |
* @param string charset, default is UTF-8
|
|
|
114 |
*/
|
|
|
115 |
public function setCharset($charset)
|
|
|
116 |
{
|
|
|
117 |
$this->charset = $charset;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Gets the charset for message output. Default is UTF-8.
|
|
|
122 |
* @return string charset, default UTF-8
|
|
|
123 |
*/
|
|
|
124 |
public function getCharset()
|
|
|
125 |
{
|
|
|
126 |
return $this->charset;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Load the message from a particular catalogue. A listed
|
|
|
131 |
* loaded catalogues is kept to prevent reload of the same
|
|
|
132 |
* catalogue. The load catalogue messages are stored
|
|
|
133 |
* in the $this->message array.
|
|
|
134 |
* @param string message catalogue to load.
|
|
|
135 |
*/
|
|
|
136 |
protected function loadCatalogue($catalogue)
|
|
|
137 |
{
|
|
|
138 |
if(in_array($catalogue,$this->catagloues))
|
|
|
139 |
return;
|
|
|
140 |
|
|
|
141 |
if($this->source->load($catalogue))
|
|
|
142 |
{
|
|
|
143 |
$this->messages[$catalogue] = $this->source->read();
|
|
|
144 |
$this->catagloues[] = $catalogue;
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Format the string. That is, for a particular string find
|
|
|
150 |
* the corresponding translation. Variable subsitution is performed
|
|
|
151 |
* for the $args parameter. A different catalogue can be specified
|
|
|
152 |
* using the $catalogue parameter.
|
|
|
153 |
* The output charset is determined by $this->getCharset();
|
|
|
154 |
* @param string the string to translate.
|
|
|
155 |
* @param array a list of string to substitute.
|
|
|
156 |
* @param string get the translation from a particular message
|
|
|
157 |
* @param string charset, the input AND output charset
|
|
|
158 |
* catalogue.
|
|
|
159 |
* @return string translated string.
|
|
|
160 |
*/
|
|
|
161 |
public function format($string,$args=array(), $catalogue=null, $charset=null)
|
|
|
162 |
{
|
|
|
163 |
if(empty($charset)) $charset = $this->getCharset();
|
|
|
164 |
|
|
|
165 |
//force args as UTF-8
|
|
|
166 |
foreach($args as $k => $v)
|
|
|
167 |
$args[$k] = I18N_toUTF8($v, $charset);
|
|
|
168 |
$s = $this->formatString(I18N_toUTF8($string, $charset),$args,$catalogue);
|
|
|
169 |
return I18N_toEncoding($s, $charset);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Do string translation.
|
|
|
174 |
* @param string the string to translate.
|
|
|
175 |
* @param array a list of string to substitute.
|
|
|
176 |
* @param string get the translation from a particular message
|
|
|
177 |
* catalogue.
|
|
|
178 |
* @return string translated string.
|
|
|
179 |
*/
|
|
|
180 |
protected function formatString($string, $args=array(), $catalogue=null)
|
|
|
181 |
{
|
|
|
182 |
if(empty($catalogue))
|
|
|
183 |
{
|
|
|
184 |
if(empty($this->Catalogue))
|
|
|
185 |
$catalogue = 'messages';
|
|
|
186 |
else
|
|
|
187 |
$catalogue = $this->Catalogue;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
$this->loadCatalogue($catalogue);
|
|
|
191 |
|
|
|
192 |
if(empty($args))
|
|
|
193 |
$args = array();
|
|
|
194 |
|
|
|
195 |
foreach($this->messages[$catalogue] as $variant)
|
|
|
196 |
{
|
|
|
197 |
// foreach of the translation units
|
|
|
198 |
foreach($variant as $source => $result)
|
|
|
199 |
{
|
|
|
200 |
// we found it, so return the target translation
|
|
|
201 |
if($source == $string)
|
|
|
202 |
{
|
|
|
203 |
//check if it contains only strings.
|
|
|
204 |
if(is_string($result))
|
|
|
205 |
$target = $result;
|
|
|
206 |
else
|
|
|
207 |
{
|
|
|
208 |
$target = $result[0];
|
|
|
209 |
}
|
|
|
210 |
//found, but untranslated
|
|
|
211 |
if(empty($target))
|
|
|
212 |
{
|
|
|
213 |
return $this->postscript[0].
|
|
|
214 |
strtr($string, $args).
|
|
|
215 |
$this->postscript[1];
|
|
|
216 |
}
|
|
|
217 |
else
|
|
|
218 |
return strtr($target, $args);
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// well we did not find the translation string.
|
|
|
224 |
$this->source->append($string);
|
|
|
225 |
|
|
|
226 |
return $this->postscript[0].
|
|
|
227 |
strtr($string, $args).
|
|
|
228 |
$this->postscript[1];
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Get the message source.
|
|
|
233 |
* @return MessageSource
|
|
|
234 |
*/
|
|
|
235 |
function getSource()
|
|
|
236 |
{
|
|
|
237 |
return $this->source;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Set the prefix and suffix to append to untranslated messages.
|
|
|
242 |
* e.g. $postscript=array('[T]','[/T]'); will output
|
|
|
243 |
* "[T]Hello[/T]" if the translation for "Hello" can not be determined.
|
|
|
244 |
* @param array first element is the prefix, second element the suffix.
|
|
|
245 |
*/
|
|
|
246 |
function setUntranslatedPS($postscript)
|
|
|
247 |
{
|
|
|
248 |
if(is_array($postscript) && count($postscript)>=2)
|
|
|
249 |
{
|
|
|
250 |
$this->postscript[0] = $postscript[0];
|
|
|
251 |
$this->postscript[1] = $postscript[1];
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
|