Springboot项目的国际化 - i18n
springboot版本 2.4.0
目录
步骤一、添加Resource Bundle
STS/Eclipse安装ResourceBundleEditor
创建ResourceBundle文件
步骤二、配置中设置ResourceBundle的basename
步骤三、登陆页面
步骤四、自定义LocaleResovler
自定义LocaleResovler
步骤五、页面测试
步骤一、添加Resource Bundle
如果使用STS或者Eclipse开发项目,想要更方便的添加Resource Bundle文件,需要安装ResourceBundleEditor。
STS/Eclipse安装ResourceBundleEditor
help -> Eclipse Marketplace

搜索ResourceBundleEditor,点击install即可(笔者本机上已经安装,故搜索出来显示为installed)。

创建ResourceBundle文件
在项目的resources/static目录下创建i18n文件夹,在该文件夹下创建ResourceBundle文件。



填写Base Name - message, 选择语言和国家,比如选择英语、美国(en、US)并且添加到项目国际化所需要的语言列表中。
本例中添加三个 —— 中文,默认,英语。

创建ResourceBundle后,i18n下会有三个文件——message_en.properties、message_zh.properties、message.properties。打开任意一个文件,

添加键值对,一个键对应三个语言的值。比如添加Welcome键。

添加完成后三个文件都会有相应的键值。
注意:如果添加的中文时出现乱码,右击properties文件,选择properties(属性)项,修改text file encoding为utf-8
步骤二、配置中设置ResourceBundle的basename
由于resourcebundle在项目的 static/i18n下,在设置basename时需要加上以上路径
application.yml
1 2 3 | spring: messages: basename: static/i18n/message |
步骤三、登陆页面
login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <a th:href="@{/login(l='zh_CN')}">中文</a> <a th:href="@{/login(l='en_US')}">English</a> <h1 th:text="#{Login}"></h1> <form th:action="@{'/login'}" method="post"> <input type="text" name="username"/> <input type="password" name="password"/> <input type="checkbox" name="rememberme"/>[[#{Remember_Me}]]<br> <input type="submit" th:value="#{Login}"> </form> </body> </html> |
本例中使用了thymeleaf,引入thymeleaf的依赖即可。
1 2 3 4 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> |
步骤四、自定义LocaleResovler
在WebMvcAutoConfiguration.java中创建了默认的LocaleResovler,但有创建的条件——即项目路径下没有LocaleResovler这样的bean时才创建。所以在项目下创建自定义的LocaleResovler,该默认LocaleResovler就不会启用。
WebMvcAutoConfiguration.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Override @Bean @ConditionalOnMissingBean // 条件注解 @SuppressWarnings("deprecation") public LocaleResolver localeResolver() { if (this.webProperties.getLocaleResolver() == WebProperties.LocaleResolver.FIXED) { return new FixedLocaleResolver(this.webProperties.getLocale()); } if (this.mvcProperties.getLocaleResolver() == WebMvcProperties.LocaleResolver.FIXED) { return new FixedLocaleResolver(this.mvcProperties.getLocale()); } AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver(); Locale locale = (this.webProperties.getLocale() != null) ? this.webProperties.getLocale(): this.mvcProperties.getLocale(); localeResolver.setDefaultLocale(locale); return localeResolver; } |
自定义LocaleResovler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.example.demo.config; import java.util.Locale; import java.util.Objects; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.util.ObjectUtils; import org.springframework.web.servlet.LocaleResolver; public class CustomLocaleResovler implements LocaleResolver{ @Override public Locale resolveLocale(HttpServletRequest request) { HttpSession session = request.getSession(); String language = request.getParameter("l"); if (!ObjectUtils.isEmpty(language)) { String[] lanArrary = language.split("_"); Locale locale = new Locale(lanArrary[0], lanArrary[1]); session.setAttribute("Locale", locale); return locale; } Locale localeUsing = (Locale) session.getAttribute("Locale"); return Objects.isNull(localeUsing)?Locale.getDefault() : localeUsing; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } } |
步骤五、页面测试

本机环境默认是英语,所以一开始会显示英语。点击中文进行语言切换。

再切换成英文

