thinkphp Multi-language switching Setup method detailed _php Example

Source: Internet
Author: User
Tags keyword list php template smarty template

This article illustrates the thinkphp switching setup method for multiple languages. Share to everyone for your reference, specific as follows:

thinkphp Multi-Language setting is a bit ' tall ', why is it a bit ' tall '? Because I set up a long time to be done, and I did a long time reason, unexpectedly because ' To open the language setting must first open the initialization system behavior class ', so, here, because my experience is limited, I think the multiple language settings TP must first initialize TP CheckLangBehavior.class.php

How do you initialize CheckLangBehavior.class.php? The following is explained.

Below the TP framework, all modules initialize CheckLangBehavior.class.php, screenshot below:

Path you should understand, just in your public module configuration file config.php There is a new tags.php on the line, tags.php content as follows:

<?php return
Array (
//app_begin This behavior tag, in fact, is the built-in label TP, the role is when the TP initialization, triggering an action, where set of course is to trigger the function of multi-language
"App_ Begin ' => Array (' Behavior\checklangbehavior '),//Open language function

The above checkLangBehavior.class.php in the TP's built-in behavior class folder, screenshots to prove that first:

Is this class exactly the same as the class above? You can go into the Library/behavior folder of the TP framework to see if the class exists, what it is, what it is, and where it's not resolved.

After configuring the above tags.php, we must configure the TP configuration file config.php

This config.php is in the first screenshot above where you just add the following code:

' lang_switch_on '  => true,    //Open Multi-Language support switch
' Default_lang '    => ' ZH-CN ',  //default language
' Lang_ List '    => ' zh-cn,en-us ',//Allow switched language lists comma-delimited
' Lang_auto_detect '  => true,//  Automatically detect language

After configuring this is a multilingual file configuration, not to say, screenshots:

The following is the code in the configuration en-us.php file:

<?php return
Array (///
  * Core language variable * *
  _web_name_ '   => ' TRAVEL ',
  ' _wellcome_ '   => ') Wellcome here! ',

Zh-cn.php the code in the file:

<?php return
Array (//
  * Core language variable * * 
  _web_name_ '   => ' contacts ',
  ' _wellcome_ '   => ') Welcome! ',

Here's the code for the Lang method I IndexController.class.php:

Public Function lang () {
    $wellcome = L (' _wellcome_ ');
    $this->assign (' Wellcome ', $wellcome);
    $this->display ();

This is the default template lang.html under the Lang method in my IndexController.class.php controller, which can be tested below.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
" Xhtml1-transitional.dtd ">

The test results are as follows:

You can click on the switch language, if there is a change in English and Chinese

More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."

I hope this article will help you with the PHP program design based on thinkphp framework.

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.