SpringBoot国际化(jsp)

原来的项目用的是jsp,改成html太麻烦,这里介绍一下jsp国际化的(如果是html使用thymeleaf,它自带了的):

#国际化标签

1.项目路径如图:


image.png

2.相关依赖:


        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        
        
        
            javax.servlet
            javax.servlet-api
        
        
            javax.servlet
            jstl
        
    

3.新增I18nConfig(设置默认语言)

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;

/**
 * 设置默认语言
 *
 * @author xxx
 * @date 2019/12/30
 * @since 1.0.0
 */
@Configuration
public class I18nConfig {

    private static Logger logger = LoggerFactory.getLogger(I18nConfig.class);

    @Bean(name = "localeResolver")
    public LocaleResolver localeResolver() {
        logger.info("创建cookieLocaleResolver");

        LocaleResolver localeResolver = new LocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        logger.info("cookieLocaleResolver:");
        return localeResolver;
    }

}

4.新增LocaleResolver(修改local的值)文件

import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 每次加载页面时都会被调用,加载页面时都会设置并修改语言
 *
 * @author xxx
 * @date 2019/12/30
 * @since 1.0.0
 */
public class LocaleResolver extends AcceptHeaderLocaleResolver {

    private Locale myLocal;

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取cookie数组
        Cookie[] cookies = request.getCookies();
        String lang = "";
        if (cookies != null) {
            //遍历cookie数组
            for (Cookie cookie : cookies) {
                if ("ClientLanguage".equals(cookie.getName())) {
                    lang = cookie.getValue();
                    break;
                }
            }
        }
        LocaleResolver localeResolver = (LocaleResolver) RequestContextUtils.getLocaleResolver(request);
        if (lang == null || "".equals(lang)) {
            myLocal = Locale.CHINA;
        } else {
            if (lang.equals("zh")) {
                myLocal = Locale.CHINA;
            } else if (lang.equals("en")) {
                myLocal = Locale.ENGLISH;
            }
        }
        return myLocal;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        myLocal = locale;
    }
}

5.application.yml文件

spring:
  http:
    encoding:
      charset: UTF-8
  mvc:
    view:
      prefix: /WEB-INF/view/
      suffix: .jsp
  messages:
    basename: i18n.messages
    cache-duration: 3600
    encoding: UTF-8
server:
  port: 80
  tomcat:
    uri-encoding: UTF-8
    max-threads: 100
    min-spare-threads: 30
  servlet:
    context-path: /ChineseDemo

6.新建messages.properties,messages_zh.properties,messages_en.properties个文件:

  • messages为默认语言,zh中文,en英文
  • messages和zh文件内容:
commom.xxx.logout=登出
commom.xxx.login=登录
  • en文件内容:
commom.xxx.logout=logout
commom.xxx.login=login

7.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>



    
    
    Insert title here





 中文 
 english 



效果:


中文

英文

你可能感兴趣的:(SpringBoot国际化(jsp))