summaryrefslogtreecommitdiff
path: root/framework/I18N/core/MessageFormat.php
blob: 7af6deb13ff6b8278d75d4a16be76edcb6449198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php

/**
 * MessageFormat class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
 *
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: 1.5 $  $Date: 2005/08/27 03:21:12 $
 * @package System.I18N.core
 */

/**
 * Get the MessageSource classes.
 */
require_once(dirname(__FILE__).'/MessageSource.php');

/**
 * Get the encoding utilities
 */
require_once(dirname(__FILE__).'/util.php');

/**
 * MessageFormat class.
 * 
 * Format a message, that is, for a particular message find the 
 * translated message. The following is an example using 
 * a SQLite database to store the translation message. 
 * Create a new message format instance and echo "Hello"
 * in simplified Chinese. This assumes that the world "Hello"
 * is translated in the database.
 *
 * <code>
 *  $source = MessageSource::factory('SQLite', 'sqlite://messages.db');
 *	$source->setCulture('zh_CN');
 *	$source->setCache(new MessageCache('./tmp'));
 *
 * 	$formatter = new MessageFormat($source); 
 * 	
 *	echo $formatter->format('Hello');
 * </code>
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Fri Dec 24 20:46:16 EST 2004
 * @package System.I18N.core
 */
class MessageFormat
{
	/**
	 * The message source.
	 * @var MessageSource 
	 */
	protected $source;
	
	/**
	 * A list of loaded message catalogues.
	 * @var array 
	 */
	protected $catagloues = array();
	
	/**
	 * The translation messages.
	 * @var array 
	 */
	protected $messages = array();
	
	/**
	 * A list of untranslated messages.
	 * @var array 
	 */
	protected $untranslated = array();
	
	/**
	 * The prefix and suffix to append to untranslated messages.
	 * @var array 
	 */
	protected $postscript = array('','');
	
	/**
	 * Set the default catalogue.
	 * @var string 
	 */
	public $Catalogue;

	/**
	 * Output encoding charset
	 * @var string
	 */
	protected $charset = 'UTF-8'; 

	/**
	 * Constructor.
	 * Create a new instance of MessageFormat using the messages
	 * from the supplied message source.
	 * @param MessageSource the source of translation messages.
	 * @param string charset for the message output.
	 */
	function __construct(IMessageSource $source, $charset='UTF-8')
	{
		$this->source = $source;	
		$this->setCharset($charset);
	}

	/** 
	 * Sets the charset for message output.
	 * @param string charset, default is UTF-8
	 */
	public function setCharset($charset)
	{
		$this->charset = $charset;
	}

	/**
	 * Gets the charset for message output. Default is UTF-8.
	 * @return string charset, default UTF-8
	 */
	public function getCharset()
	{
		return $this->charset;
	}
	
	/**
	 * Load the message from a particular catalogue. A listed
	 * loaded catalogues is kept to prevent reload of the same
	 * catalogue. The load catalogue messages are stored
	 * in the $this->message array.
	 * @param string message catalogue to load.
	 */
	protected function loadCatalogue($catalogue)
	{
		if(in_array($catalogue,$this->catagloues))
			return;
			
		if($this->source->load($catalogue))
		{
			$this->messages[$catalogue] = $this->source->read();
			$this->catagloues[] = $catalogue;						
		}
	}

	/**
	 * Format the string. That is, for a particular string find
	 * the corresponding translation. Variable subsitution is performed
	 * for the $args parameter. A different catalogue can be specified
	 * using the $catalogue parameter.
	 * The output charset is determined by $this->getCharset();
	 * @param string the string to translate.
	 * @param array a list of string to substitute.
	 * @param string get the translation from a particular message
	 * @param string charset, the input AND output charset
	 * catalogue.
	 * @return string translated string.
	 */
	public function format($string,$args=array(), $catalogue=null, $charset=null) 
	{
		if(empty($charset)) $charset = $this->getCharset();
		
		//force args as UTF-8
		foreach($args as $k => $v)
			$args[$k] = I18N_toUTF8($v, $charset);
		$s = $this->formatString(I18N_toUTF8($string, $charset),$args,$catalogue);
		return I18N_toEncoding($s, $charset);
	}

	/**
	 * Do string translation.
	 * @param string the string to translate.
	 * @param array a list of string to substitute.
	 * @param string get the translation from a particular message
	 * catalogue.
	 * @return string translated string.
	 */
	protected function formatString($string, $args=array(), $catalogue=null)
	{		
		if(empty($catalogue))
		{
			if(empty($this->Catalogue))
				$catalogue = 'messages';
			else 
				$catalogue = $this->Catalogue;
		}
				
		$this->loadCatalogue($catalogue);
		
		if(empty($args))
			$args = array();		
		
		foreach($this->messages[$catalogue] as $variant)
		{
			// foreach of the translation units
			foreach($variant as $source => $result)
			{ 
				// we found it, so return the target translation
				if($source == $string)
				{
					//check if it contains only strings.
					if(is_string($result))
						$target = $result;
					else
					{
						$target = $result[0];
					}
					//found, but untranslated
					if(empty($target))
					{
						return 	$this->postscript[0].
								strtr($string, $args).
								$this->postscript[1];		
					}
					else
						return strtr($target, $args);
				}
			}
		}
		
		// well we did not find the translation string.
		$this->source->append($string);
		
		return 	$this->postscript[0].
				strtr($string, $args).
				$this->postscript[1];
	}
	
	/**
	 * Get the message source.
	 * @return MessageSource 
	 */
	function getSource()
	{
		return $this->source;
	}
	
	/**
	 * Set the prefix and suffix to append to untranslated messages.
	 * e.g. $postscript=array('[T]','[/T]'); will output 
	 * "[T]Hello[/T]" if the translation for "Hello" can not be determined.
	 * @param array first element is the prefix, second element the suffix.
	 */
	function setUntranslatedPS($postscript)
	{
		if(is_array($postscript) && count($postscript)>=2)
		{
			$this->postscript[0] = $postscript[0];
			$this->postscript[1] = $postscript[1];
		}
	}
}