Thinkphp搭建包括JS多语言的多语言项目实现方法_php实例
本文实例讲述了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()方法内调用下面的生成函数:
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框架程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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 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

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.

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.

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.

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.
