This article mainly introduces thinkphp to implement multi-language functions (language packs). For more information, see 1. Add the following configuration in config. php of Home (your project name ).
The Code is as follows:
Return array (
// 'Config maps '=> 'configuration value'
'Lang _ SWITCH_ON '=> true, // enable the Language Pack Function
'Lang _ AUTO_DETECT '=> true, // automatic language Detection
'Default _ lang' => 'zh-cn', // DEFAULT language
'Lang _ list' => 'en-us, zh-cn, zh-tw ', // a LIST of allowed languages must be written.
'Var _ language' => 'l', // default LANGUAGE switch variable
);
?>
2. Add a php file (tag. php) to the conf folder of Home and add the following code:
The Code is as follows:
Return array (
// Add the following line of definition.
'App _ begin' => array ('checklang ')
);
3. Copy the Extend/Behavior/CheckLangBehavior. class. php file to Home/lib/Behavior/(the full version of the thinkphp package is available. If not, create it on your own)
CheckLangBehavior. class. php code:
The Code is as follows:
Defined ('Think _ path') or exit ();
/**
* Language detection and automatic loading of language packs
* @ Category Extend
* @ Package Extend
* @ Subpackage Behavior
*/
Class CheckLangBehavior extends Behavior {
// The behavior Parameter definition (default value) can be overwritten in the project configuration
Protected $ options = array (
'Lang _ SWITCH_ON '=> false, // The Language Pack function is disabled by default.
'Lang _ AUTO_DETECT '=> true, // the automatic detection language is valid after the multi-language function is enabled.
'Lang _ list' => 'zh-cn', // The LIST of languages that can be switched is separated by commas (,).
'Var _ language' => 'l', // default LANGUAGE switch variable
);
// The execution entry for behavior extension must be run.
Public function run (& $ params ){
// Enable static Cache
$ This-> checkLanguage ();
}
/**
* Language check
* Check the Supported languages of the browser and automatically load the Language Pack.
* @ Access private
* @ Return void
*/
Private function checkLanguage (){
// If the Language Pack function is not enabled, the system simply loads the framework language file and returns the result directly.
If (! C ('lang _ SWITCH_ON ')){
Return;
}
$ LangSet = C ('default _ LANG ');
// Enable the Language Pack Function
// Set the language selection based on whether Automatic Detection is Enabled
If (C ('lang _ AUTO_DETECT ')){
If (isset ($ _ GET [C ('var _ language')]) {
$ LangSet = $ _ GET [C ('var _ language')]; // The LANGUAGE variable is set in the url.
Cookie ('Think _ Language', $ langSet, 3600 );
} Elseif (cookie ('Think _ language') {// obtain the selection of the last user
$ LangSet = cookie ('Think _ language ');
} Elseif (isset ($ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']) {// automatically detects the browser Language
Preg_match ('/^ ([a-z \ d \-] +)/I', $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE '], $ matches );
$ LangSet = $ matches [1];
Cookie ('Think _ Language', $ langSet, 3600 );
}
If (false === stripos (C ('lang _ list'), $ langSet) {// invalid language Parameter
$ LangSet = C ('default _ LANG ');
}
}
// Define the current language
Define ('lang _ set', strtolower ($ langSet ));
$ Group = '';
$ Path = (defined ('group _ name') & C ('app _ GROUP_MODE ') = 1 )? BASE_LIB_PATH. 'lang/'. LANG_SET.'/': LANG_PATH.LANG_SET .'/';
// Read the Project Public Language Pack
If (is_file (LANG_PATH.LANG_SET. '/common. php '))
L (include LANG_PATH.LANG_SET. '/common. php ');
// Read the Group Public Language Pack
If (defined ('group _ name ')){
If (C ('app _ GROUP_MODE ') = 1) {// independent group
$ File = $ path. 'common. php ';
} Else {// normal group
$ File = $ path. GROUP_NAME. '. php ';
$ Group = GROUP_NAME.C ('tmpl _ FILE_DEPR ');
}
If (is_file ($ file ))
L (include $ file );
}
// Read the language pack of the current Module
If (is_file ($ path. $ group. strtolower (MODULE_NAME). '. php '))
L (include $ path. $ group. strtolower (MODULE_NAME). '. php ');
}
}
4. Create three language folders under the lang folder in Home. Zh-cn en-us zh-tw,
Create a common. php file in each of the three folders,
Write in common. php
The Code is as follows:
Return array (
'Welcome '=> 'Welcome to use thinkphp ',
);
?>
The Code is as follows:
Return array (
'Welcome '=> 'Welcome to thinkphp ',
);
?>
The Code is as follows:
Return array (
'Welcome '=> 'Welcome to thinkphp ',
);
?>
5. Create the view index.html In the tpl/Index/folder.
The Code is as follows:
ThinkPHP example: multiple languages
Switch language: Simplified Chinese | traditional Chinese | English
{$ Think. lang. welcome}
Success!
To switch the background language, add L before each sentence, for example:
The Code is as follows:
Public function index (){
Print L ('add _ user_error '); // add_user_error is only a language variable. The specific language must be written in the Language Pack.
$ This-> display ();
}
In this regard, I think cakephp is better at doing so and does not need to give a variable to every sentence.