Springboot项目i18n国际化配置

Springboot项目i18n国际化配置

1、创建Springboot项目

创建Springboot项目,添加以下依赖:
Spring Web
Thymeleaf

2、Springboot项目i18n配置

thymelea模板配置:

  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
  messages:
    basename: i18n/messages

messages.properties:

login.username.placeholder=请输入用户名
login.password.placeholder=请输入密码
login.button.name=登录

messages_en_US.properties:

login.username.placeholder=Login
login.password.placeholder=Please input Password
login.button.name=Login

messages_zh_CN.properties:

login.username.placeholder=请输入用户名~
login.password.placeholder=请输入密码~
login.button.name=登录~

login.html:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
    <title>登录页</title>
</head>
<body>
    <form>
        <input name="username" type="input" th:placeholder="#{login.username.placeholder}">
        <input name="password" type="password" th:placeholder="#{login.password.placeholder}">
        <input type="button" th:value="#{login.button.name}"/>
    </form>
    <a th:href="@{/login(language='zh_CN')}">中文</a>
    <a th:href="@{/login(language='en_US')}">English</a>
</body>
</html>

MyConfig 配置类代码如下:

package com.business.config;

import org.junit.platform.commons.util.StringUtils;
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;

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

/**
 * @author whw
 * @Description 自定义配置类
 * @createTime 2021/9/25 22:47
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

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

    protected static class NativeLocaleResolver implements LocaleResolver {

        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String language = request.getParameter("language");
            System.out.println(language);
            Locale locale = Locale.getDefault();
            if (!StringUtils.isBlank(language)) {
                String[] splits = language.split("_");
                locale = new Locale(splits[0], splits[1]);
            }
            return locale;
        }

        @Override
        public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

        }
    }
}

测试接口类TestController.java内容如下:

/**
 * @author whw
 * @Description 测试接口类
 * @createTime 2021/9/21 19:38
 */
@Controller
public class TestController {

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        return "hello test";
    }

    @RequestMapping("/login")
    public String login(Model model) {
        return "login";
    }
}

3、i18n配置测试

打开启动类BusinessStudyApplication.java,启动Springboot项目,访问http://localhost:8080/login,切换中英文验证国际化配置是否生效。如下图示:

Springboot.gifSpringboot.gif

(完)

最后修改于:2022年10月03日 03:24

添加新评论