thinkphp External JS Language pack

Source: Internet
Author: User

thinkphp PHP files are also external JS files common to the same language pack

One. PHP language Pack to JSON data format

1. New validation field for language pack application\common\lang\validate-cn.php

<?PHP//Validation Field language Pack Chinesereturn [    'Validate'=[        'name'= [            'Empty'='user name cannot be empty',            'length'='User name length 6-20 characters'        ],        'Email'= [            'Empty'='The mailbox cannot be empty',            'format'='Incorrect mailbox format'        ],        'Password'=[            'Empty'='The password cannot be empty',            'format'='password length is 6-20 characters'        ],    ]];

2.ZH-CN Importing Language Pack validate-cn.php data

<?PHP/** * Global Language Pack * ZH-CN **/$lang= Include (App_path.'common/lang/validate-cn.php');; $data= [    'Success'='Successful execution ^_^',    'Error'='execution Failure (┬_┬)',    'Confirm'=[        '1'='is a',        '2'='No',    ],    'Check'=[        '1'='has been audited',        '2'='not audited',        '3'='Disabled'    ],    'Sex'=[        '1'='Confidentiality',        '2'='male',        '3'='female'    ]];returnArray_merge ($lang, $data);

The purpose of this is to put the "common language pack" and "Field validation language pack" side in different files, and then merge the data. The JSON data is transferred to the Thinkphp method or to the initialized place.

 Public function _initialize ()    {        //  instantiation of the validation language pack        $lang = lang ('validate')        ; = Json_encode ($lang);   // go to JSON string        $This->assign ('validate', $validate);    }

And the variable is instantiated, the following HTML page will pass the instantiated variables of JS (recommended in the Global shared HTML page), add the following code

<script>    //  thinkphp Language Pack pass a JS    var'{$validate}' ; </script>

Two. External JS receive PHP JSON language pack data Common.js

var lang=eval ('('+language+')') ; alert (lang.email.empty);

Output: Mailbox cannot be empty

thinkphp External JS Language pack

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.