Spring Boot 學習(4)springboot中的國際化

寫在前面:最近利用晚上的時間在網上看視頻學習SpringBoot,這博客是對學習的一點點總結及記錄,技術是開源的、知識是共享的
如果你對Spring Boot 感興趣,可以關注我的動態,我們一起學習。用知識改變命運,讓家人過上更好的生活

相關文章:
Springboot 系列文章

在項目中,很多時候需要國際化的支持,這篇博客介紹一下 Spring Boot 項目中多語言國際化的使用。

Spring Boot 是默認支持國際化的,而且不需要寫過多的配置,只需要在resources/下創建國際化配置文件即可, 並且有默認的語言配置文件,當找不到其他語言的配置的時候,使用該文件進行展示。

一、springmvc實現國際化的步驟

  1. 編寫國際化配置文件
  2. 使用ResourceBundleMessageSource管理國際化資源文件
  3. 在頁面使用fmt:message取出國際化內容

二、springboot 實現國際化的步驟

1. 編寫國際化配置文件,抽取頁面需要顯示的國際化消息

1.1 新建一個名叫“i18n”的包,我們用來存放國際化配置,然後在這個包下,我們再創建幾個properties的配置文件(文件名_區域_語言.properties),用來配置語言:

在這裏插入圖片描述

1.2 點擊login_en_US.properties 的配置文件,然後點擊下邊如圖所示的Resource Bundle按鈕在這裏插入圖片描述

1.3 寫配置,添加屬性

在這裏插入圖片描述
1.4 分別填寫右邊的提示信息,第一個是默認的、第二個是英文、第三個是中文
在這裏插入圖片描述

填寫完成以後:如下圖所示:
在這裏插入圖片描述

2. 使用ResourceBundleMessageSource管理國際化資源文件

首先看看底層源碼

@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;
}

從源碼可以看出:SpringBoot自動配置好了管理國際化資源文件的組件

4. 在配置文件中指定國際化資源文件的文件路徑

在 application.properties 中

#指定從哪個包下面找ָ
spring.messages.basename=i18n/login

將國際化配置文件讓Spring Boot 配置的 ResourceBundleMessageSource管理起來,讓我們的配置生效

5. 去頁面獲取國際化的值

語法是

Message Expressions: #{...}

在登錄頁面的html裏面按一下的方式修改代碼
在這裏插入圖片描述

注意input輸入框不能使用th:text取值,text是標籤裏面的內容,input輸入框是字節數,沒有標籤體

使用 thymeleaf的行內表達式,雙中括號裏面寫表達式

<p>Hello, [[${session.user.name}]]!</p>

在這裏插入圖片描述
注意:在這沒有提示,需要手動輸入

配置好以後,瀏覽器切換語言格式的時候將會切換語言

<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>

在這裏插入圖片描述

6. 點擊鏈接實現國際化

6.1 爲了讓自定義的配置生效,覆蓋或改變默認的配置,新建一個文件 MyLocaleResolver,用來實現 LocaleResolver 接口的作用

/**
 * @Description: 可以在鏈接上面攜帶區域信息
 * @Author: zhangxy
 * 
 */
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        // 獲取系統默認的
        Locale locale = Locale.getDefault();
        // 如果參數帶了區域信息,則使用參數的
        if (!StringUtils.isEmpty(l)) {
            String[] s = l.split("_");
            locale = new Locale(s[0], s[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                          Locale locale) {

    }
}

6.2 爲了使得區域解析器生效,需要把它加入到容器中

在mvc的配置類裏面添加一個組件

 	@Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

請求參數沒有帶區域信息,將使用系統默認的
在這裏插入圖片描述

點擊 English, 將會切換到英文
在這裏插入圖片描述

可以看到瀏覽器請求路徑帶了區域信息


由於水平有限,本博客難免有不足,懇請各位大佬不吝賜教!

發佈了89 篇原創文章 · 獲贊 2181 · 訪問量 32萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章