Multi-language switching using

Source: Internet
Author: User

Step one: Config to open multi-language support in the configuration file of your project

' lang_switch_on ' =>true,//whether to open multi-lingual support
' Var_language ' = ' hl ',//language variable
' Lang_auto_detect ' =>true,//turn on automatic language detection
' Lang_list ' = ' zh-cn,zh-hk,en-us ',//list of supported languages

The above configuration reference CheckLangBehavior.class.php file

Second step: Turn on Checklang behavior, create a new tags.php file in the project configuration file conf directory, make the following configuration in this file

Return ' tags ' =>array (

' App_begin ' = Array (
' Behavior\readhtmlcachebehavior ',//Read static cache

' Behavior\checklangbehavior ',


The above configuration reference thinkphp/mode/common.php file

Step three: Create a language Pack file that corresponds to the language list

Create a new zh-cn.php,zh-hk.php,en-us.php file in the project/common/lang directory, and set the language variable information in these three files.

Return Array (

' username ' = ' username ',

' Password ' = ' password ',


Return Array (

' Username ' = ' user name ',

' Password ' = ' password ',


In the controller, the shortcut function L () can be used to obtain all language variable information, assigned to the template through assign, $this->assign (' Lang ', L ()) in the template to get the language variable {$lang. USERNAME}, the language variable name in the template must be capitalized.

By getting the value of a language variable, you can automatically switch languages, such as by accessing WWW.BAIDU.COM/HL/ZH-CN in a browser, and automatically loading the simplified language pack

There are 4 types of language packs, namely the entire Framework language pack, the project Common language Pack, the module language pack, the Controller language pack, the difference is the scope is different

Multi-language switching using

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: 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.