thinkphp build multi-language project implementation method including JS multi-language, thinkphpjs_php tutorial

Source: Internet
Author: User

thinkphp build multi-language project implementation method including JS multi-language, Thinkphpjs


In this paper, the implementation method of multi-language project including JS multi-language is described in thinkphp. Share to everyone for your reference. The implementation method is as follows:

First, the question:

The project needs to be developed in English, so you need to build a multi-lingual project.

The project uses the thinkphp framework, vaguely remember that thinkphp has multiple language settings, turned to help manuals, sure enough, this side of the experiment began:

Second, the realization method:

thinkphp use App_begain to detect and switch language packs, language packs and project-related, architecture, etc. are relatively simple, specific here: http://www.thinkphp.cn/info/188.html

When you're ready, you can use the URL. L=en-us "to dynamically switch and debug, very good.

This side found JS inside the multi-lingual bad processing, can not all assign the past, anyway, many languages will not always be configured, simply automatically generate the corresponding multi-language JS file, and then the page according to Lang_set to dynamically request the corresponding multi-language files, in the base action class _ The following build function is called within the Initialize () method: The
copy Code code is as follows: Public function _generatejslanguagefile () {
if (C (" Lang_switch_on ") {
$jsLangFilePath ="./public/v2/js/lang ";
$langList = L ();
$jsLangFileName = $jsLangFilePath. " /". Lang_set. ". JS ";
//@unlink ($jsLangFileName);//test, not persistent cache language Pack
//Existing Language Pack
if (Is_file ($jsLangFileName)) {
return;
}
$str = "var \ $LANG ={";
$total = count ($langList);
$k = 1;
foreach ($langList as $key = = $value) {
$str. = $key. ":". $value. "'";
if ($k < $total) {
$str. = ",";
}
//$str. = "\ r \ n";
$k + +;
}
if (!emptyempty ($STR)) {
$str. = "}";
$file _handel = fopen ($jsLangFileName, "w+");//Open file, override mode
Fwrite ($file _handel, $STR);
Fclose ($file _handel);
}
}
}
This will generate the current language pack before each access, and then call within the TPL.

In this way, each time will be automatically loaded, this JS is permanently cached, if there are language pack changes, only need to modify the Thinkphp language pack, and then delete the old JS language pack, let him automatically regenerate.

JS can be used in this way: Top10_title = $LANG. _new_language; So the whole project is bilingual, and it's a configuration.

It is hoped that this article will be helpful to everyone's thinkphp framework design.

http://www.bkjia.com/PHPjc/917043.html www.bkjia.com true http://www.bkjia.com/PHPjc/917043.html techarticle thinkphp constructs the multi-language project realization method which includes the JS multi-language, Thinkphpjs this article narrates the thinkphp constructs including the JS Multi-language multi-language project realization method. Share to everyone ...

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