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:
- Public function _ generateJsLanguageFile (){
- If (C ("LANG_SWITCH_ON ")){
- $ JsLangFilePath = "./Public/v2/js/lang ";
- $ LangList = L ();
- $ JsLangFileName = $ jsLangFilePath. "/". LANG_SET. ". js ";
- // @ 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.