SpringBoot---页面国际化i18n

①在根目录下创建i18n 文件夹,按照idea的格式写三个文件

  • login.properties
  • login_en_US.properties
  • login_zh_CN.properties

②根据MessageSourceAutoConfiguration在application.properties 中配置
spring.message.basename=i18n.login
③用国际化消息表达式#{}在页面中编写

④要是想要实现页面中英文切换的话,就需要自己去拓展MVC功能了

package com.ty.config;

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;

import static javax.swing.JComponent.getDefaultLocale;

public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lan = request.getParameter("lan");
        Locale locale = getDefaultLocale();
        if (!StringUtils.isEmpty(lan)){
            String[] split = lan.split("_");
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

  **其中配置类的方法名一定要是localeResolver**
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

你可能感兴趣的:(SpringBoot---页面国际化i18n)