thinkphp implement multilingual features (language Pack) _php instances

Source: Internet
Author: User
Tags pack

1. Add the following configuration in the config.php of the home (your project name)

Copy Code code as follows:

<?php
Return Array (
' Configuration item ' => ' config value '
' lang_switch_on ' => true,//enable the Language pack feature
' Lang_auto_detect ' => true,//automatic detection language
' Default_lang ' => ' ZH-CN ',//default language
' Lang_list ' => ' en-us,zh-cn,zh-tw ',//must write a list of permissible languages
' Var_language ' => ' l ',//default language switch variable
);
?>

2.Home in the Conf folder add a PHP file (tag.php), add the following code:

Copy Code code as follows:

Return Array (
Add the following line definition to
' App_begin ' => array (' Checklang ')
);

3. Copy extend/behavior/checklangbehavior.class.php file to home/lib/behavior/(full version of thinkphp package only, if not, please create yourself)

CheckLangBehavior.class.php Code:

Copy Code code as follows:

<?php
Defined (' Think_path ') or exit ();
/**
* Language detection and automatically load 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 Shutdown Language Pack feature
' Lang_auto_detect ' => true,//automatic detection language is effective when multilingual features are turned on
' Lang_list ' => ' ZH-CN ',//Allow switched language list separated by commas
' Var_language ' => ' l ',//default language switch variable
);

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

/**
* Language Check
* Check the browser support language and load language packs automatically
* @access Private
* @return void
*/
Private Function CheckLanguage () {
Does not open the language pack function, simply loads the frame language file to return directly
if (! C (' lang_switch_on ')) {
Return
}
$langSet = C (' Default_lang ');
Enable the Language pack feature
Get language selection based on whether to enable automatic detection settings
if (C (' Lang_auto_detect ')) {
if (Isset ($_get[c (' Var_language '))) {
$langSet = language variable set in $_get[c (' var_language ')];//URL
Cookies (' Think_language ', $langSet, 3600);
}elseif (' Think_language ') {//Get last user's selection
$langSet = Cookie (' think_language ');
}elseif (Isset ($_server[' http_accept_language ')) {//automatically detects 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. ' /';
Reading the 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) {//standalone 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);
}
Reading 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. is ZH-CN en-US ZH-TW,

Create a common.php file in each of these three folders, as shown in the figure:

In the common.php, write it.

Copy Code code as follows:

<?php
Return Array (
' Welcome ' => ' Welcome to use thinkphp ',
);
?>

Copy Code code as follows:

<?php
Return Array (
' Welcome ' => ' Welcome to use thinkphp ',
);
?>

Copy Code code as follows:

<?php
Return Array (
' Welcome ' => ' Welcome to use thinkphp ',
);
?>

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

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>thinkphp Example: Multilingual </title>
<body>
<div class= "Main" >
<div> Switching languages: <a href= "? l=zh-cn" > Simplified Chinese </a> | <a href= "? l=zh-tw" > Traditional Chinese </a> | <a href= "l=en-us" > English </a></div>
<div class= "Result" >{$Think .lang.welcome}</div>
</div>
</body>

Done!

Background language to be a language switch, before each sentence plus L, such as:

Copy 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 ();
}

I think cake does a good job, do not need to give each sentence a variable.

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.