SpringBoot利用Thymeleaf模板引擎实现页面信息国际化

  • 在resources下创建如下配置文件admin、before、common
    在这里插入图片描述
  1. 配置属性
  • adminMessages.properties
    在这里插入图片描述
  • adminMessages_en_US.properties
    在这里插入图片描述
  • adminMessages_zh_CN.properties
    在这里插入图片描述
  1. 在application.properties中添加如下配置
spring.messages.basename=i18n/admin/adminMessages,i18n/before/beforeMessages,i18n/common/commonMessages

在这里插入图片描述
4. 创建LocaleConfig配置类

package com.gh.firstdemo;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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;

/**
 * @author gaohan
 * @version 1.0
 * @date 2020/7/25 21:54
 */
@Configuration  // 标识当前类为配置类
@EnableAutoConfiguration    // TODO 不明该注解意思
public class LocaleConfig implements WebMvcConfigurer {
    /**
     * 根据用户本次会话过程中的语义设定语言区域
     * (如用户进入首页前选择的语言种类)
     * @return
     */
    @Bean   // 表明这个方法需要交给Spring进行管理
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.CHINA);
        return slr;
    }


    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 选择语言的参数名
        lci.setParamName("locale");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}
  1. 创建html页面
    在这里插入图片描述

admin.html

<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<span th:text="#{admin}"></span><br/>
<a th:href="@{/i18n/first}" th:text="#{return}"></a>
</body>
</html>

before.html

<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<span th:text="#{before}"></span><br>
<a th:href="@{/i18n/first}" th:text="#{return}"></a>
</body>
</html>

first.html

<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<a th:text="#{chinese.key}" th:href="@{/i18n/first(locale='zh_CN')}"></a>
<a th:text="#{english.key}" th:href="@{/i18n/first(locale='en_US')}"></a>
<br>
<a th:text="#{test.admin}" th:href="@{/i18n/admin}"></a>
<a th:text="#{test.before}" th:href="@{/i18n/before}"></a>
</body>
</html>
  1. 创建Controller

注意:框框处不能有斜杠,否则项目打包成jar运行时会访问不到页面
在这里插入图片描述

package com.gh.firstdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author gaohan
 * @version 1.0
 * @date 2020/7/25 22:11
 */
@Controller
@RequestMapping("i18n")
public class I18nTestController {

    @RequestMapping("/first")
    public String index(){
        return "i18n/first";
    }

    @RequestMapping("/admin")
    public String admin(){
        return "i18n/admin";
    }

    @RequestMapping("/before")
    public String before(){
        return "i18n/before";
    }
}
  1. 运行测试
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

项目demo源码如下:

https://github.com/YUNZE-GH/FirstSpringBootDemo.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值