From 4835704a04cf5aa5ec71a8aef902d54b9c6cae82 Mon Sep 17 00:00:00 2001 From: wei <> Date: Fri, 6 Jan 2006 04:42:44 +0000 Subject: Adding I18N support. --- framework/I18N/Translation.php | 144 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 framework/I18N/Translation.php (limited to 'framework/I18N/Translation.php') diff --git a/framework/I18N/Translation.php b/framework/I18N/Translation.php new file mode 100644 index 00000000..8b420bbf --- /dev/null +++ b/framework/I18N/Translation.php @@ -0,0 +1,144 @@ + + * @version $Revision: 1.9 $ $Date: 2005/12/17 06:11:28 $ + * @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 + * @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->Culture); + + if($config['cache']) + $source->setCache(new MessageCache($config['cache'])); + + self::$formatter = new MessageFormat($source, $app->Charset); + + //save the message on end request + Prado::getApplication()->attachEventHandler( + 'EndRequest', 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->Culture); + $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->Charset; + + //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); +} + +?> \ No newline at end of file -- cgit v1.2.3