thinkphp implements multilingual functionality in the of language package

  • 2021-01-25 07:20:15
  • OfStack

1. Add the following configuration to config.php of Home(your project name)


<?php
return array(
    //' Configuration items '=>' Configuration values '
        'LANG_SWITCH_ON'     =>     true,    // Enable language pack functionality         
        'LANG_AUTO_DETECT'     =>     true, //  Autodetection language 
        'DEFAULT_LANG'         =>     'zh-cn', //  The default language         
        'LANG_LIST'            =>    'en-us,zh-cn,zh-tw', // You must write a list of allowable languages 
        'VAR_LANGUAGE'     => 'l', //  Default language switching variables 
);
?>

2. Add 1 php file (tag.php) to Home folder conf. Add the following code:


return array(
    //  Add the following 1 Just line definition 
    'app_begin' => array('CheckLang')
);

3. The Extend/Behavior/CheckLangBehavior. class. php file copy to Home/lib Behavior/(full version thinkphp package has, if not please create)

CheckLangBehavior. class. php code:


<?php
defined('THINK_PATH') or exit();
/**
 *  Language testing   Language packs are loaded automatically 
 * @category   Extend
 * @package  Extend
 * @subpackage  Behavior
 */
class CheckLangBehavior extends Behavior {
    //  Behavior parameter definition (default)   It can be overridden in the project configuration 
    protected $options   =  array(
            'LANG_SWITCH_ON'        => false,   //  Language packs are turned off by default 
            'LANG_AUTO_DETECT'      => true,   //  Autodetection language   This is effective when multilingual functionality is enabled 
            'LANG_LIST'             => 'zh-cn', //  List of languages that are allowed to switch   Separate with commas 
            'VAR_LANGUAGE'          => 'l',        //  Default language switching variables 
        );
    //  The execution entry for the behavior extension must be run
    public function run(&$params){
        //  Enable static caching 
        $this->checkLanguage();
    }
    /**
     *  Language check 
     *  Check the browser support language 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 functionality is enabled 
        //  Gets the language selection depending on whether the automatic detection Settings are enabled 
        if (C('LANG_AUTO_DETECT')){
            if(isset($_GET[C('VAR_LANGUAGE')])){
                $langSet = $_GET[C('VAR_LANGUAGE')];// url Language variables are set in the 
                cookie('think_language',$langSet,3600);
            }elseif(cookie('think_language')){//  Gets the selection of the last user 
                $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];
                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 common language package 
        if(is_file(LANG_PATH.LANG_SET.'/common.php'))
            L(include LANG_PATH.LANG_SET.'/common.php');     
        //  Read the grouping common language pack 
        if(defined('GROUP_NAME')){
            if(C('APP_GROUP_MODE')==1){ //  Independently grouped 
                $file = $path.'common.php';
            }else{ //  Ordinary group 
                $file = $path.GROUP_NAME.'.php';
                $group = GROUP_NAME.C('TMPL_FILE_DEPR');
            }
            if(is_file($file))
                L(include $file);
        }
        //  Reads the current module language pack 
        if (is_file($path.$group.strtolower(MODULE_NAME).'.php'))
            L(include $path.$group.strtolower(MODULE_NAME).'.php');
    }
}

4. Create three language folders under lang in Home. zh-cn en-us zh-tw, en-us zh-tw

Create 1 common. php file in each of the 3 folders, as shown in the figure below:

Write the corresponding in common.php


<?php
return array(
        'welcome'=>'Welcome to use thinkphp',
);
?>


<?php
return array(
    'welcome'=>' Welcome to use ThinkPHP',
);
?>


<?php
return array(
    'welcome'=>' � into use ThinkPHP',
);
?>

5. Create the view index.html in the folder tpl/Index/


<!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: Multiple languages </title>
 </head>
 <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>
</html>

And you're done!

To switch the background language, add ES73en before each sentence, such as:


public function index(){
print  L('add_user_error');  //add_user_error It's just a linguistic variable , The specific language should be written in the language package 
$this->display();
}

In my opinion, cakephp does a better job of this. There is no need to give a variable to every sentence.


Related articles: