1. effect achieved: the default font language of the page is the language of the operating system. Click the Chinese font to be Chinese, and click the English font to be English
2 Implementation ideas:
1) Prepare internationalization configuration file;
2) Use ResourceBundleMessageSource to manage internationalization resource files
3) . use thymeleaf to retrieve international content on the page
3. specific implementation
1) Write an internationalization profile and extract the internationalization messages that need to be displayed on the page
Create the properties file idea in resources to help us automatically generate the corresponding resource directory
* * * solve the problem of haymeleaf fetching content garbled
Edit the corresponding information in the international file properties
2) SpringBoot automatically configures the components for managing internationalization resource files; MessageSourceAutoConfiguration
We only need to configure the path package name + distinguished name of the international resource file in the configuration file
spring.messages.basename=internationalization.login
3) Go to the page to get the internationalization value; thymeleaf uses \;{} to get the international value
In this way, the default is to obtain the Locale for internationalization according to the region information brought by the request header
4) Click the link to switch internationalization
If the region information parser is not configured, springboot helps us create the region information parser. By default, the region information of the request header is used. If there is one, we configure it ourselves
Configure your own area information parser
package com.example.springbootdemo.Assembly; import org.springframework.web.servlet.LocaleResolver; import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Locale; public class MylocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest httpServletRequest) { String l = httpServletRequest.getParameter("l");//Get address information System.out.println("____"+l); Locale locale=Locale.getDefault();//Default operating system zone information if(!StringUtils.isEmpty(l)){//Judge whether it is empty String[] s = l.split("_");//As per“_"segmentation System.out.println(s[0]+"_______"+s[1]); locale= new Locale(s[0],s[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }
Declare our zone information parser in the springmvc configuration class
//Add your own LocaleResolver //Method name must be this @Bean public LocaleResolver localeResolver(){ return new MylocaleResolver(); }
4. summary
1.springboot automatically configures two components for us
Internationalization Resource Manager: MessageSourceAutoConfiguration
Area information parser: configured in WebMvcAutoConfiguration