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.
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.
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 filethinkphp/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
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
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 stateThis is the Chinese stateThe implementation of multi-language is completed .
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 modificationPersistence 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!