Configuration and use of ThinkPHP multi-language support

Source: Internet
Author: User
ThinkPHP has built-in support for multiple languages. if the application project involves internationalization, you can define related language pack files for multi-language support. The multilingual support involved here refers to the template language, and the multilingual conversion (translation) of data is not in this category. ThinkPHP multi-language configuration

ThinkPHP
Built-in support for multiple languages. if the application project involves internationalization, you can define related language pack files for multi-language support. The multilingual support involved here refers to the template language, and the multilingual conversion (translation) of data is not in this category.

Add the following parameters to the project configuration file to enable multi-language support:

'Lang _ SWITCH_ON'
=> True, // enable multi-language support
'Default _ lang' => 'zh-cn ',//
Default language
'Lang _ AUTO_DETECT '=> true, // automatic language detection

ThinkPHP
The system automatically determines the language supported by the current user's browser to locate which language pack to use. if the relevant language pack file cannot be found, the default language is used. If the browser supports multiple languages, use the first supported language.

In addition to automatically detecting the current user's browser language
URL
Specifies the language to use. For more information, see the following example.

The packages or directories related to multiple languages are as follows:

ThinkPHP system (simplified Chinese language pack: ThinkPHP system directory/Lang/zh-cn.php)
ThinkPHP system traditional Chinese language pack: ThinkPHP system directory/Lang/zh-tw.php
ThinkPHP system American English language pack: ThinkPHP system directory/Lang/en-us.php
Project simplified Chinese language pack Directory: project directory/Lang/ <项目分组 />Zh-cn/
Project traditional Chinese language pack Directory: project directory/Lang/ <项目分组 />Zh-tw/
Project American English Chinese language pack Directory: project directory/Lang/ <项目分组 />En-us/

If there are more languages, you can create files or directories in sequence according to the example.
Multi-language project instance

The following example shows how to implement multi-language support in the project.

In
Create the common. php file under the/Lang/zh-cn/project directory, and set the definition of the simplified Chinese language in the project:




  1. Return array (
  2. 'Welcome '=>' welcome to ThinkPHP! ',
  3. 'Message' => 'this is a test message! ',
  4. );
  5. ?>

Create in the project directory/Lang/en-us/
Common. php file, and set the definition of American English language inside:




  1. Return array (
  2. 'Welcome '=> 'Welcome to ThinkPHP! ',
  3. 'Message' => 'This is a test message! ',
  4. );
  5. ?>

In the operation (such as the Index module lang
Operation:




  1. Public function lang (){
  2. $ This-> assign ('message', L ('message '));
  3. $ This-> display ();
  4. }

Corresponding template (Tpl/default/Index/lang.html ):

{$ Think. lang. welcome}


{$ Message}


Switch to: href = "? L = zh-cn "> simplified Chinese | href = "? L = en-us "> English



When we access this operation, the system will automatically obtain the corresponding language pack content based on the current browser language. In the above example, the content directly displayed in the template is used
{$ Think. lang. welcome} is output. when obtaining the Language Pack content in an operation, use the L shortcut. When you click the English link
L = en-us parameter. The system will obtain the corresponding English language pack to display:

Welcome to ThinkPHP!
This is a test
Message!

L shortcuts

L The shortcut is used to obtain and set language definitions. The syntax is as follows:

L (name,
Value)

If both the name and value parameters exist, the language variable name = value is set. if the value is omitted, the name is obtained.
The value corresponding to the language variable. if the name is not defined
Returns in uppercase.
Module Definition Language Pack

When the project is large, you may need to define the language pack according to the module. When a language pack is defined by module, the language pack name is the corresponding module name (in lower case ). For example
When the User module defines the language pack, the corresponding language pack file is: project directory/Lang/zh-cn/user. php
.

When defining a language pack for a module, it is used in exactly the same way as the preceding general definition. When the module language pack has the same language definition items as the common language pack, the module language pack definition overwrites the definition of the common mode.
Use language packs in the project model

In Project multi-language support, besides template and module operations, language packs (such as prompt information for automatic verification) may also be used in model classes ). To use the language pack in the project model, you only need to change the prompt information for the original write verification rules:

Array ('title', 'require ',' {% vali_title} ', 1 ),

Vali_title
It is the language variable defined in the project language pack, just as the example above defines welcome and message. If you want to test the effect of multiple languages, you can input l
Parameters:

Method = "post">

Note: Do not use _ URL _/insert? L = en-us
This parameter is passed in, otherwise it will cause "form token error ".

Because ThinkPHP can automatically detect the language used by the browser
Parameter. of course, this is not absolute. it can be determined based on the actual situation.

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.