Implementation of Thinkphp multi-language projects including JS

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, if you need a friend

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, if you need a friend

This article describes how Thinkphp builds a multi-language project that includes multiple languages of JS. 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 uses app_begain to detect and switch the Language Pack, which is related to the project, and has a simple architecture. Here:

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? 6.1.3 ";
// @ 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.