thinkphp Implementing multilingual features _php Tutorials

Source: Internet
Author: User
This article mainly describes the thinkphp implementation of multilingual features (language Pack), the need for friends can refer to the following

1. Add the following configuration code in the config.php of Home (the project name you are taking) as follows: ' Config value ' ' lang_switch_on ' + true,//Turn on Language pack function ' Lang_auto_detect ' + true,//Auto Detect language ' default_lang ' = ' zh-cn ',//default language Words ' lang_list ' + ' en-us,zh-cn,zh-tw ',//must be written in the list of allowed languages ' var_language ' + ' l ',//default language toggle variable);? > 2.Home conf folder add a PHP file (tag.php), add the following code: The code is as follows: Return array (//Add the following line definition can ' app_begin ' = = Array (' Checklang ')); 3. Copy the extend/behavior/checklangbehavior.class.php file into the home/lib/behavior/(full version of the thinkphp package only, if not, please create your own) CheckLangBehavior.class.php Code: The code is as follows: False,//Turn off the Language pack feature ' lang_auto_detect ' = true,//auto-detect language after opening multi-language feature effective ' lang_list ' = ' zh-cn ',//Allow switch language list separated by commas ' VA R_language ' + ' l ',//default language toggle variable); The execution entry for the behavior extension must be run public function run (& $params) {//Open static cache $this->checklanguage ();}/** * language check * Check browser support language and auto-loader Words Package * @access private * @return void */Private Function CheckLanguage () {//Do not turn on the language pack feature, just load the framework language file directly back if (! C (' lang_switch_on ')) {return;} $langSet = C (' Default_lang '); The language pack feature is enabled//based on whether the auto-detect setting is enabled gets the language selection if (C (' Lang_auto_detect ')) {if (Isset ($_get[c (' Var_language ')])) {$langSet = $_get[c (' Var_language ') The language variable cookie is set in the];//url (' think_language ', $langSet, 3600); }elseif (Cookie (' Think_language ')) {//Get last user's Choice $langSet = Cookie (' Think_language ');} ElseIf (Isset ($_server[' http_accept_language ')) {//Auto Detect Browser Language Preg_match ('/^ ([a-z\d\-]+)/I ', $_server[' http_accept _language '], $matches); $langSet = $matches [1]; Cookies (' Think_language ', $langSet, 3600); } if (false = = = Stripos (C (' lang_list '), $langSet)) {//illegal 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 Common Language Pack if (Is_file (Lang_path. Lang_set. ' /common.php ') L (include Lang_path. Lang_set. ' /common.php '); Read the packet Common Language pack if (defined (' Group_name ')) {if (C (' App_group_mode ') ==1) {///Independent grouping $file = $path. ' common.php ';} else{//Normal grouping $file = $path. Group_name. '. PHP '; $group = Group_name. C (' Tmpl_file_depr '); } if (Is_file ($file)) L (include $file); }//reads the current module language Pack if (Is_file ($path. $group. Strtolower (module_name). php ')) L (include $path. $group. Strtolower (module_name). php '); } 4. Create 3 language folders under the Lang folder in home. Respectively is ZH-CN en-US zh-tw, in these three folders each create a common.php file, in the common.php corresponding write code as follows: ' Welcome to use thinkphp ',);? The > code is as follows: ' Welcome to the use of thinkphp ', ');? The > code is as follows: ' Welcome to use thinkphp ', ');? > 5. Create a view under the tpl/index/folder The index.html code is as follows: <title>thinkphp Example: Multi-language</title> Switch languages: English | Traditional Chinese | English {$Think. Lang.welcome}Done!  In the background language to language switch words, before each sentence plus L, such as: Code as follows: Public Function index () {print L (' add_user_error '); Add_user_error is only a language variable, the specific language to be written in the language Pack $this->display ();} This point I think cakephp is doing better, do not need to give each sentence to give a variable.

http://www.bkjia.com/PHPjc/737702.html www.bkjia.com true http://www.bkjia.com/PHPjc/737702.html techarticle This article mainly describes the thinkphp implementation of multilingual features (language Pack), the need for friends can refer to the next 1. In the home (you take the project name) of the config.php to add the following configuration code is as follows:? 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.