I’ve been busy with projects lately, so I haven’t been able to write much. The main content of this article is how to implement a multi-language backend.
Foreword
The company needs to write a multi-language backend, and it used to be I haven’t done it before, so I went to Baidu to check it out. This is the first time I have used the TP framework for two years and discovered that the framework has its own multi-language switching function. I really give it a thumbs up here.
Next let’s implement this function.
1. Backend implementation of multi-language
Using the framework is v5. 1.38 LTS
Come to the configuration fileconfig/app.php
You can see that the configuration information about the language only has these two parameters.
lang_switch_on
This parameter is the browser language that is automatically monitored.
default_lang Regarding this parameter, it will directly default to Chinese before switching other languages.Configuration file understanding After that, we need to create the lang directory in the required module and configure the language package of the corresponding language.
Here Kaka only configures English and Chinese. If you need other languages, you can add them yourself. At this step, you will find that there is no parameter from beginning to end that is used to configure the language list. The configuration file was not found either.
But you can see this line of code in the document. Let’s go to the source code and take a look.
You can see clearly in the source code that the name value of the language cookie is also fixed, and the validity period of the cookie is 3600. These need to be adjusted according to your own project. of.
The most important thing is to look at the list of allowed languages. We continue to trace this variableIn the file
thinkphp/library/think/Lang.php
Finally we can see a method, which is exactly the method given in the documentBecause we The entire backend needs to use multiple languages, so we need to configure it in common
2. View switching language
Directly call the languageChange
method in the view template There is a variable in the view called
$languageName
, this variable is click It is taken directly from the configuration file and will be adjusted to the database later.
Then write the method
changeLanguage
in the controller to handle the cookie change after language switchingAt this time our background page is like this
3. Test
After the above operation, we can test it according to the language package we set. After switching the language
In the template we need to use the lang template tag to obtain dataThis is the English state
This is the Chinese state
The implementation of multi-language is completed .
4. Summary
I have said so much above, but I just summarized the details of each step. The steps are written out for everyone.
The key points are as follows
application/config.php
Configuration file modificationAdd the lang directory in the corresponding module and create the required language package The view performs language switching and calls the background interface to store the cookie value. Save it in think_var Template and use the lang tag
Persistence in learning, blogging, and sharing is what Kaka has been doing since A belief that has always been upheld. I hope Kaka’s article on Nuoda Internet can bring you a little bit of help.
The above is the detailed content of Do you know how ThinkPHP multi-language is implemented?. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
