Multi-language Website development focuses on solving problems between languages.
So how to solve this problem? It can be divided into three steps:
1. Multi-language page
2. Multi-language database
3. Unified user access language
1. Multi-language page
Required Questions to consider:
A. When users log in, characters are automatically recognized and different language packages are called?
B. When the user switches between different languages, different language packages are called?
C.Directory structure after adding multiple languages?
Multi-language page means multi-lingualization of appearance. Static language package can be used here.
The language directory should be included during design, with separate subdirectories for different languages.
Such as English language/en, Simplified Chinese language/gb, Traditional Chinese language/b5 (can be extended to other languages)
Each directory contains the language version of each page. When selecting a language version, you can call the language pack of the corresponding version.
The following is an example code
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 string first appears, such as 'wo' in 'hello world', the return value is 6
substr(); is part of the intercepted string
Next is the code added during 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
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. If it is repeated Loading files 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 these a few days ago, so it is better to separate them according to the type of use. 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.
The above is the detailed content of Detailed explanation of php Chinese and English language conversion class. For more information, please follow other related articles on the PHP Chinese website!