SpringBoot page internationalization transformation

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




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 {
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");//Get address information
        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
            locale= new Locale(s[0],s[1]);
        return locale;

    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
    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











Posted by Wireless102 on Wed, 01 Jun 2022 02:33:58 +0530