Initial implementation of lang class that prefers PHP to XML

This commit is contained in:
Kijin Sung 2016-01-27 16:26:06 +09:00
parent 8ae2dba0b9
commit f68e6118bc
3 changed files with 219 additions and 132 deletions

View file

@ -337,8 +337,10 @@ class Context
self::set('lang_supported', $lang_supported);
self::setLangType($this->lang_type);
// load module module's language file according to language setting
self::loadLang(_XE_PATH_ . 'modules/module/lang');
// Load languages
$this->lang = Rhymix\Framework\Lang::getInstance($this->lang_type);
$this->lang->addDirectory(RX_BASEDIR . 'common/lang');
$this->lang->addDirectory(RX_BASEDIR . 'modules/module/lang');
// set session handler
if(self::isInstalled() && $this->db_info->use_db_session == 'Y')
@ -405,9 +407,6 @@ class Context
}
}
// load common language file
self::loadLang(_XE_PATH_ . 'common/lang/');
// check if using rewrite module
$this->allow_rewrite = ($this->db_info->use_rewrite == 'Y' ? TRUE : FALSE);
@ -920,118 +919,7 @@ class Context
*/
public static function loadLang($path)
{
global $lang;
if(!self::$_instance->lang_type)
{
return;
}
if(!is_object($lang))
{
$lang = new stdClass;
}
if(!($filename = self::$_instance->_loadXmlLang($path)))
{
$filename = self::$_instance->_loadPhpLang($path);
}
if(!is_array(self::$_instance->loaded_lang_files))
{
self::$_instance->loaded_lang_files = array();
}
if(in_array($filename, self::$_instance->loaded_lang_files))
{
return;
}
if($filename && is_readable($filename))
{
self::$_instance->loaded_lang_files[] = $filename;
include($filename);
}
else
{
self::$_instance->_evalxmlLang($path);
}
}
/**
* Evaluation of xml language file
*
* @param string Path of the language file
* @return void
*/
public function _evalxmlLang($path)
{
global $lang;
if(!$path) return;
$_path = 'eval://' . $path;
if(in_array($_path, $this->loaded_lang_files))
{
return;
}
if(substr_compare($path, '/', -1) !== 0)
{
$path .= '/';
}
$oXmlLangParser = new XmlLangParser($path . 'lang.xml', $this->lang_type);
$content = $oXmlLangParser->getCompileContent();
if($content)
{
$this->loaded_lang_files[] = $_path;
eval($content);
}
}
/**
* Load language file of xml type
*
* @param string $path Path of the language file
* @return string file name
*/
public function _loadXmlLang($path)
{
if(!$path) return;
$oXmlLangParser = new XmlLangParser($path . ((substr_compare($path, '/', -1) !== 0) ? '/' : '') . 'lang.xml', $this->lang_type);
return $oXmlLangParser->compile();
}
/**
* Load language file of php type
*
* @param string $path Path of the language file
* @return string file name
*/
public function _loadPhpLang($path)
{
if(!$path) return;
if(substr_compare($path, '/', -1) !== 0)
{
$path .= '/';
}
$path_tpl = $path . '%s.lang.php';
$file = sprintf($path_tpl, $this->lang_type);
$langs = array('ko', 'en'); // this will be configurable.
while(!is_readable($file) && $langs[0])
{
$file = sprintf($path_tpl, array_shift($langs));
}
if(!is_readable($file))
{
return FALSE;
}
return $file;
return self::$_instance->lang->addDirectory($path);
}
/**
@ -1069,15 +957,8 @@ class Context
*/
public static function getLang($code)
{
if(!$code)
{
return;
}
if($GLOBALS['lang']->{$code})
{
return $GLOBALS['lang']->{$code};
}
return $code;
$lang = self::$_instance->lang;
return isset($lang->{$code}) ? $lang->{$code} : $code;
}
/**
@ -1089,11 +970,7 @@ class Context
*/
public static function setLang($code, $val)
{
if(!isset($GLOBALS['lang']))
{
$GLOBALS['lang'] = new stdClass;
}
$GLOBALS['lang']->{$code} = $val;
self::$_instance->lang->{$code} = $val;
}
/**

View file

@ -1132,7 +1132,10 @@ class ModuleHandler extends Handler
}
// Load language files for the class
if($module !== 'module')
{
Context::loadLang($class_path . 'lang');
}
if($extend_module)
{
Context::loadLang(ModuleHandler::getModulePath($parent_module) . 'lang');

207
common/framework/lang.php Normal file
View file

@ -0,0 +1,207 @@
<?php
namespace Rhymix\Framework;
/**
* The language class.
*/
class Lang
{
/**
* Instances are stored here.
*/
protected static $_instances = [];
/**
* Configuration.
*/
protected $_language;
protected $_loaded_directories = [];
protected $_loaded_files = [];
/**
* This method returns the cached instance of a language.
*
* @param string $language
* @return object
*/
public static function getInstance($language)
{
if (!isset(self::$_instances[$language]))
{
self::$_instances[$language] = new self($language);
}
return self::$_instances[$language];
}
/**
* The constructor should not be called from outside.
*
* @param string $language
*/
protected function __construct($language)
{
$this->_language = strtolower(preg_replace('/[^a-z0-9_-]/i', '', $language));
}
/**
* Add a directory to load translations from.
*
* @param string $dir
* @return bool
*/
public function addDirectory($dir)
{
// Do not load the same directory twice.
$dir = rtrim($dir, '/');
if (in_array($dir, $this->_loaded_directories))
{
return true;
}
// Alias $this to $lang.
$lang = $this;
// Check if there is a PHP lang file.
if (file_exists($filename = $dir . '/' . $this->_language . '.php'))
{
$this->_loaded_directories[] = $dir;
$this->_loaded_files[] = $filename;
include $filename;
return true;
}
elseif (file_exists($filename = $dir . '/' . $this->_language . '.lang.php'))
{
$this->_loaded_directories[] = $dir;
$this->_loaded_files[] = $filename;
include $filename;
return true;
}
elseif (($hyphen = strpos($this->_language, '-')) !== false)
{
if (file_exists($filename = $dir . '/' . substr($this->_language, 0, $hyphen) . '.php'))
{
$this->_loaded_directories[] = $dir;
$this->_loaded_files[] = $filename;
include $filename;
return true;
}
elseif (file_exists($filename = $dir . '/' . substr($this->_language, 0, $hyphen) . '.lang.php'))
{
$this->_loaded_directories[] = $dir;
$this->_loaded_files[] = $filename;
include $filename;
return true;
}
}
// Check if there is a XML lang file.
if (file_exists($filename = $dir . '/lang.xml'))
{
$this->_loaded_directories[] = $dir;
$this->_loaded_files[] = $filename;
$compiled_filename = $this->compileXMLtoPHP($filename, $this->_language);
if ($compiled_filename !== false)
{
include $compiled_filename;
return true;
}
}
// Return false if no suitable lang file is found.
return false;
}
/**
* Compile XE-compatible XML lang files into PHP.
*
* @param string $filename
* @param string $language
* @return string|false
*/
public function compileXMLtoPHP($filename, $language)
{
// Check if the cache file already exists.
$cache_filename = RX_BASEDIR . 'files/cache/lang/' . md5($filename) . '.' . $language . '.php';
if (file_exists($cache_filename) && filemtime($cache_filename) > filemtime($filename))
{
return $cache_filename;
}
// Load the XML lang file.
$xml = @simplexml_load_file($filename);
if ($xml === false)
{
\FileHandler::writeFile($cache_filename, '');
return false;
}
// Convert XML to a PHP array.
$lang = array();
foreach ($xml->item as $item)
{
$name = strval($item['name']);
if (strval($item['type']) === 'array')
{
$lang[$name] = array();
foreach ($item->item as $subitem)
{
$subname = strval($subitem['name']);
foreach ($subitem->value as $value)
{
$attribs = $value->attributes('xml', true);
if (strtolower($attribs['lang']) === $language)
{
$lang[$name][$subname] = strval($value);
break;
}
}
}
}
else
{
foreach ($item->value as $value)
{
$attribs = $value->attributes('xml', true);
if (strtolower($attribs['lang']) === $language)
{
$lang[$name] = strval($value);
break;
}
}
}
}
unset($xml);
// Save the array as a cache file.
$buff = "<?php\n";
foreach ($lang as $key => $value)
{
$buff .= '$lang->' . $key . ' = ' . var_export($value, true) . ";\n";
}
\FileHandler::writeFile($cache_filename, $buff);
return $cache_filename;
}
/**
* Magic method for translations with arguments.
*
* @param string $key
* @param mixed $args
* @return string|null
*/
public function __call($key, $args)
{
// Remove a colon from the beginning of the string.
if ($key !== '' && $key[0] === ':') $key = substr($key, 1);
// If the string does not have a translation, return it verbatim.
if (!isset($this->{$key})) return $key;
// If there are no arguments, return the translation.
if (!count($args)) return $this->{$key};
// If there are arguments, interpolate them into the translation and return the result.
return vsprintf($this->{$key}, $args);
}
}