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就烦!!!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。