如何使用Spring Boot+;配置国际化;百里香?

如何使用Spring Boot+;配置国际化;百里香?,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,在一个使用Thymeleaf作为模板渲染器的springboot项目中,根据用户区域设置,如何将一段文本翻译成多种语言 如何在Thymeleaf中显示这些消息?例如,Spring Boot 2.0.2.RELEASE 文件WebConfig.java // File /src/main/java/com/donhuvy/WebConfig.java package com.donhuvy; import org.springframework.boot.autoconfigure.Enable

在一个使用Thymeleaf作为模板渲染器的springboot项目中,根据用户区域设置,如何将一段文本翻译成多种语言


如何在Thymeleaf中显示这些消息?

例如,Spring Boot 2.0.2.RELEASE

文件
WebConfig.java

// File /src/main/java/com/donhuvy/WebConfig.java
package com.donhuvy;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * Configuration for overall application.
 */
//@EnableWebMvc
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class WebConfig extends WebMvcConfigurationSupport {

    /**
     * Switch language, default language is Vietnamese - Vy's mother tongue language.
     * If user would like to switch to other language, use parameter,
     * for example: http://localhost:8081/all?lang=en
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        // sessionLocaleResolver.setDefaultLocale(Locale.US);
        Locale vietnamLocale = new Locale("vi", "VN");
        sessionLocaleResolver.setDefaultLocale(vietnamLocale);
        return sessionLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry ir) {
        ir.addInterceptor(localeChangeInterceptor());
    }

}
需要3个文件
*。属性

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
文件
messages.properties

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
文件
消息\u en.properties

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
文件
messages_vi.properties

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
查看文件
ccy.html

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<!-- File: /src/main/resources/templates/cash/ccy.html -->

<!--<th:block th:include="fragments/header :: header"></th:block>-->
<!--For suggest css class purpose-->
<!--<link rel="stylesheet" href="../../static/bootstrap.min.css">-->
<th:block th:include="fragments/header"></th:block>

<div class="row">
    <div class="col-lg-12 col-md-12">
        <table class="table table-bordered table-striped">
            <thead class="thead-light">
            <tr>
                <th><span th:text="#{cash.list.order_number}"></span></th>
                <th><span th:text="#{cash.list.currency_code}"/></th>
                <th><span th:text="#{cash.list.description}"></span></th>
                <th><span th:text="#{cash.list.conversion_rate}"/></th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="currency : ${currencies}">
                <!--/*@thymesVar id="currency" type="com.donhuvy.entity.Ccy"*/-->
                <td><span th:text="${currency.currencyId}"/></td>
                <td><span th:text="${currency.currencyName}"/></td>
                <td><span th:text="${currency.ccyName}"/><td>
                <span th:text="${currency.convertRate}"/></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

<!--<th:block th:include="fragments/footer :: footer"></th:block>-->
<th:block th:include="fragments/footer"></th:block>

请参见特定语言的结果:

(默认语言)


参考资料:

例如,Spring Boot 2.0.2.RELEASE

文件
WebConfig.java

// File /src/main/java/com/donhuvy/WebConfig.java
package com.donhuvy;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * Configuration for overall application.
 */
//@EnableWebMvc
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class WebConfig extends WebMvcConfigurationSupport {

    /**
     * Switch language, default language is Vietnamese - Vy's mother tongue language.
     * If user would like to switch to other language, use parameter,
     * for example: http://localhost:8081/all?lang=en
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        // sessionLocaleResolver.setDefaultLocale(Locale.US);
        Locale vietnamLocale = new Locale("vi", "VN");
        sessionLocaleResolver.setDefaultLocale(vietnamLocale);
        return sessionLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry ir) {
        ir.addInterceptor(localeChangeInterceptor());
    }

}
需要3个文件
*。属性

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
文件
messages.properties

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
文件
消息\u en.properties

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
文件
messages_vi.properties

cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Currency code
cash.list.description=Description
cash.list.order_number=No.
cash.receipt.object.name=Object name
cash.list.conversion_rate=Conversion rate
cash.list.currency_code=Mã tiền tệ
cash.list.description=Mô tả
cash.list.order_number=STT
cash.receipt.object.name=Đối tượng
cash.list.conversion_rate=Tỷ giá
查看文件
ccy.html

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<!-- File: /src/main/resources/templates/cash/ccy.html -->

<!--<th:block th:include="fragments/header :: header"></th:block>-->
<!--For suggest css class purpose-->
<!--<link rel="stylesheet" href="../../static/bootstrap.min.css">-->
<th:block th:include="fragments/header"></th:block>

<div class="row">
    <div class="col-lg-12 col-md-12">
        <table class="table table-bordered table-striped">
            <thead class="thead-light">
            <tr>
                <th><span th:text="#{cash.list.order_number}"></span></th>
                <th><span th:text="#{cash.list.currency_code}"/></th>
                <th><span th:text="#{cash.list.description}"></span></th>
                <th><span th:text="#{cash.list.conversion_rate}"/></th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="currency : ${currencies}">
                <!--/*@thymesVar id="currency" type="com.donhuvy.entity.Ccy"*/-->
                <td><span th:text="${currency.currencyId}"/></td>
                <td><span th:text="${currency.currencyName}"/></td>
                <td><span th:text="${currency.ccyName}"/><td>
                <span th:text="${currency.convertRate}"/></td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

<!--<th:block th:include="fragments/footer :: footer"></th:block>-->
<th:block th:include="fragments/footer"></th:block>

请参见特定语言的结果:

(默认语言)

参考: