Thinkphp to implement multilingual features

Source: Internet
Author: User
Tags extend pack php file php code

  This article mainly introduces the thinkphp implementation of multilingual features (language packs), the need for friends can refer to the following

1. Add the following configuration     code in the config.php of Home (your project name): <?php return Array (   //' Config item ' => ' configuration value '   &NBSP ;     ' lang_switch_on '     =>     True,    //Open language pack function                 ' lang_auto_detect '     =>     true,//automatic detection language       &NBS P ' Default_lang '         =>     ' ZH-CN ',//default language                 ' lang_list '            =>     ' EN-US,ZH-CN,ZH-TW ',//must write a list of permissible languages & nbsp       ' var_language '     => ' l ',//default language switch variable); Add a PHP file in the Conf folder of?>     2.Home (t ag.php), add the following code:     Code as follows: Return Array (   ///Add the following line definition to     ' App_begin ' => Array (' Checkl Ang '));     3. Copy extend/behavior/checklangbehavior.class.php file to home/lib/behavior/(full version of thinkphp package only, if not, please create it yourself)   ChecklangBehavior.class.php 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 {   //behavioral parameter definition (default) can be overridden in a project configuration     protected $optio NS   =  array (            ' lang_switch_on '        => false ,  //Default shutdown Language Pack features             ' lang_auto_detect '      => true,   Automatic detection language open multilingual features effective             ' lang_list '             => ' ZH-CN ',//Allow switched language list comma-delimited             ' var_language '          =& Gt ' L ',        //default language switching variable        );      //Behavior extension Implementation Portal must be run     Public function run (& $params) {    &nbsp  //Open static cache         $this->checklanguage ();    }      /**      * language check      * check browser support language and automatically load language pack      * @access private      * @return void      */    Private Function Checklan Guage () {       //Do not open the Language pack feature, just load frame language files directly back to       the   if (! C (' lang_switch_on ')) {            return;        }         $langSet = C (' Default_lang ');        //Enable Language Pack feature        //Enable automatic detection setting to obtain language selection         if ( C (' Lang_auto_detect ')) {            if (Isset ($_get[c (' Var_language '))) {    & nbsp           $langSet = $_get[c (' var_language ')];//URL set language variable           & nbsp     Cookies (' Think_language ', $langSet, 3600);            }elseif (Cookie (' Think_language ')) {//Get last user's selection         &NBSP ;       $langSet = Cookie (' think_language ');            }elseif (isset ($_server[' http_accept_language ')) {//automatic detection Browser language     & nbsp           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 parameters                 $langSet = C (' Default_lang ');            }         {       //define current language       &N Bsp 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 ');             //Read Group Common Language Pack         if (defined (' Group_name ')) {            if (C (' App_group_mode ') ==1) {//standalone group               & nbsp $file = $path. ' common.php ';            }else{//General Group                 $file = $ Path. Group_name. '. PHP ';                 $group = group_name. C (' Tmpl_file_depr ');                         if (Is_file ($file))                 L (i Nclude $file);        }        /read current module language Pack         if (is_file $path. $grou P.strtolower (module_name). php ')             L (include $path. $group. Strtolower (module_name). php ');    }       4. Create 3 language folders under the Lang folder in home. respectively is ZH-CN  en-us  ZH-TW,   in these three folders each create a common.php file, such as:   in common.php corresponding to write     code as follows: PHP return Array (        ' welcome ' => ' Welcome to use thinkphp ');?> code is as follows: <?php return ARRA Y (    ' welcome ' => ' Welcome to use thinkphp ',);?>   Code as follows: <?php return Array (    ' Welcome ' => ' Welcome to use thinkphp ',);?>     5. Create a view under the tpl/index/folder index.html   code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <html>  <head>   <meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 ">   <title>thinkphp Example: Multi-language </title>  </head>  <body>   <div class= "main" > <div> switching language: <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> </html>    !    Background language to switch words, before each sentence plus L, such as:     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 better job and doesn't need to give a variable to every sentence.  

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.