**1)、編寫國際化配置文件;**
2)、使用ResourceBundleMessageSource管理國際化資源文件
3)、在頁面使用fmt:message取出國際化內容
步驟:
1)、編寫國際化配置文件,抽取頁面需要顯示的國際化消息

2)、SpringBoot自動配置好了管理國際化資源文件的組件;
```java
@ConfigurationProperties(prefix = "spring.messages")
public class MessageSourceAutoConfiguration {
/**
* Comma-separated list of basenames (essentially a fully-qualified classpath
* location), each following the ResourceBundle convention with relaxed support for
* slash based locations. If it doesn't contain a package qualifier (such as
* "org.mypackage"), it will be resolved from the classpath root.
*/
private String basename = "messages";
//我們的配置文件可以直接放在類路徑下叫messages.properties;
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (StringUtils.hasText(this.basename)) {
//設置國際化資源文件的基礎名(去掉語言國家代碼的)
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(this.basename)));
}
if (this.encoding != null) {
messageSource.setDefaultEncoding(this.encoding.name());
}
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
messageSource.setCacheSeconds(this.cacheSeconds);
messageSource.setAlwaysUseMessageFormat(this.alwaysUseMessageFormat);
return messageSource;
}
```
3)、去頁面獲取國際化的值;

```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html">
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label class="sr-only" th:text="#{login.username}">Username</label>
<input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"/> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">? 2017-2018</p>
<a class="btn btn-sm">中文</a>
<a class="btn btn-sm">English</a>
</form>
</body>
</html>
```
效果:根據瀏覽器語言設置的信息切換了國際化;
原理:
? 國際化Locale(區域信息對象);LocaleResolver(獲取區域信息對象);
```java
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.mvc", name = "locale")
public LocaleResolver localeResolver() {
if (this.mvcProperties
.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) {
return new FixedLocaleResolver(this.mvcProperties.getLocale());
}
AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
return localeResolver;
}
默認的就是根據請求頭帶來的區域信息獲取Locale進行國際化
```
4)、點擊鏈接切換國際化
```java
/**
* 可以在連接上攜帶區域信息
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l = request.getParameter("l");
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(l)){
String[] split = l.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
```
- Spring Boot 入門
- Spring Boot 簡介
- 微服務
- 環境準備
- MAVEN設置
- IDEA設置
- Spring Boot HelloWorld
- 創建一個maven工程;(jar)
- 導入spring boot相關的依賴
- 編寫一個主程序;啟動Spring Boot應用
- 編寫相關的Controller、Service
- 運行主程序測試
- 簡化部署
- Hello World探究
- POM文件
- 父項目
- 啟動器
- 主程序類,主入口類
- 使用Spring Initializer
- IDEA使用 Spring Initializer
- STS使用 Spring Starter Project快速創建項目
- 配置文件
- 配置文件
- YAML語法
- 基本語法
- 值的寫法
- 普通的值(數字,字符串,布爾)
- 對象、Map(屬性和值)(鍵值對)
- 數組(List、Set)
- 配置文件值注入
- 其他問題
- properties配置文件在idea中默認utf-8可能會亂碼
- @Value獲取值和@ConfigurationProperties獲取值比較
- 配置文件注入值數據校驗
- @PropertySource&@ImportResource&@Bean
- 配置文件占位符
- 隨機數
- 占位符獲取之前配置的值
- Profile
- 多Profile文件
- yml支持多文檔塊方式
- 激活指定profile
- 配置文件加載位置
- 外部配置加載順序
- 自動配置原理
- 自動配置原理
- 細節
- @Conditional派生注解(Spring注解版原生的@Conditional作用)
- 日志
- 日志框架
- SLF4j使用
- 如何在系統中使用SLF4j
- 遺留問題
- SpringBoot日志關系
- 日志使用
- 默認配置
- 指定配置
- 切換日志框架
- Web開發
- 簡介
- SpringBoot對靜態資源的映射規則
- 模板引擎
- 引入thymeleaf
- Thymeleaf使用
- 語法規則
- SpringMVC自動配置
- Spring MVC auto-configuration
- 擴展SpringMVC
- 全面接管SpringMVC
- 如何修改SpringBoot的默認配置
- RestfulCRUD
- 默認訪問首頁
- 國際化
- 登陸
- 攔截器進行登陸檢查
- CRUD-員工列表
- thymeleaf公共頁面元素抽取
- CRUD-員工添加
- CRUD-員工修改
- CRUD-員工刪除
- 錯誤處理機制
- SpringBoot默認的錯誤處理機制
- 如果定制錯誤響應
- 如何定制錯誤的頁面
- 如何定制錯誤的json數據
- 將我們的定制數據攜帶出去
- 配置嵌入式Servlet容器
- 如何定制和修改Servlet容器的相關配置
- 注冊Servlet三大組件【Servlet、Filter、Listener】
- 替換為其他嵌入式Servlet容器
- 嵌入式Servlet容器自動配置原理
- 嵌入式Servlet容器啟動原理
- 使用外置的Servlet容器
- 步驟
- 原理
- Docker
- 簡介
- 核心概念
- 安裝Docker
- 安裝linux虛擬機
- 在linux虛擬機上安裝docker
- Docker常用命令&操作
- 鏡像操作
- 容器操作
- 安裝MySQL示例
- SpringBoot與數據訪問
- JDBC
- 整合Druid數據源
- 整合MyBatis
- 注解版
- 配置文件版
- 整合SpringData JPA
- SpringData簡介
- 整合SpringData JPA
- 啟動配置原理
- 創建SpringApplication對象
- 運行run方法
- 事件監聽機制
- 自定義starter