1. 程式人生 > >idea Springboot2.0.6 Thymeleaf配置國際化

idea Springboot2.0.6 Thymeleaf配置國際化

前言:thymeleaf肯定得匯入pom檔案了

1.先在resource下新建一個資料夾,用來存國際化的配置檔案

2.在 i18n 下 新建一個register.properties(現在idea還沒有識別出來是在進行國際化配置)

現在長這樣

再建一個 register_en_US.properties

你會發現它神奇的變了(idea識別了你在國際化配置)接下來就好辦了,再來配箇中文的

idea很智慧的在左側生成了一箇中文的,

如果左側沒有的話。點選右側的加號

這樣也是可以的

接下來,編寫配置檔案

點這個

然後

最後

到此為止一個國際化的配置就配好了,如果還有其他的需要配置的,自己設定就好。

接下來編寫springboot的配置檔案,我使用的是yml檔案

spriungboot預設沒有生成application.yml,需要自己新建一個,右擊resources,新建一個File,吧application.yml填入即可

右邊具體寫什麼,是由左邊的檔名構成的,

spring:
  messages:
    basename: i18n.register
  thymeleaf:
    cache: false

#後面這兩行是取消了thymeleaf的快取,之後修改頁面的時候,按完 ctrl s之後再按ctrl F9,直接重新整理頁面就好了,不用再重啟伺服器了

在java下新建一個 解析區域資訊的類

這個類需要實現

org.springframework.web.servlet.LocaleResolver;

一定要匯入正確的包,否則,後果不堪設想,在導包正確的情況下,同時按下 Ctrl 和 O鍵,選中下面的兩個,點選ok 

package cn.junhui.i18ndemo.component;


import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocalResolver implements LocaleResolver {
    /*
   重寫本地區域化資訊
    */
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String lan = request.getParameter("lan
"); Locale locale = Locale.getDefault(); if (!StringUtils.isEmpty(lan)) { String[] split = lan.split("_"); locale = new Locale(split[0], split[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }

我用紅色標記的是之後在超連結上攜帶的字串,大家可以隨意編寫,只需要跟以後的一樣就行

再編寫一個配置類,

package cn.junhui.i18ndemo.config;

import cn.junhui.i18ndemo.component.MyLocalResolver;
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.WebMvcConfigurationSupport;


@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {

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

導包的時候注意一點,千萬不要導錯包,

接下來還需要一個Controller

package cn.junhui.i18ndemo.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class i18nController {
    @GetMapping("/i18n")
    public ModelAndView toIndex() {
        return new ModelAndView("Testi18n");
    }
}

配置基本寫好了,接下來來個頁面測試一下

<!DOCTYPE html>
<!--匯入thylemeaf的名稱空間-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>國際化測試</title>
</head>
<body>

<form action="">
    <table>
        <tr>
            <td>標籤內寫法(有提示)</td>
            <td>
                <label th:text="#{register.name}">Username</label>
                <input type="text" placeholder="Username" th:placeholder="#{register.name}" name="name">
            </td>
        </tr>
        <tr>
            <td>標籤外寫法(無提示)</td>
            <td>
                <input type="text">[[#{register.name}]]
            </td>
        </tr>
        <tr>
            <td>
                <a th:href="@{/i18n(lan='zh_CN')}">中文</a>
            </td>
            <td>
                <a th:href="@{/i18n(lan='en_US')}">英文</a>
            </td>
        </tr>
    </table>
</form>

</body>
</html>

大功基本已經告成,啟動springboot

因為我的谷歌瀏覽器的語言預設為中文,所以他直接就顯示是中文

下面是點選 英文 超連結顯示的,注意觀看位址列的變化

來張中文的

大功告成!

附:專案結構圖