springboot+thymeleaf國際化之LocaleResolver接口的示例
springboot中大部分有默認(rèn)配置所以開發(fā)起項目來非常迅速,僅對需求項做單獨配置覆蓋即可
spring采用的默認(rèn)區(qū)域解析器是AcceptHeaderLocaleResolver,根據(jù)request header中的accept-language值來解析locale,并且是不可變的。
那么想要實現(xiàn)國際化,就要使用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" 是必須的
優(yōu)先級如下:
session中對應(yīng)屬性(3中有說明)有值則按session來
如果沒有但是SessionLocaleResolver設(shè)置了默認(rèn)的locale則按默認(rèn)值來
// SessionLocaleResolver localeResolver=new SessionLocaleResolver(); // localeResolver.setDefaultLocale(Locale.ENGLISH);
再然后就還是按request header中的accept-language值來
2.建立對應(yīng)的messages.properties
messages.properties
messages_en.properties
messages_zh_CN.properties
前面注釋的代碼則可以修改properties的前綴部分,name="messageSource" 同樣是必須的
比如 setBasename("msg"); 對應(yīng)properties則為
msg.properties
msg_en.properties
msg_zh_CN.properties
格式上sys.test=hello、sys.test=你好,應(yīng)該無需贅述(可能轉(zhuǎn)碼會避免一些問題,我這里直接放的中文)
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中默認(rèn)屬性名
可以看一下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默認(rèn)屬性名為
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);
}
本質(zhì)上就是一些非空判斷取默認(rèn),最終給session中的對應(yīng)屬性賦值
4.thymeleaf頁面中調(diào)用
<!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就煩?。?!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot配置ssl實現(xiàn)HTTPS的方法
這篇文章主要介紹了spring boot配置ssl實現(xiàn)HTTPS的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Java線程編程中isAlive()和join()的使用詳解
這篇文章主要介紹了Java線程編程中isAlive()和join()的使用詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實踐與指導(dǎo)詳解
這篇文章主要介紹了圖數(shù)據(jù)庫NebulaGraph的Java 數(shù)據(jù)解析實踐與指導(dǎo)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
java使用EasyExcel導(dǎo)入導(dǎo)出excel
導(dǎo)入導(dǎo)出excel數(shù)據(jù)是常見的需求,今天就來看一下Java基于EasyExcel實現(xiàn)這個功能,感興趣的朋友可以了解下2021-05-05
MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式
這篇文章主要介紹了MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

