Thinkphp multi-language function _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Thinkphp supports multiple languages. This article mainly introduces thinkphp to implement multi-language functions (language packs). For more information, see section 1. in the config. add the following configuration code to php :? Phpr this article mainly introduces thinkphp to implement multi-language functions (language packs). For more information, see

1. add the following configuration code to config. php of Home (your project name: 'Configuration value ''lang _ SWITCH_ON '=> true, // enable the language pack function 'Lang _ AUTO_DETECT' => true, // the automatic detection language 'default _ lang' => 'zh-cn', // the DEFAULT language 'Lang _ list' => 'en-us, zh-cn, zh-tw ', // you must write the list of allowed languages 'Var _ language' => 'L', // Default LANGUAGE switch variable);?> 2. add a php file (tag. php), add the following code: Code: return array (// add the following line definition to 'app _ begin' => array ('checklang '); 3. put Extend/Behavior/CheckLangBehavior. class. copy the php file to Home/lib/Behavior/(the full version of the thinkphp package is available, if not, create your own) CheckLangBehavior. class. php code: the code is as follows: False, // The Language Pack function 'Lang _ AUTO_DETECT '=> true is disabled by default. // after the multi-language function is enabled, the function 'Lang _ list' => 'zh-cn' is effective ', // The list of languages that can be switched are separated by commas (,). 'Var _ language' => 'L', // Default LANGUAGE switch variables ); // The execution entry for behavior extension must be run public function run (& $ params) {// enable static cache $ this-> checkLanguage ();} /*** language check * Check the supported language of the browser and automatically load the language pack * @ access private * @ return void */private function checkLanguage () {// The Language Pack function is not enabled, if (! C ('Lang _ SWITCH_ON ') {return;} $ langSet = C ('default _ LANG '); // enable the language pack function // select if (C ('Lang _ AUTO_DETECT ') based on whether automatic detection is enabled ')) {if (isset ($ _ GET [C ('Var _ LANGUAGE ')]) {$ langSet =$ _ GET [C ('Var _ LANGUAGE')]; // set the language variable cookie ('think _ local', $ langSet, 3600);} elseif (cookie ('think _ Local') in the url ')) {// Obtain the last user's selection $ langSet = cookie ('think _ language');} elseif (isset ($ _ SERVER ['http _ ACCEPT_LANGUAGE ']) {// automatically detects browser language pre G_match ('/^ ([a-z \ d \-] +)/I', $ _ SERVER ['http _ ACCEPT_LANGUAGE '], $ matches ); $ langSet = $ matches [1]; cookie ('think _ color', $ langSet, 3600 );} if (false === stripos (C ('Lang _ list'), $ langSet )) {// invalid language parameter $ langSet = C ('default _ lang');} // defines 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 current module language pack 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. These are zh-cn en-us zh-tw. create a common. php file in each of these three folders, and write the following code in common. php: 'Welcome to use thinkphp',);?> The code is as follows: 'Welcome to thinkphp',);?> The code is as follows: 'Welcome to thinkphp',);?> 5. the code for creating a view index.html in The tpl/Index/folder 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 should be written to the language pack $ this-> display ();}. I think cakephp is doing better, you do not need to give a variable to every sentence.

Refer to 1. add the following configuration code in config. php of Home (your project name :? Phpr...

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.