springboot+thymeleaf国际化之LocaleResolver接口的示例

 更新时间:2017年11月08日 14:22:30   作者:幽魂步  
本篇文章主要介绍了springboot+thymeleaf国际化之LocaleResolver的示例 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

springboot中大部分有默认配置所以开发起项目来非常迅速,仅对需求项做单独配置覆盖即可

spring采用的默认区域解析器是AcceptHeaderLocaleResolver,根据request header中的accept-language值来解析locale,并且是不可变的。

那么想要实现国际化,就要使用SessionLocaleResolver或者CookieLocaleResolver。正如类的名字所示,是按session或cookie中储存的locale值来解析locale。

我就以SessionLocaleResolver举例:

1.建立一个配置类

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * Created by wq on 2016/8/15.
 */
@Configuration
public class SpringMVC_config {
  @Bean(name="localeResolver")
  public LocaleResolver localeResolverBean() {
    return new SessionLocaleResolver();
  }
//  @Bean(name="messageSource")
//  public ResourceBundleMessageSource resourceBundleMessageSource(){
//    ResourceBundleMessageSource source=new ResourceBundleMessageSource();
//    source.setBasename("messages");
//    return source;
//  }
}

注意 name="localeResolver" 是必须的

优先级如下:

session中对应属性(3中有说明)有值则按session来

如果没有但是SessionLocaleResolver设置了默认的locale则按默认值来

//    SessionLocaleResolver localeResolver=new SessionLocaleResolver();
//    localeResolver.setDefaultLocale(Locale.ENGLISH);

再然后就还是按request header中的accept-language值来

2.建立对应的messages.properties

messages.properties

messages_en.properties

messages_zh_CN.properties

前面注释的代码则可以修改properties的前缀部分,name="messageSource" 同样是必须的

比如 setBasename("msg"); 对应properties则为

msg.properties

msg_en.properties

msg_zh_CN.properties

格式上sys.test=hello、sys.test=你好,应该无需赘述(可能转码会避免一些问题,我这里直接放的中文)

3.controller中切换locale 

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;

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

import static org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME;

/**
 * Created by Administrator on 2016/6/11.
 */
@Controller
public class DemoController {
  @Autowired
  LocaleResolver localeResolver;

  @RequestMapping("test")
  public String test(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session=request.getSession();
    localeResolver.setLocale(request,response,Locale.ENGLISH);
    System.out.println(session.getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME));
    return "messages";
  }
}

这里org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME这个字符串常量则是session中默认属性名

可以看一下SessionLocaleResolver的部分源码

public class SessionLocaleResolver extends AbstractLocaleContextResolver {
  public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";
  public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".TIME_ZONE";

locale默认属性名为

org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE

setLocale是抽象类AbstractLocaleContextResolver中方法

  public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    this.setLocaleContext(request, response, locale != null?new SimpleLocaleContext(locale):null);
  }

然后看SessionLocaleResolver中setLocaleContext 

  public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
    Locale locale = null;
    TimeZone timeZone = null;
    if(localeContext != null) {
      locale = localeContext.getLocale();
      if(localeContext instanceof TimeZoneAwareLocaleContext) {
        timeZone = ((TimeZoneAwareLocaleContext)localeContext).getTimeZone();
      }
    }

    WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
    WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
  }

本质上就是一些非空判断取默认,最终给session中的对应属性赋值

4.thymeleaf页面中调用

<!DOCTYPE html>
<html lang="zh_CN"
   xmlns:th="http://www.thymeleaf.org">
<head>
  <title>msg</title>
</head>
<body>
<h1 th:text="${#locale}"></h1>
<h1 th:text="#{sys.test}"></h1>
</body>
</html>

则显示en hello

能用注解的,尽量不用xml,看着xml就烦!!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Spring简单容器中的Bean基本加载过程

    详解Spring简单容器中的Bean基本加载过程

    本篇将对定义在 XMl 文件中的 bean,从静态的的定义到变成可以使用的对象的过程,即 bean 的加载和获取的过程进行一个整体的了解
    2017-05-05
  • springboot多项目结构实现

    springboot多项目结构实现

    本文主要介绍了springboot多项目结构实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • eclipse中maven插件安装教程

    eclipse中maven插件安装教程

    这篇文章主要为大家详细介绍了eclipse中maven插件安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • JAVA内存模型(JMM)详解

    JAVA内存模型(JMM)详解

    这篇文章主要介绍了JAVA内存模型(JMM)详解的相关资料,需要的朋友可以参考下
    2022-12-12
  • java调用远程服务器的shell脚本以及停止的方法实现

    java调用远程服务器的shell脚本以及停止的方法实现

    这篇文章主要介绍了java调远程服务器的shell脚本以及停止的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 通过JDK源码角度分析Long类详解

    通过JDK源码角度分析Long类详解

    这篇文章主要给大家介绍了关于通过JDK源码角度分析Long类的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用long类具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • 关于Java垃圾回收开销降低的几条建议

    关于Java垃圾回收开销降低的几条建议

    垃圾回收(Garbage Collection)是Java虚拟机(JVM)垃圾回收器提供的一种用于在空闲时间不定时回收无任何对象引用的对象占据的内存空间的一种机制,下面这篇文章主要介绍了关于Java垃圾回收开销降低的几条建议,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • 深入了解Java核心类库--BigDecimal和System类

    深入了解Java核心类库--BigDecimal和System类

    这篇文章主要为大家详细介绍了javaBigDecimal和System类定义与使用的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助
    2021-07-07
  • Spring AOP底层源码详解

    Spring AOP底层源码详解

    这篇文章主要介绍了Spring AOP底层源码详解,帮助大家更好的理解和学习使用Spring AOP,感兴趣的朋友可以了解下
    2021-03-03
  • SpringBoot构建RESTful API的实现示例

    SpringBoot构建RESTful API的实现示例

    本文主要介绍了SpringBoot构建RESTful API的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05

最新评论