search
HomePHP FrameworkThinkPHPDo you know how ThinkPHP multi-language is implemented?

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.phpYou can see that the configuration information about the language only has these two parameters.

lang_switch_onThis parameter is the browser language that is automatically monitored.

default_lang Regarding this parameter, it will directly default to Chinese before switching other languages.Do you know how ThinkPHP multi-language is implemented?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. Do you know how ThinkPHP multi-language is implemented?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.

Do you know how ThinkPHP multi-language is implemented?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 variableDo you know how ThinkPHP multi-language is implemented?In the filethinkphp/library/think/Lang.phpFinally we can see a method, which is exactly the method given in the documentDo you know how ThinkPHP multi-language is implemented?Because we The entire backend needs to use multiple languages, so we need to configure it in commonDo you know how ThinkPHP multi-language is implemented?

2. View switching language

Directly call the languageChange method in the view template Do you know how ThinkPHP multi-language is implemented?Do you know how ThinkPHP multi-language is implemented?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.

Do you know how ThinkPHP multi-language is implemented?Then write the method changeLanguage in the controller to handle the cookie change after language switchingDo you know how ThinkPHP multi-language is implemented?At this time our background page is like thisDo you know how ThinkPHP multi-language is implemented?

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 dataDo you know how ThinkPHP multi-language is implemented?This is the English stateDo you know how ThinkPHP multi-language is implemented?This is the Chinese stateDo you know how ThinkPHP multi-language is implemented?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.phpConfiguration file modification
  • Add 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!

Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

MinGW - Minimalist GNU for Windows

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

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.