Thinkphp multi-language function (language pack) _ PHP Tutorial

Source: Internet
Author: User
Thinkphp provides multilingual functions (language packs ). 1. add the following configuration copy code in config. php of Home (your project name :? Phpreturnarray (configuration item value: LANG_SWITCH_ONtrue, enable language pack function 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.

The following configuration code is added to config. php of configure (your project name :? Php return array (// 'config map '= 'configuration value ''LANG _ SWITCH_ON' = true, // enable the language pack function...

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.