springboot2.0配合thymeleaf实现页面国际化

springboot2.0配合thymeleaf实现页面国际化

1. 引入thymeleaf


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>cn.jfjbgroupId>
    <artifactId>crudartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>crudname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

2. 新建一个“i18n”的包,用来存放国际化配置

springboot2.0配合thymeleaf实现页面国际化_第1张图片

写入如下内容

3. application.yml中添加配置参数

spring:
  messages:
    basename: i18n.login

4. 编写控制器页面

package cn.jfjb.crud.controller;

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

/**
 * @author john
 * @date 2019/11/22 - 19:38
 */
@Controller
@RequestMapping({"/"})
public class HelloController {
   
    @RequestMapping({"/"})
    public String hello12() {
        return "index";
    }
}

5. 编写模板页面


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1 th:text="#{login.name}">h1>
<h1 th:text="#{login.password}">h1>
body>
html>

使用th标签之前需要在页面加上xmlns,加上这个之后会有提示!


然后切换浏览器上面的语言然后刷新页面就会变化

如果出现乱码,需要设置idea的编码
springboot2.0配合thymeleaf实现页面国际化_第2张图片

测试

springboot2.0配合thymeleaf实现页面国际化_第3张图片

配置使其可以当请求参数中携带语言参数时自动切换语言

1. 编写自定义的LocalResolver

package cn.jfjb.crud.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;

/**
 * @author john
 * @date 2019/11/23 - 21:08
 */
public class MyLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest req) {
        // 获取l后面的参数
        String l = req.getParameter("l");
        Locale locale = Locale.getDefault();
        //没有l使用默认配置,有l使用自定义配置
        if (!StringUtils.isEmpty(l)) {
            String[] split = l.split("_");
            // 第一个是语言代码  第二个是国家代码
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

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

    }
}

2. 把配置加入到SpringBoot的容器中

package cn.jfjb.crud.config;

import cn.jfjb.crud.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.WebMvcConfigurer;

/**
 * @author john
 * @date 2019/11/23 - 18:01
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

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

}

测试

springboot2.0配合thymeleaf实现页面国际化_第4张图片

springboot2.0配合thymeleaf实现页面国际化_第5张图片

springboot2.0配合thymeleaf实现页面国际化_第6张图片

参考

SpringBoot2.0(十二):国际化

你可能感兴趣的:(springboot)