SpringBoot+Thymeleaf實現國際化

1.在pom.xml中添加Thymeleaf依賴

<properties>
	<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
</properties>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.#{…}消息表達式

​外部化文本就是從模板文件中提取的模板代碼片段,它們可以分開保存。最典型的就是保存在.properties文件中。因此它們可以被輕易地用對應的其他語言文本來代替,這就是國際化的處理。外部化文本片段通常被稱爲“message”消息。消息都有一個key來識別它們。Thymeleaf允許我們用#{…}指定text對應的消息。

Thymeleaf的standard message resolver會根據我們提供的key幫我們從src/main/resources目錄下與模板同名的.properties文件中把值取回來。

3.舉例

3.1語言配置文件

src/main/resources目錄下添加與模板同名.properties語言配置文件,先創建默認的配置文件,再創建各個語言的配置文件,各個語言配置文件名後面要加上對應語言的後綴,如英文_en、中文_zh。注意:各個語言都應該有一個對應的加上後綴名的配置文件。即使那種語言已在默認文件中了,也要有一個加了後綴的配置文件。如下所示:

home.properties(默認)

home.title=Application title
home.language.chinese=Chinese
home.language.english=English
home.welcome=Welcome here!

home_zh.properties(中文)

home.title=國際化示例
home.language.chinese=中文(簡體)
home.language.english=英語
home.welcome=歡迎到來!

home_en.properties(英文)

home.title=Application title
home.language.chinese=Chinese
home.language.english=English
home.welcome=Welcome here!

3.2添加模板

Spring Boot默認存放頁面模板的路徑在src/main/resources/templates或者src/main/view/templates,Thymeleaf默認的頁面文件後綴是.html。

home.html(模板)

<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="#{home.title}">Insert title here</title>
</head>
<body>
<div>
    <a data-th-href="@{/locale(lang=zh_CN)}" th:text="#{home.language.chinese}">中文</a>
    <a data-th-href="@{/locale(lang=en_US)}" th:text="#{home.language.english}">英語</a>
</div>
    <h1 data-th-text="#{home.welcome}">Fluid jumbotron</h1>
</body>
</html>

模板中通過#{…}消息表達式獲取.properties文件中的消息。

3.3配置類

配置區域信息解析器實現程序國際化。當攔截到請求中有參數lang=zh_CN之類的,就做語言切換。

package com.wong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;

@Configuration// 當系統啓動時,就會來配置WebConfig
public class WebConfig implements WebMvcConfigurer {


    @Bean//爲了讓區域信息解析器生效,將區域信息解析器註冊在容器中,配置加入到SpringBoot的容器中
    public LocaleResolver localeResolver(){
        /**
         * 通過定義區域信息解析器實現程序國際化
         */
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        // 設置默認語言爲中文即使用_zh.properties的文件
        localeResolver.setDefaultLocale(new Locale("zh","CN"));
        return localeResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        // 設置參數
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 攔截請求,註冊區域信息攔截器
        registry.addInterceptor(localeChangeInterceptor());
    }
}

3.4控制器代碼

首頁控制器HomeController.java:
可以通過http://ip:port/index、http://ip:port/home、http://ip:port/三種方式來訪問home.html模板。

package com.wong.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Locale;

@Controller
public class HomeController{

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = {"/index","/home","/"},method = RequestMethod.GET)
    public String getHomePage(Model model, Locale locale) {
        return "home";
    }
}

語言域控制器控制語言切換的LocaleController.java:
語言切換時,進行轉發,觸發語言切換。

package com.wong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LocaleController {

    @GetMapping("/locale")
    public String localeHandler(HttpServletRequest request){
        String lastUrl = request.getHeader("referer");
        return "redirect:"+lastUrl;
    }
}

4.運行項目

~/Desktop/WInternationalization$ mvn clean spring-boot:run

在這裏插入圖片描述
在這裏插入圖片描述

Demo歡迎下載學習。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章