在php中,如何实现中英文语言转换呢,思路可能有XML文档形式,但XML效率不怎样;又想到不同的模板,有些词汇比如时间提示是不确定,与可能是minute ,day。也有可能复数加 s
class language
{
static $lanObject;
public $type; // unit , dashboard , menu ,other
public $lan; // language
private $special; // The common in the file
private function __construct()
{
if( isset($_GET['hl']) || isset($_POST['hl']) )
{
switch( isset($_GET['hl'])?$_GET['hl']:$_POST['hl'] )
{
case 'en':
$this->lan = 'en';
case 'zh':
$this->lan = 'zh';
case 'all':
$this->lan = 'all';
default:
$this->error();
}
}
else
$this->lan = isset($_COOKIE['hl']) ? $_COOKIE['hl']:'zh';
}
public static function getObject()
{
if( !(self::$lanObject instanceof self) )
self::$lanObject = new language();
return self::$lanObject;
}
public function lto($key) //$key is English
{
if( $this->lan !== 'zh' )
return $key;
if( empty($this->special) ) // if the $special is null
{
if( isset($this->type) )
$this->special =
file_get_contents($this->type.'.txt');
else
return $key;
}
echo $this->search($key);
}
private function search($searchTozh) // PHP String
{
$key_start = strpos($this->special,$searchTozh);
$key_end = strpos($this->special,' ',$key_start);
$len_str = strlen($searchTozh);
$for_sub = $key_start + $len_str + 1;
return substr($this->special, $for_sub, $key_end - $for_sub);
}
}