Recently the project is quite busy, did not write how article. The main content of this article is how to achieve the background of multiple languages.

preface

The company needs to write a multi – language background, also had not done before, to face Baidu then. This is the first time I’ve used the TP framework for two years that it comes with its own multilanguage switching capability. Here really give a thumbs up.

Let’s implement this function.

First, the background implementation of multiple languages

The framework used is V5.1.38 LTS

If you go to the config file config/app.php, you can see that the only configuration information for the language is these two parameters.

Lang_switch_on is the browser language that will be listened to automatically,

Default_lang is an argument that defaults to Chinese without switching to another languageAfter the configuration file is understood we need to create the lang directory for the required module and configure the language pack for the corresponding language.

Here only configured English and Chinese, if you also need other languages can be added.At this point, you’ll notice that none of the parameters are in the configuration language list. The configuration file was also not found.

But you can see this line of code in the documentation. Let’s go to the source code

In the source code, you can see that the name value of the language cookie is fixed, and the cookie’s validity period is 3600, which need to be adjusted according to their own projects.

The most important thing is to look at the list of allowed languages. Let’s go back to this variableIn the filethinkphp/library/think/Lang.phpFinally, we see a method, which is one of the methods presented in the documentSince we need to use multiple languages throughout the background, we need to configure it in Common

2. View switching language

Call directly from the view templatelanguageChangemethodsThere’s a variable in the view called$languageNameThis variable is retrieved directly from the configuration file and later adjusted to the database.

And then write the method in the controllerchangeLanguageIs used to handle cookie changes after language switchingThis is what our background page looks like at this point

Three, test,

After that, we can switch languages according to the language package we set

In the template we need to use the lang template tag to get the dataThis is the English stateThis is the Chinese stateAt this point the multilingual implementation is complete.

Four,

So much is said above, just to write out the steps of each step for you.

The key points are

  • application/config.phpModifying a Configuration file
  • Add the lang directory to the corresponding module and create the required language pack
  • View language switch calls the background interface to store cookie values. Stored in the think_var
  • Use the lang tag for templates

Insist on learning, insist on blogging and insist on sharing is the belief that Kaka has been holding since its inception. Hope in the big Internet articles can bring you a silk of help.