Thinkphp supports multiple languages

Source: Internet
Author: User

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): <? Phpreturn array (// 'config map '=> '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: <? Phpdefined ('Think _ path') or exit (); /*** language detection and automatic loading of language packs * @ category Extend * @ package Extend * @ subpackage Behavior */class CheckLangBehavior extends Behavior {// Behavior Parameter definition (default) you can override protected $ options = array ('lang _ SWITCH_ON '=> false, // The Language Pack function 'lang _ AUTO_DETECT' => true by default, // The Automatic Detection language is effective after the multi-language function is enabled 'lang _ list' => 'zh-cn ', // The list of languages that can be switched is separated by commas (,). The default LANGUAGE switch variable is 'VAR _ LANGUAGE' => 'L'. // The execution entry of the behavior extension must be run. Public function run (& $ params) {// enable static cache $ this-> checkLanguage ();}/*** language check * Check the supported language of the browser, the Language Pack * @ access private * @ return void */private function checkLanguage () is automatically loaded. {// 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. En-cn en-us zh-tw. Create a common. php file in these three folders, and write the following code in common. php: <? Phpreturn array ('Welcome '=> 'Welcome to use thinkphp',);?> The Code is as follows: <? Phpreturn array ('Welcome '=> 'Welcome to thinkphp',);?> The Code is as follows: <? Phpreturn array ('Welcome '=> 'Welcome to thinkphp',);?> 5. The code for creating the view index.html In the tpl/Index/folder is as follows: <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">

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.