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)文章
IntelliJ IDEA中查看文件內(nèi)所有已聲明的方法(類似eclipse的outline)
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中查看文件內(nèi)所有已聲明的方法(類似eclipse的outline),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
SpringBoot配置Actuator組件,實現(xiàn)系統(tǒng)監(jiān)控
在生產(chǎn)環(huán)境中,需要實時或定期監(jiān)控服務(wù)的可用性。Spring Boot的actuator(健康監(jiān)控)功能提供了很多監(jiān)控所需的接口,可以對應(yīng)用系統(tǒng)進行配置查看、相關(guān)功能統(tǒng)計等。2021-06-06
關(guān)于Mybatis動態(tài)sql中test的坑點總結(jié)
這篇文章主要介紹了關(guān)于Mybatis動態(tài)sql中test的坑點,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
idea如何debug看springsecurity的過濾器順序
這篇文章主要介紹了idea如何debug看springsecurity的過濾器順序,文中通過圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04

