Php code for conversion between Chinese and English languages. At first, it was easy to create an XML document. However, we can see that the XML efficiency is not very efficient. In addition, there is also a small problem. some words, such as the first thought of the XML document form, are easy to perform. I just saw that XML is not efficient.
In addition, there are different templates, but there is also a small problem, some words such as the time prompt are not sure, and may be minute, day. It is also possible to add s to the plural number.
Well, make it an array, but the array has to be made into a variable in the PHP file, and it is difficult to make some extensions (as I know)
The final form of a txt text file is also worried about this efficiency. open the file, search for the string, and intercept the string. Fortunately, it ran for a moment. Generally, the host is about 0.0004 seconds, this surprised me to think it would be very slow. after all, I had to call it multiple times.
Okay, go to 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 locate the position where the string appears for the first time. for example, in 'Hello world', the return value is 6.
Substr (); is part of the truncated string
The following is the code added during debugging.
The code is as follows:
$ La = language: getObject ();
$ La-> type = 'unit ';
$ La-> lto ('min ');
Echo'
';
$ La-> lto ('Hello ');
Lto (the English translation here );
The content format of the unit.txt file is
Hello-min-minute-min Utes-min
$ Special is designed globally to call lto () more than once. it is a waste of performance to load files repeatedly.
For operations, such as deleting and collecting translated txt text. In this way, you can freely set the text to be loaded.
The language can also be set freely.
Well, the program can be improved. I didn't set $ lan according to the client language in the http request.
Bytes. We can only see that the XML efficiency is not very good. In addition, there are different templates, but there is also a small problem. some words, such as time...