Springboot 实现国际化
项目目录结构
创建国际化配置文件
在项目 resources 目录下创建 i18n 文件夹,用于存放国际化配置
创建 login.properties 默认语言配置文件(这里以登录页为例)
再创建对应中文语言配置 login_zh_CN.properties ,创建完后,会发现配置文件自动合并
继续创建对应英文配置 login_en_US.properties ,在合并文件处右击添加一个配置文件
添加 en_US 英文配置
login.properties:默认配置;
login_en_US.properties:英文配置;
login_zh_CN.properties:中文配置;
添加完配置后,就可进行抽取页面需要显示的国际化消息。
在这之前一定要检查下项目编码格式,并设置成UTF-8,否则国际化消息会乱码
idea - File - Settings - editor - File Encodings
国际化配置文件进行可视化编辑
国际化配置文件下方,会多出一个 Rescurce Bundle 选项,点击可进行可视化配置
对页面需要国际化的消息以 key-value 的形式进行配置
yml 添加国际化配置
spring:
# 国际化配置
messages:
basename: i18n.login
encoding: UTF-8
修改html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>登录</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/static/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/util.css">
<link rel="stylesheet" type="text/css" href="/static/css/main.css">
</head>
<body>
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100">
<div class="login100-form-title" style="background-image: url(/static/images/bg-01.jpg);">
<span class="login100-form-title-1" th:text="#{login.tip}">登 录</span>
</div>
<form class="login100-form validate-form">
<div class="wrap-input100 validate-input m-b-26" th:data-validate="#{login.usernameValidate}">
<span class="label-input100" th:text="#{login.username}">用户名</span>
<label>
<input class="input100" type="text" name="username" th:placeholder="#{login.usernamePlaceholder}"/>
</label>
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input m-b-18" th:data-validate="#{login.passwordValidate}">
<span class="label-input100" th:text="#{login.password}">密码</span>
<label>
<input class="input100" type="password" name="password" th:placeholder="#{login.passwordPlaceholder}"/>
</label>
<span class="focus-input100"></span>
</div>
<div class="flex-sb-m w-full p-b-30">
<div class="contact100-form-checkbox">
<input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
<label class="label-checkbox100" for="ckb1" th:text="#{login.remember}">记住我</label>
</div>
<div>
<a href="javascript:" class="txt1" th:text="#{login.forget}">忘记密码?</a>
</div>
</div>
<div class="container-login100-form-btn m-b-18">
<button class="login100-form-btn" th:text="#{login.btn}">登 录</button>
</div>
<div class="flex-sa-m w-full p-b-30">
<div>
<a class="txt1" th:href="@{/hello(l='zh_CN')}">中文</a>
</div>
<div>
<a class="txt1" th:href="@{/hello(l='en_US')}">English</a>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="/static/js/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="/static/js/main.js" type="text/javascript"></script>
</body>
</html>
国际化消息使用 thymeleaf 消息表达式 #{}
添加国际化解析器 MyLocaleResolver
package com.example.demo.common.config;
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;
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求中的语言参数
String language = request.getParameter("l");
//默认的;如果没有就使用默认的
Locale locale = Locale.getDefault();
//如果请求的了携带了国际化的参数
if(!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
将国际化解析器注册到 Spring 容器中
package com.example.demo.common.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.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}
/**
* 国际化配置
*/
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
测试
启动项目,进行中英文切换测试
可以发现,中英文可正常切换了。
Springboot 实现国际化完成!
内容如有帮助,记得点赞哦~