Home > Backend Development > PHP Tutorial > thinkphp implements multi-language functions_PHP tutorial

thinkphp implements multi-language functions_PHP tutorial

WBOY
Release: 2016-07-13 10:36:52
Original
1812 people have browsed it

This article mainly introduces thinkphp to implement multi-language functions (language packs). Friends who need it can refer to it

1. Add the following configuration in config.php of Home (the project name you chose) ​ The code is as follows: 'Configuration value'                                                                                                                                                                                                                                                                                                 'LANG_AUTO_DETECT' => true, // Automatically detect language 'DEFAULT_LANG' 'DEFAULT_LANG' => 'zh-cn', // Default language 'LANG_LIST' 'LANG_LIST' => 'en-us,zh-cn,zh-tw', //Must write a list of allowed languages 'VAR_LANGUAGE' => 'l', // Default language switching 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( // Just add the following line of definition 'app_begin' => array('CheckLang') ); ​ ​ 3. Copy the Extend/Behavior/CheckLangBehavior.class.php file to Home/lib/Behavior/ (only the full version of thinkphp package is available, if not, please create it yourself) ​ CheckLangBehavior.class.php code: ​ The code is as follows: true, // Automatically detect language. Valid after turning on multi-language function 'Lang_list' = & gt; 'zh-cn', // The language list that allows switching is separated in a comma 'Var_language' = & gt; 'l', // default language switch variable ); ​ //The execution entry of behavioral extension must be run Public function run(&$params){ // Enable static cache $this->checkLanguage(); } ​ /*** Language check * Check the browser supported languages ​​and automatically load the language pack * @access private * @return void ​​*/ private function checkLanguage() { //Do not enable the language pack function, just load the framework language file and return directly If (!C('LANG_SWITCH_ON')){                  return; } } $langSet = C('DEFAULT_LANG'); // Language pack function is enabled // Get the language selection based on whether automatic detection settings are 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')){// Get the last user’s choice                     $langSet = cookie('think_language');           }elseif(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){//Automatically detect browser language                        preg_match('/^([a-zd-]+)/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 the project public language package If(is_file(LANG_PATH.LANG_SET.'/common.php'))           L(include LANG_PATH.LANG_SET.'/common.php'); // Read the grouped common language package If(defined('GROUP_NAME')){                  if(C('APP_GROUP_MODE')==1){ // Independent grouping                      $file = $path.'common.php';           }else{ // Ordinary grouping                      $file = $path.GROUP_NAME.'.php';                     $group = GROUP_NAME.C('TMPL_FILE_DEPR');           }                                                                                                                                                                                                                                if(is_file($file))                       L(include $file); } } // Read the current module language package 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. They are zh-cn en-us zh-tw, ​ Create a common.php file in each of these three folders, as shown in the figure: ​ In common.php, write the corresponding ​ The code is as follows: 'Welcome to use thinkphp', ); ?> The code is as follows: 'Welcome to ThinkPHP', ); ?> ​ The code is as follows: 'Welcome to ThinkPHP', ); ?> ​ ​ 5. Create the view index.html under the tpl/Index/ folder ​ The code is as follows: ThinkPHP Example: Multilingual
{$Think.lang.welcome}
​ ​ Done! ​ If you want to switch the background language, add L before each sentence, such as: ​ The code is as follows: public function index(){ print L('add_user_error'); //add_user_error is just a language variable, the specific language must be written in the language package $this->display(); } ​ ​ I think cakephp does this better. There is no need to give a variable to every sentence.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/737702.htmlTechArticleThis article mainly introduces thinkphp to implement multi-language functions (language packages). Friends who need it can refer to 1. Add the following configuration code to config.php in Home (the project name you chose):?phpr...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template