Php code for conversion between Chinese and English languages

Source: Internet
Author: User
I suddenly wanted to give it a try in Chinese and English, just to convert some frequently-used and regular words such as 'comments' and time units (several seconds ago. At first, it was easy to create an XML document. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.