Home > Backend Development > PHP Tutorial > PHP Chinese and English language conversion code_PHP tutorial

PHP Chinese and English language conversion code_PHP tutorial

WBOY
Release: 2016-07-21 15:25:21
Original
856 people have browsed it

At first I thought of making it in the form of an XML document, which would be easy to operate. I just saw that XML is not very efficient
Furthermore, there are different templates, but there is a small problem. Some words such as time prompts are uncertain, and may be minute or day. It is also possible to add s to the plural number
Well, make it into an array, but the array has to be made into a variable in the php file, which is difficult to expand (this is what I know and think)
Finally, it is made into txt In the form of a text file, I was also worried about the efficiency. I opened the file, searched for strings, and intercepted strings. Fortunately, I finally ran it. It took about 0.0004 seconds on an average machine. This surprised me. I thought it would be very slow. After all, it takes Called multiple times.
Okay, enter the code

Copy the code The code is as follows:

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);
}
}

strpos(); is to find the position where the string first appears, such as 'wo ' In 'hello world', the return value is 6
substr(); is a part of the intercepted string
Next is the code added during debugging
Copy Code The code is as follows:

$la = language::getObject();
$la->type = 'unit';
$la-> ;lto('min');
echo '
';
$la->lto('hello');

lto(here is Translated in English); The content format of the
unit.txt file is
hello-hello min-small minute-minutes-minutes

$special is designed to be global and it is expected that lto will be called more than once () , loading files repeatedly is a waste of performance.
$type is designed to be public to take into account the efficiency of loaded files. Sometimes there is no need to display files from a few days ago, so it is better to separate them by usage type. For example, there is menu.txt specifically responsible for menu translation. There are also translated txt texts specifically for operations such as deletion and collection. In this way, the text to be loaded can be freely set
The language can also be freely set.
Okay, the program can be improved, I did not set $lan according to the client language in the http request

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324184.htmlTechArticleAt first I thought of making it into an XML document, which is easy to operate. I just saw that XML is not very efficient. Furthermore, there are different templates. But there is a small problem. Some words such as time...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template