温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot+thymeleaf国际化之LocaleResolver接口有什么用

发布时间:2021-07-08 11:35:08 来源:亿速云 阅读:209 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“springboot+thymeleaf国际化之LocaleResolver接口有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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>
<h2 th:text="${#locale}"></h2>
<h2 th:text="#{sys.test}"></h2>
</body>
</html>

则显示en hello

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

以上是“springboot+thymeleaf国际化之LocaleResolver接口有什么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI