Home Backend Development PHP Tutorial Thinkphp搭建包括JS多语言的多语言项目实现方法_php实例

Thinkphp搭建包括JS多语言的多语言项目实现方法_php实例

May 16, 2016 pm 08:30 PM
thinkphp multi-language build project

本文实例讲述了Thinkphp搭建包括JS多语言的多语言项目实现方法。分享给大家供大家参考。具体实现方法如下:

一、问题:

项目需要开发英文版,于是需要搭建多语言项目.

项目使用Thinkphp框架,隐约记得Thinkphp有多语言设置,翻看了帮助手册,果然有,这就边实验边开始:

二、实现方法:

Thinkphp采用app_begain来检测和切换语言包,语言包和项目相关,构架等都比较简单,具体的这里:http://www.thinkphp.cn/info/188.html

搭建好了,就可以使用URL."?l=en-us"来动态切换和调试,很不错的.

这边发现js内的多语言不好处理,总不能全部assign过去,反正多语言也不会总是配置,干脆自动生成对应的多语言js文件,然后页面根据LANG_SET来动态请求对应的多语言文件,在基础Action类的_initialize()方法内调用下面的生成函数:

复制代码 代码如下:
public function _generateJsLanguageFile(){ 
        if(C("LANG_SWITCH_ON")){ 
            $jsLangFilePath = "./Public/v2/js/lang"; 
            $langList = L(); 
            $jsLangFileName = $jsLangFilePath."/".LANG_SET.".js"; 
            //@unlink($jsLangFileName);//测试,不永久缓存语言包 
            //已存在语言包 
            if(is_file($jsLangFileName)){ 
                return; 
            } 
            $str = "var \$LANG={";
            $total = count($langList);
            $k = 1;
            foreach ($langList as $key => $value) {
                $str .=$key.":'".$value."'";
                if($k                     $str .=",";
                }
                // $str .="\r\n";
                $k++;
            }
            if(!emptyempty($str)){
                $str .= "}";
                $file_handel = fopen($jsLangFileName, "w+");//打开文件,重写模式
                fwrite($file_handel, $str);
                fclose($file_handel);
            }
        }
}

这样,每次访问前都会生成当前的语言包,然后在Tpl内调用.

这样,每次都会自动载入,这个js是永久缓存的,如果有语言包的更改,只需要修改Thinkphp的语言包,然后删除旧的js语言包,让他自动重新生成.

js内就可以这么使用:top10_title = $LANG._NEW_LANGUAGE;这样整个项目就等于是双语的,并且是一处配置即可.

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code Apr 09, 2024 pm 03:20 PM

Fermat's last theorem, about to be conquered by AI? And the most meaningful part of the whole thing is that Fermat’s Last Theorem, which AI is about to solve, is precisely to prove that AI is useless. Once upon a time, mathematics belonged to the realm of pure human intelligence; now, this territory is being deciphered and trampled by advanced algorithms. Image Fermat's Last Theorem is a "notorious" puzzle that has puzzled mathematicians for centuries. It was proven in 1993, and now mathematicians have a big plan: to recreate the proof using computers. They hope that any logical errors in this version of the proof can be checked by a computer. Project address: https://github.com/riccardobrasca/flt

A closer look at PyCharm: a quick way to delete projects A closer look at PyCharm: a quick way to delete projects Feb 26, 2024 pm 04:21 PM

Title: Learn more about PyCharm: An efficient way to delete projects. In recent years, Python, as a powerful and flexible programming language, has been favored by more and more developers. In the development of Python projects, it is crucial to choose an efficient integrated development environment. As a powerful integrated development environment, PyCharm provides Python developers with many convenient functions and tools, including deleting project directories quickly and efficiently. The following will focus on how to use delete in PyCharm

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

PyCharm Practical Tips: Convert Project to Executable EXE File PyCharm Practical Tips: Convert Project to Executable EXE File Feb 23, 2024 am 09:33 AM

PyCharm is a powerful Python integrated development environment that provides a wealth of development tools and environment configurations, allowing developers to write and debug code more efficiently. In the process of using PyCharm for Python project development, sometimes we need to package the project into an executable EXE file to run on a computer that does not have a Python environment installed. This article will introduce how to use PyCharm to convert a project into an executable EXE file, and give specific code examples. head

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles