Home > PHP Framework > ThinkPHP > body text

Do you know how ThinkPHP multi-language is implemented?

咔咔
Release: 2020-08-10 14:11:38
Original
1964 people have browsed it

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!

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