一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - springboot+thymeleaf國際化之LocaleResolver接口的示例

springboot+thymeleaf國際化之LocaleResolver接口的示例

2021-02-01 12:04幽魂步 Java教程

本篇文章主要介紹了springboot+thymeleaf國際化之LocaleResolver的示例 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

springboot中大部分有默認配置所以開發(fā)起項目來非常迅速,僅對需求項做單獨配置覆蓋即可

spring采用的默認區(qū)域解析器是AcceptHeaderLocaleResolver,根據(jù)request header中的accept-language值來解析locale,并且是不可變的。

那么想要實現(xiàn)國際化,就要使用SessionLocaleResolver或者CookieLocaleResolver。正如類的名字所示,是按session或cookie中儲存的locale值來解析locale。

我就以SessionLocaleResolver舉例:

1.建立一個配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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è)置了默認的locale則按默認值來

?
1
2
//    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 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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的部分源碼

?
1
2
3
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中方法

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

然后看SessionLocaleResolver中setLocaleContext 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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ì)上就是一些非空判斷取默認,最終給session中的對應(yīng)屬性賦值

4.thymeleaf頁面中調(diào)用

?
1
2
3
4
5
6
7
8
9
10
11
<!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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/wqbill/p/5773338.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本激情小说 | 日本sss在线高清观看 | 成人影院在线观看免费 | 欧美成人乱弄视频 | 亚洲欧美综合人成野草 | 好大~好爽~再进去一点 | 369手机看片 | 91精品免费观看老司机 | 欧美不卡一区二区三区 | 国产一区二区精品久久91 | 免费看视频高清在线观看 | japanesexxxx日本妞 | h片在线看 | 免费一级欧美大片在线观看 | 日本道高清| 久久草香蕉频线观 | 亚洲人成网站在线观看青青 | yellow视频免费观看播放 | 国产日本免费 | 色五月天天 | 午夜国产 | 女教师巨大乳孔中文字幕免费 | 免费观看二十女人一摸是水 | 色综合亚洲天天综合网站 | 国内精品久久久久香蕉 | 青青青国产在线观看 | 亚洲欧洲日产国码无码av | 男人j进女屁股视频在线观看 | 成人影院免费在线观看 | 亚洲电影第1页 | 99re这里都是精品 | 成人免费一区二区三区在线观看 | 国产成人综合亚洲一区 | 14一15sexvideo日本 | 欧美成人aletta ocean | 99视频在线看观免费 | 亚洲国产精品一区二区首页 | 精品国产在天天线在线麻豆 | 国产亚洲综合精品一区二区三区 | 成人依依网 | 亚洲国产日韩欧美一区二区三区 |