summaryrefslogtreecommitdiff
path: root/framework/I18N/Translation.php
blob: ba8ec0a58ea6ac479a49922cb6787de8754e7dcb (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
<?php
/**
 * Translation, static.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  $Date: $
 * @package System.I18N
 */

 /**
 * Get the MessageFormat class.
 */
Prado::using('System.I18N.core.MessageFormat');


/**
 * Translation class.
 *
 * Provides translation using a static MessageFormatter.
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Tue Dec 28 11:54:48 EST 2004
 * @package System.I18N
 */
class Translation extends TComponent
{
	/**
	 * The string formatter. This is a class static variable.
	 * @var MessageFormat
	 */
	protected static $formatter;

	/**
	 * Initialize the TTranslate translation components
	 */
	public static function init()
	{
		//initialized the default class wide formatter
		if(is_null(self::$formatter))
		{
			$app = Prado::getApplication()->getGlobalization();
			$config = $app->getTranslationConfiguration();
			$source = MessageSource::factory($config['type'],
											$config['source'],
											$config['filename']);

			$source->setCulture($app->getCulture());

			if($config['cache'])
				$source->setCache(new MessageCache($config['cache']));

			self::$formatter = new MessageFormat($source, $app->getCharset());

			//save the message on end request
			Prado::getApplication()->attachEventHandler(
				'OnEndRequest', array('Translation', 'saveMessages'));
		}
	}

	/**
	 * Get the static formatter from this component.
	 * @return MessageFormat formattter.
	 * @see localize()
	 */
	public static function formatter()
	{
		return self::$formatter;
	}

	/**
	 * Save untranslated messages to the catalogue.
	 */
	public static function saveMessages()
	{
		static $onceonly = true;

		if($onceonly && !is_null($formatter = self::$formatter))
		{
			$app = Prado::getApplication()->getGlobalization();
			$config = $app->getTranslationConfiguration();
			if(isset($config['autosave']))
			{
				$formatter->getSource()->setCulture($app->getCulture());
				$formatter->getSource()->save($config['catalogue']);
			}
			$onceonly = false;
		}
	}
}

/**
 * Localize a text to the locale/culture specified in the globalization handler.
 * @param string text to be localized.
 * @param array a set of parameters to substitute.
 * @param string a different catalogue to find the localize text.
 * @param string the input AND output charset.
 * @return string localized text.
 * @see TTranslate::formatter()
 * @see TTranslate::init()
 */
function localize($text, $parameters=array(), $catalogue=null, $charset=null)
{

	$app = Prado::getApplication()->getGlobalization();

	$params = array();
	foreach($parameters as $key => $value)
		$params['{'.$key.'}'] = $value;

	//no translation handler provided
	if(is_null($config = $app->getTranslationConfiguration()))
		return strtr($text, $params);

	Translation::init();

	if(empty($catalogue) && isset($config['catalogue']))
		$catalogue = $config['catalogue'];

	//globalization charset
	$appCharset = is_null($app) ? '' : $app->getCharset();

	//default charset
	$defaultCharset = (is_null($app)) ? 'UTF-8' : $app->getDefaultCharset();

	//fall back
	if(empty($charset)) $charset = $appCharset;
	if(empty($charset)) $charset = $defaultCharset;

	return Translation::formatter()->format($text,$params,$catalogue,$charset);
}

?>