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/TGlobalization.php | 199 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 framework/I18N/TGlobalization.php (limited to 'framework/I18N/TGlobalization.php') diff --git a/framework/I18N/TGlobalization.php b/framework/I18N/TGlobalization.php new file mode 100644 index 00000000..473fe315 --- /dev/null +++ b/framework/I18N/TGlobalization.php @@ -0,0 +1,199 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.I18N + */ + + +/** + * TGlobalization contains settings for Culture, Charset, ContentType + * and TranslationConfiguration. + * + * TGlobalization can be subclassed to change how the Culture, Charset + * are determined. See TGlobalizationAutoDetect for example of + * setting the Culture based on browser settings. + * + * @author Wei Zhuo + * @version $Revision: 1.66 $ $Date: ${DATE} ${TIME} $ + * @package System.I18N + * @since 3.0 + */ +class TGlobalization extends TModule +{ + /** + * Default character set is 'UTF-8'. + * @var string + */ + private $_defaultCharset = 'UTF-8'; + + /** + * Default culture is 'en'. + * @var string + */ + private $_defaultCulture = 'en'; + + /** + * Default content type is 'text/html' + * @var ${type} + */ + private $_defaultContentType = 'text/html'; + + /** + * Translation source parameters. + * @var TMap + */ + private $_translation; + + /** + * The current charset. + * @var string + */ + public $Charset='UTF-8'; + + /** + * The current culture. + * @var string + */ + public $Culture='en'; + + /** + * The content type for the http header + * @var string + */ + public $ContentType='text/html'; + + /** + * Initialize the Culture and Charset for this application. + * You should override this method if you want a different way of + * setting the Culture and/or Charset for your application. + * If you override this method, call parent::init($xml) first. + * @param TXmlElement application configuration + */ + public function init($xml) + { + $this->Culture = str_replace('-','_',$this->Culture); + $this->_defaultContentType = $this->ContentType; + $this->_defaultCharset = $this->Charset; + $this->_defaultCulture = $this->Culture; + + $config = $xml->getElementByTagName('translation')->getAttributes(); + $this->setTranslationConfiguration($config); + $this->getApplication()->setGlobalization($this); + } + + /** + * @return TMap translation source configuration. + */ + public function getTranslationConfiguration() + { + return $this->_translation; + } + + /** + * Sets the translation configuration. Example configuration: + * + * $config['type'] = 'XLIFF'; //XLIFF, gettext, mysql or sqlite + * $config['source'] = 'Path.to.directory'; //or database connection string + * $config['catalogue'] = 'messages'; //default catalog + * $config['autosave'] = 'true'; //save untranslated message + * $config['cache'] = 'true'; //cache translated message + * + * Throws exception is source is not found. + * @param TMap configuration options + * @return ${return} + */ + protected function setTranslationConfiguration(TMap $config) + { + if($config['type'] == 'XLIFF' || $config['type'] == 'gettext') + { + $config['source'] = Prado::getPathOfNamespace($config['source']); + if(!is_dir($config['source'])) + throw new TException("invalid source dir '{$config['source']}'"); + } + if($config['cache']) + $config['cache'] = $this->getApplication()->getRunTimePath().'/i18n'; + $this->_translation = $config; + } + + /** + * @return string default charset set in application.xml + */ + public function getDefaultCharset() + { + return $this->_defaultCharset; + } + + /** + * @return string default culture set in application.xml + */ + public function getDefaultCulture() + { + return $this->_defaultCulture; + } + + /** + * @return string default content-type set in application.xml + */ + public function getDefaultContentType() + { + return $this->_defaultContentType; + } + + /** + * Gets all the variants of a specific culture. If the parameter + * $culture is null, the current culture is used. + * @param string $culture the Culture string + * @return array variants of the culture. + */ + public function getCultureVariants($culture=null) + { + if(is_null($culture)) $culture = $this->Culture; + $variants = explode('_', $culture); + $result = array(); + for(; count($variants) > 0; array_pop($variants)) + $result[] = implode('_', $variants); + return $result; + } + + /** + * Returns a list of possible localized files. Example + * + * $files = $app->getLocalizedResource("path/to/Home.page","en_US"); + * + * will return + *
+	 * array
+	 *   0 => 'path/to/en_US/Home.page'
+	 *   1 => 'path/to/en/Home.page'
+	 *   2 => 'path/to/Home.en_US.page'
+	 *   3 => 'path/to/Home.en.page'
+	 *   4 => 'path/to/Home.page'
+	 * 
+ * Note that you still need to verify the existance of these files. + * @param string filename + * @param string culture string, null to use current culture + * @return array list of possible localized resource files. + */ + public function getLocalizedResource($file,$culture=null) + { + $files = array(); + $variants = $this->getCultureVariants($culture); + $path = pathinfo($file); + foreach($variants as $variant) + $files[] = $path['dirname'].'/'.$variant.'/'.$path['basename']; + $filename = substr($path['basename'],0,strrpos($path['basename'],'.')); + foreach($variants as $variant) + $files[] = $path['dirname'].'/'.$filename.'.'.$variant.'.'.$path['extension']; + $files[] = $file; + return $files; + } + +} + +?> \ No newline at end of file -- cgit v1.2.3