SpringBoot 国际化 (多语言)

该博客介绍了如何在SpringBoot项目中实现国际化,包括配置文件MyLocaleResolver和MyMvcConfig的设置,Controller中页面跳转的实现,以及启动类I18nResApplication的配置。同时详细列举了不同语言的资源文件,如messages.properties、messages_en_US.properties和messages_zh_CN.properties,以及Thymeleaf静态资源和项目其他配置文件的作用。
该文章已生成可运行项目,

目录

项目架构:

两个配置文件(MyLocaleResolver、MyMvcConfig):

Controller文件实现页面跳转:

启动类(I18nResApplication.java):

国际化资源目录文件1:(messages.properties)

国际化资源目录文件2:(messages_en_US.properties)

国际化资源目录文件3:(messages_zh_CN.properties)

Thymeleaf静态资源文件:

配置文件(application.properties):

Pom.xml依赖文件:


多语言:中英文切换进行不同语言的切换,实现“国际化”

项目架构:

两个配置文件(MyLocaleResolver、MyMvcConfig):

MyLocaleResolver.java:

package com.chenke.config;

import org.springframework.stereotype.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;

@Component
public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        Locale locale = Locale.getDefault();
        // 获取请求中的语言参数
        String language = httpServletRequest.getParameter("lang");
        // 验证参数的有效性
        if(StringUtils.hasLength(language)){
            //zh_CN
            String[] split = language.split("_");
            //国家,地区
            locale = new Locale(split[0], split[1]);
        }

        return locale;
    }

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

    }
}

MyMvcConfig.java:

package com.chenke.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.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

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

}

Controller文件实现页面跳转:

package com.chenke.controller;

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

@Controller
public class InternationalTest {

    @RequestMapping("/index.html")
    public String test(){
        return "index";
    }

}

启动类(I18nResApplication.java):

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class I18nResApplication {

    public static void main(String[] args) {
        SpringApplication.run(I18nResApplication.class, args);
    }

}

国际化资源目录文件1:(messages.properties)

login.tip = "请登录"
login.username = "用户名"
login.password = "密码"
login.btn = "提交"
login.remeber = "记住"

国际化资源目录文件2:(messages_en_US.properties)

login.tip = "Please login: "
login.username = "Username: "
login.password = "Password: "
login.btn = "Submit"
login.remeber = "Remember"

国际化资源目录文件3:(messages_zh_CN.properties)

login.tip = "请登录"
login.username = "用户名"
login.password = "密码"
login.btn = "提交"
login.remeber = "记住"

Thymeleaf静态资源文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form>

    <h3 th:text="#{login.tip}" >Please Sign in</h3>

    <input type="text"  th:placeholder="#{login.username}" > <br>
    <input type="password" th:placeholder="#{login.password}"> <br>


    <input type="checkbox" value="rember-me">[[#{login.remeber}]] <br>

    <button type="submit" th:text="#{login.btn}" > Sign in</button>  <br>

    <a th:href="@{/index.html(lang='zh_CN')}" >中文</a> &nbsp;&nbsp;&nbsp;
    <a th:href="@{/index.html(lang='en_US')}">English</a>

</form>
</body>
</html>

配置文件(application.properties):

# 国际化资源文件位置
spring.messages.basename=i18n.messages
# messages 文件的缓存失效时间
spring.messages.cache-duration=1
# 属性配置文件中文乱码问题
spring.messages.encoding=utf-8
# 解决中文乱码
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
# 解决Tomcat中文乱码问题
server.tomcat.accesslog.encoding=UTF-8

Pom.xml依赖文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wys</groupId>
    <artifactId>i18n-res</artifactId>
    <version>1.0.0</version>
    <name>i18n-res</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <xxl-job.version>2.2.0</xxl-job.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值