Multilingual website Development, the focus is still in the solution of the problem between the language.
So how do we solve this problem? It's about three steps away:
1. Page Multi-lingual
2. Database Multi-language
3. Unified User Access language
1. Page Multi-lingual
Questions to consider:
A. When the user logs in, the characters are automatically recognized and the different language packs are called.
B. When the user switches different languages, call different language packs?
C. Add a multi-lingual directory structure?
Page multi-language is also the appearance of multi-language, here can adopt static language package way.
Design time should include the language directory, for different languages have a separate subdirectory.
such as English language/en, Simplified Chinese language/gb, Traditional Chinese language/b5 (can expand other languages)
Each directory contains a language version of each page. When you select a language version, you can call the appropriate version of the language pack.
Here is the instance code
Class Language {static $lanObject; public $type;//Unit, Dashboard, menu, other public $lan;//Language Private $spec Ial 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 ': $th Is->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 are English {if ($this->lan!== ' en ') 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 first occurrence of a string such as ' wo ' in ' Hello World ', the return value is 6
SUBSTR (); is part of the Intercept string
Next is the code that is added when debugging
$la = Language::getobject (); $la->type = ' unit '; $la->lto (' min '); Echo ' <br/> '; $la->lto (' hello ');
LTO (the English to be translated here);
The content format of the Unit.txt file is
hello-Hello min-small minute-minutes minutes-minutes
$special is designed to be global and it is thought that more than once will call LTO () if loading files repeatedly is too wasteful of performance.
$type Design for the public is to take into account the efficiency of the loaded files, and sometimes do not need to display these days ago, so it is better to separate these according to the type of use, such as the menu.txt is responsible for menu translation, but also specifically for the operation, such as delete, collection of txt text translation. This gives you the freedom to set the text to be loaded
Language can also be set freely.