Home > Backend Development > PHP Tutorial > Thinkphp builds multi-language project implementation methods including JS multi-language, thinkphpjs_PHP tutorial

Thinkphp builds multi-language project implementation methods including JS multi-language, thinkphpjs_PHP tutorial

WBOY
Release: 2016-07-13 10:13:06
Original
878 people have browsed it

Thinkphp builds multi-language project implementation methods including JS multi-language, thinkphpjs

The example in this article describes the implementation method of Thinkphp to build multi-language projects including JS multi-language. Share it with everyone for your reference. The specific implementation method is as follows:

1. Question:

The project needs to develop an English version, so a multi-language project needs to be built.

The project uses the Thinkphp framework. I vaguely remembered that Thinkphp has multiple language settings. I looked through the help manual and found that it was there. Let’s start experimenting:

2. Implementation method:

Thinkphp uses app_begain to detect and switch language packs. Language packs are related to projects and the architecture is relatively simple. The details are here: http://www.thinkphp.cn/info/188.html

After it is set up, you can use the URL."?l=en-us" to dynamically switch and debug, which is very good.

I found that multi-language in js is difficult to handle. You can’t assign all of them. Anyway, multi-language will not always be configured. Simply automatically generate the corresponding multi-language js file, and then the page will dynamically request the corresponding according to LANG_SET For multi-language files, call the following generation function in the _initialize() method of the basic Action class:

Copy code 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";
//@unlink($jsLangFileName);//Testing, no permanent caching of language packs
//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 .="rn";
                   $k++;
            }
If(!emptyempty($str)){
                      $str .= "}";
                    $file_handel = fopen($jsLangFileName, "w+");//Open the file, rewrite mode
                                                fwrite($file_handel, $str);
                                       fclose($file_handel);
            }
}
}

In this way, the current language package will be generated before each visit and then called within Tpl.

In this way, it will be automatically loaded every time. This js is permanently cached. If there are changes to the language package, you only need to modify the Thinkphp language package, then delete the old js language package and let it be automatically regenerated.

You can use it like this in js: top10_title = $LANG._NEW_LANGUAGE; In this way, the entire project is bilingual and can be configured in one place.

I hope this article will be helpful to everyone’s ThinkPHP framework programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/917043.htmlTechArticleThinkphp builds multi-language project implementation methods including JS multi-language, thinkphpjs This article describes the example of Thinkphp building including JS multi-language Multi-language project implementation method. Share it with everyone for everyone...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template