Thinkphp: how to build a multi-language project that includes multiple languages in JavaScript _ php instance

Source: Internet
Author: User
This article mainly introduces how Thinkphp builds a multi-language project that includes multiple languages in JavaScript. it is a very practical technique to build a multi-language website by calling JavaScript language packs, for more information about how to build a multi-language project with multiple languages including JavaScript, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:

I. problems:

The English version of the project needs to be developed, so a multi-language project needs to be set up.

The Thinkphp framework is used in the project. I vaguely remember that Thinkphp has multi-language settings. after reading the help manual, I did. here I started my experiment:

II. implementation method:

Thinkphp using app_begain to detect and switch language packs, language packs and project-related, architecture are relatively simple, specific here: http://www.thinkphp1.cn/info/188.html

After setting up, you can use URL ."? L = en-us "for dynamic switching and debugging, very good.

I found that the multilingual files in JavaScript are not easy to handle, so we can't use all assign in the past. anyway, the multilingual files won't always be configured. Instead, the corresponding multilingual js files are automatically generated, then, the page dynamically requests the corresponding multilingual files based on LANG_SET and calls the following generation function in the _ initialize () method of the basic Action class:

The code is as follows:

Public function _ generateJsLanguageFile (){
If (C ("LANG_SWITCH_ON ")){
$ JsLangFilePath = "./Public/v2/js/lang ";
$ LangList = L ();
$ JsLangFileName = $ jsLangFilePath. "/". LANG_SET. ". js? 1.1.9 ";
// @ Unlink ($ jsLangFileName); // test. The Language pack is not permanently cached.
// A language pack already exists
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 the file and rewrite the mode.
Fwrite ($ file_handel, $ str );
Fclose ($ file_handel );
}
}
}


In this way, the current language pack is generated before each access and then called in Tpl.

In this way, the js will be automatically loaded every time. This js will be permanently cached. if there is a change to the language pack, you only need to modify the language pack of Thinkphp and then delete the old js language pack so that it will be automatically regenerated.

This can be used in js: top10_title = $ LANG. _ NEW_LANGUAGE; in this way, the entire project is equivalent to being bilingual and can be configured in one place.

I hope this article will help you with ThinkPHP framework programming.

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.