Use Thinkphp to build multi-language projects (including JS multi-language projects)

Source: Internet
Author: User
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 found that Thinkphp started with the experiment: Thinkphp uses app_begain to detect and switch language packs and language packs.

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:

Thinkphp uses app_begain to detect and switch the language pack, which is related to the project, and has a simple architecture. here:

Http://www.thinkphp.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:

  1. Public function _ generateJsLanguageFile (){
  2. If (C ("LANG_SWITCH_ON ")){
  3. $ JsLangFilePath = "./Public/v2/js/lang ";
  4. $ LangList = L ();
  5. $ JsLangFileName = $ jsLangFilePath. "/". LANG_SET. ". js ";
  6. // @ Unlink ($ jsLangFileName); // test. The Language pack is not permanently cached.
  7. // A language pack already exists
  8. If (is_file ($ jsLangFileName )){
  9. Return;
  10. }
  11. $ Str = "var \ $ LANG = {";
  12. $ Total = count ($ langList );
  13. $ K = 1;
  14. Foreach ($ langList as $ key => $ value ){
  15. $ Str. = $ key. ": '". $ value ."'";
  16. If ($ k <$ total ){
  17. $ Str. = ",";
  18. }
  19. // $ Str. = "\ r \ n ";
  20. $ K ++;
  21. }
  22. If (! Emptyempty ($ str )){
  23. $ Str. = "}";
  24. $ File_handel = fopen ($ jsLangFileName, "w +"); // open the file and rewrite the mode.
  25. Fwrite ($ file_handel, $ str );
  26. Fclose ($ file_handel );
  27. }
  28. }
  29. }

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.

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.