First of all, we prepare some resources. The purpose is to create a folder i18n under the resource for better display effect , which is used to store the internationalization configuration folder. springboot has a
default internationalization automatic configuration. MessageSourceProperties is found to be a configuration file, the prefix is spring.meaagse, we click into this class: find the message and open the global configuration file of springboot at this time: configure the page internationalization value to get the internationalization value on the page, check the Thymeleaf documentation, find The message value operation is: #{…}. Let's go to the page to test and refresh the homepage: It means that our configuration files have all taken effect , but we want to be better! Can automatically switch between Chinese and English according to the button! Configure internationalization parsing:
There is an internationalized Locale (area information object) in Spring; there is a resolver called LocaleResolver (getting area information object) in it!
Let's go to our webmvc automatic configuration file and look for it! See SpringBoot default configuration:
AcceptHeaderLocaleResolver There is a method in this class
, so if we want to click the link now to make our internationalized resources take effect, we need to make our own Locale take effect!
Let's write our own LocaleResolver, which can carry regional information on the link!
Modify the jump link of the front-end page:
let's write a custom component class for processing!
In order to make our regionalization information take effect, inject beans under our own MvcConofig;
restart the project to take effect