Thinkphp Implementing multilingual features (language packs) _php Tutorials

Source: Internet
Author: User
1. Add the following configuration to the config.php of Home (the project name you are taking)

Copy the Code code as follows:
Return Array (
' Config item ' = ' config value '
' lang_switch_on ' + true,//turn on the Language pack feature
' Lang_auto_detect ' = true,//auto-detect language
' Default_lang ' = ' zh-cn ',//default language
' Lang_list ' = ' en-us,zh-cn,zh-tw ',//must write a list of allowed languages
' Var_language ' = ' l ',//default language toggle Variable
);
?>

Add a php file (tag.php) to the Conf folder in 2.Home and add the following code:

Copy the Code code as follows:
Return Array (
Add the following line definition
' 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:

Copy the Code code 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 {
Behavior parameter definitions (default values) can be overridden in the project configuration
Protected $options = Array (
' lang_switch_on ' = false,//default language pack feature turned off
' Lang_auto_detect ' + true,//auto-detect language effective after opening multi-language function
' Lang_list ' + ' ZH-CN ',//The list of languages allowed to switch is separated by commas
' Var_language ' = ' l ',//default language toggle Variable
);

The execution entry for the behavior extension must be run
Public function run (& $params) {
Turn on static caching
$this->checklanguage ();
}

/**
* language Check
* Check browser support language and automatically load language packs
* @access Private
* @return void
*/
Private Function Checklang Uage () {
//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 ');
//Language Pack 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 ')];//URL set language variable
cookie (' 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];
Cookie (' 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 Project Common Language Pack
if (Is_file (Lang_path. Lang_set. ' /common.php '))
L (include Lang_path. Lang_set. ' /common.php ');
Reading a grouped common language pack
if (defined (' Group_name ')) {
if (C (' App_group_mode ') ==1) {//Independent grouping
$file = $path. ' common.php ';
}else{//General 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 3 language folders under the Lang folder in home. Zh-cn en-US zh-tw, respectively,

Create a common.php file in each of these three folders,

In the common.php, write in the correspondence.
Copy the Code code as follows:
Return Array (
' Welcome ' = ' Welcome to use thinkphp ',
);
?>

Copy the Code code as follows:
Return Array (
' Welcome ' = ' Welcome to use thinkphp ',
);
?>

Copy the Code code as follows:
Return Array (
' Welcome ' = ' Welcome to use thinkphp ',
);
?>

5. Create a view under the Tpl/index/folder index.html

Copy the Code code as follows:




thinkphp Example: Multi-language



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:

Copy the Code 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/736806.html www.bkjia.com true http://www.bkjia.com/PHPjc/736806.html techarticle 1. In the config.php of home (you take the project name), add the following configuration copy code code as follows:? PHP return Array (//' config item ' = ' config value ' ' lang_switch_on ' = true,//Open language pack function ... /c7>

  • Related Article

    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.