Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TTranslate, I18N translation component.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TTranslate.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.I18N
11
 */
12
 
13
/**
14
 * Get the parent control class.
15
 */
16
Prado::using('System.I18N.TI18NControl');
17
 
18
/**
19
 * TTranslate class.
20
 *
21
 * This component performs message/string translation. The translation
22
 * source is set in the TGlobalization handler. The following example
23
 * demonstrated a simple message translation.
24
 * <code>
25
 * <com:TTranslate Text="Goodbye" />
26
 * </code>
27
 *
28
 * Depending on the culture set on the page, the phrase "Goodbye" will
29
 * be translated.
30
 *
31
 * The {@link getParameters Parameters} property can be use to add name values pairs for
32
 * substitution. Substrings enclosed with "{" and "}" in the translation message are consider as the
33
 * parameter names during substitution lookup. The following example will substitute the substring
34
 * "{time}" with the value of the parameter attribute "Parameters.time=<%= time() %>. Note that
35
 * the value of the parameter named "time" is evaluated.
36
 * <code>
37
 * <com:TTranslate Parameters.time=<%= time() %> >
38
 *   The unix-time is "{time}".
39
 * </com:TTranslate>
40
 * </code>
41
 *
42
 * More complex string substitution can be applied using the
43
 * TTranslateParameter component.
44
 *
45
 * Namespace: System.I18N
46
 *
47
 * Properties
48
 * - <b>Text</b>, string,
49
 *   <br>Gets or sets the string to translate.
50
 * - <b>Catalogue</b>, string,
51
 *   <br>Gets or sets the catalogue for message translation. The
52
 *    default catalogue can be set by the @Page directive.
53
 * - <b>Key</b>, string,
54
 *   <br>Gets or sets the key used to message look up.
55
 * - <b>Trim</b>, boolean,
56
 *   <br>Gets or sets an option to trim the contents.
57
 *   Default is to trim the contents.
58
 *
59
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
60
 * @version v1.0, last update on Fri Dec 24 21:38:49 EST 2004
61
 * @package System.I18N
62
 */
63
class TTranslate extends TI18NControl
64
{
65
	/**
66
	 * @return string the text to be localized/translated.
67
	 */
68
	public function getText()
69
	{
70
		return $this->getViewState('Text','');
71
	}
72
 
73
	/**
74
	 * Sets the text for localization.
75
	 * @param string the text for translation.
76
	 */
77
	public function setText($value)
78
	{
79
		$this->setViewState('Text',$value,'');
80
	}
81
 
82
	/**
83
	 * Set the key for message lookup.
84
	 * @param string key
85
	 */
86
	public function setKey($value)
87
	{
88
		$this->setViewState('Key',$value,'');
89
	}
90
 
91
	/**
92
	 * Get the key for message lookup.
93
	 * @return string key
94
	 */
95
	public function getKey()
96
	{
97
		return $this->getViewState('Key','');
98
	}
99
 
100
	/**
101
	 * Get the message catalogue.
102
	 * @return string catalogue.
103
	 */
104
	public function getCatalogue()
105
	{
106
		return $this->getViewState('Catalogue','');
107
	}
108
 
109
	/**
110
	 * Set the message catalogue.
111
	 * @param string catalogue.
112
	 */
113
	public function setCatalogue($value)
114
	{
115
		$this->setViewState('Catalogue',$value,'');
116
	}
117
 
118
	/**
119
	 * Set the option to trim the contents.
120
	 * @param boolean trim or not.
121
	 */
122
	public function setTrim($value)
123
	{
124
		$this->setViewState('Trim',TPropertyValue::ensureBoolean($value),true);
125
	}
126
 
127
	/**
128
	 * Trim the content or not.
129
	 * @return boolean trim or not.
130
	 */
131
	public function getTrim()
132
	{
133
		return $this->getViewState('Trim',true);
134
	}
135
 
136
	/**
137
	 * Returns the list of custom parameters.
138
	 * Custom parameters are name-value pairs that may subsititute translation
139
	 * place holders during rendering.
140
	 * @return TAttributeCollection the list of custom parameters
141
	 */
142
	public function getParameters()
143
	{
144
		if($parameters=$this->getViewState('Parameters',null))
145
			return $parameters;
146
		else
147
		{
148
			$parameters=new TAttributeCollection;
149
			$parameters->setCaseSensitive(true);
150
			$this->setViewState('Parameters',$parameters,null);
151
			return $parameters;
152
		}
153
	}
154
 
155
	/**
156
	 * @return boolean whether the named parameter exists
157
	 */
158
	public function hasParameter($name)
159
	{
160
		if($parameters=$this->getViewState('Parameters',null))
161
			return $parameters->contains($name);
162
		else
163
			return false;
164
	}
165
 
166
	/**
167
	 * @return string parameter value, null if parameter does not exist
168
	 */
169
	public function getParameter($name)
170
	{
171
		if($parameters=$this->getViewState('Parameters',null))
172
			return $parameters->itemAt($name);
173
		else
174
			return null;
175
	}
176
 
177
	/**
178
	 * @param string parameter name
179
	 * @param string value of the parameter
180
	 */
181
	public function setParameter($name,$value)
182
	{
183
		$this->getParameters()->add($name,$value);
184
	}
185
 
186
	/**
187
	 * Removes the named parameter.
188
	 * @param string the name of the parameter to be removed.
189
	 * @return string parameter value removed, null if parameter does not exist.
190
	 */
191
	public function removeParameter($name)
192
	{
193
		if($parameters=$this->getViewState('Parameters',null))
194
			return $parameters->remove($name);
195
		else
196
			return null;
197
	}
198
 
199
	/**
200
	 * renders the translated string.
201
	 */
202
	public function render($writer)
203
	{
204
		$textWriter=new TTextWriter;
205
		$htmlWriter=new THtmlWriter($textWriter);
206
		$subs = array();
207
		foreach($this->getParameters() as $key => $value)
208
			$subs['{'.$key.'}'] = $value;
209
		foreach($this->getControls() as $control)
210
		{
211
			if($control instanceof TTranslateParameter)
212
				$subs['{'.$control->getKey().'}'] = $control->getParameter();
213
			elseif($control instanceof TControl)
214
				$control->render($htmlWriter);
215
			elseif(is_string($control))
216
				$textWriter->write($control);
217
		}
218
 
219
		$text = $this->getText();
220
		if(strlen($text)==0)
221
			$text = $textWriter->flush();
222
		if($this->getTrim())
223
			$text = trim($text);
224
 
225
		$writer->write($this->translateText($text, $subs));
226
	}
227
 
228
	/**
229
	 * Translates the text with subsititution.
230
	 * @param string text for translation
231
	 * @param array list of substitutions
232
	 * @return string translated text
233
	 */
234
	protected function translateText($text, $subs)
235
	{
236
		$app = $this->getApplication()->getGlobalization();
237
 
238
		//no translation handler provided
239
		if(is_null($config = $app->getTranslationConfiguration()))
240
			return strtr($text, $subs);
241
 
242
		$catalogue = $this->getCatalogue();
243
		if(empty($catalogue) && isset($config['catalogue']))
244
			$catalogue = $config['catalogue'];
245
		if (empty($catalogue)) $catalogue='messages';
246
		Translation::init($catalogue);
247
 
248
		$key = $this->getKey();
249
		if(!empty($key)) $text = $key;
250
 
251
		//translate it
252
		return Translation::formatter($catalogue)->format($text,
253
										$subs, $catalogue, $this->getCharset());
254
	}
255
}
256