In fact, thinkphp6 comes with a multi language logic, but it is too simple to meet the customer's needs in practical application. We need to upgrade on this basis to meet the real market demand.
1、 Implementation of multilingual logic for file variables:
Under the application directory APP, create a new folder lang to place multilingual language packs. Each language corresponds to a language pack file, and each language pack file returns an array of languages, as shown in Figure
Multi language pack
Then, where you need to use the language pack, you can load the corresponding language pack with the load class provided by thinkphp, for example, to load the English language pack:
Lang::load(app()->getRootPath() . app\lang\\en.php );
2、 Multilingual logic implementation of database:
The parameters, introductions and documents of most products are very complex, so we add a new language identification field lang in the product table of the database to identify the language of the modified product, that is, there is a row of independent data in the product table of the database for each product corresponding to each language, and then do a language switching function in the management background to switch to a certain language, Only the product data in this language will be displayed, and only the product data in the changed language will be added, deleted, or queried!
Multilingual lang field of database product table
Manage the language switching logic in the background
3、 Implementation of multilingual logic for front end and back end interaction:
We use cookies to implement the multilingual logic of front end and back end interaction. Because cookies can be read and written at both the client and server, it is the same. A lang field is saved in the cookie to identify and track the user's current language identity in real time. The front and back end use this identity to load the language package and database data. Here, three small logic designs are involved
1. When users switch languages on the website, we update the cookie in real time.
2. A field is designed in the management background to set the default language of the website. That is, when a user visits the website for the first time and there is no lang in the cookie, the default language specified by the management will be automatically loaded.
3. The website's internal page access URL contains a language identifier, that is, when a user directly accesses a multilingual product, it is used to load the language that identifies the product and overwrite the previous language. This is the highest priority, because it is equivalent to the language that the user directly specifies to access
Lang ID saved in the cookie
Administrator specifies default language
The language identifier of the content URL. Pay attention to the en
Well, using PHP's web framework thinkphp6 to develop the multilingual website system tutorial (I), the multilingual implementation logic is OK. If you have any questions or needs, you can leave a message to comment and exchange, or you can directly send a private message to me!