Spring Boot 12 国际化

建立多语言网站,可以获得更多用户流量 。多语言网站被称为国际化(i18n),与本地化相反 。
你比如京东、淘宝这类电商网站就是国际化的电商网站,它支持多国语言 。

注意: 国际化是一个由18个字符组成的单词,第一个字符为i,最后一个字符为n,因此通常缩写为 i18n 。
Spring通过为不同的语言环境使用Spring拦截器,语言环境解析器和资源包,为国际化(i18n)提供了广泛的支持 。在本文中,我将知道您使用Spring Boot构建了一个简单的多语言网站 。
你可以预览一下示例:
在示例中,语言环境信息位于URL的参数上 。语言环境信息将存储在Cookie中,并且用户不会在接下来的页面中重新选择语言 。
  • http://localhost:8080/SomeContextPath/login1?lang=zh
  • http://localhost:8080/SomeContextPath/login1?lang=en
URL上的语言环境信息的另一个示例:
  • http://localhost:8080/SomeContextPath/zh/login2
  • http://localhost:8080/SomeContextPath/en/login2
12.1 创建Spring Boot项目
Spring Boot 12 国际化

文章插图
 
我在这里为以下语言创建2个属性文件:中文、英语 。
i18n / messages_zh.properties
label.password=密码label.submit=登陆label.title= 登陆页面label.userName=用户名如果你的eclipse中显示如下编码形式:
label.password=\u5BC6\u7801label.submit=\u767B\u9646label.title= \u767B\u9646\u9875\u9762label.userName=\u7528\u6237\u540D修改首选项中的content Types下的:
Spring Boot 12 国际化

文章插图
 
 
第二步配置.proerties文件右键属性配置,设置其字符编码
Spring Boot 12 国际化

文章插图
 
i18n / messages_en.properties
label.password= Passwordlabel.submit= Loginlabel.title= Login Pagelabel.userName= User NameEclipse支持使用消息编辑器来编辑文件的信息 。
12.2 拦截器和LocaleResolver
Spring Boot 12 国际化

文章插图
 
您需要声明2个Spring bean,包括localeResolver和messageResource 。
LocaleResolver指定如何获取用户将使用的语言环境信息 。CookieLocaleResolver将从Cookie读取语言环境信息,以查找用户以前使用的语言 。
MessageResource 将加载属性文件内容
Spring Boot 12 国际化

文章插图
 
在Controller处理请求之前,它必须经过拦截器,您需要在该拦截器中注册LocaleChangeInterceptor,拦截器处理用户的语言环境更改 。
WebMvcConfig.JAVA
【Spring Boot 12 国际化】package me.laocat.i18n.config;import org.springframework.context.MessageSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.support.ReloadableResourceBundleMessageSource;import org.springframework.web.servlet.LocaleResolver;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import org.springframework.web.servlet.i18n.CookieLocaleResolver;import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@Bean(name = “localeResolver”)public LocaleResolver getLocaleResolver() {// Cookie本地化解析器CookieLocaleResolver resolver = new CookieLocaleResolver();resolver.setCookieDomain(“myAppLocaleCookie”);// 60 分钟resolver.setCookieMaxAge(60 * 60);return resolver;}@Bean(name = “messageSource”)public MessageSource getMessageResource() {// 可重新加载的资源包消息源ReloadableResourceBundleMessageSource messageResource = new ReloadableResourceBundleMessageSource();// 读 i18n/messages_xxx.properties file.// 例如: i18n/messages_en.propertiesmessageResource.setBasename(“classpath:i18n/messages”);messageResource.setDefaultEncoding(“UTF-8”);return messageResource;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 本地化修改拦截器LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();localeInterceptor.setParamName(“lang”);registry.addInterceptor(localeInterceptor).addPathPatterns(“/*”);}}12.3 控制器和视图
Spring Boot 12 国际化

文章插图
 
I18nController.java(在参数上设置本地化)
package me.laocat.i18n;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class I18nController {@RequestMapping(value = https://www.isolves.com/it/cxkf/kj/2020-09-03/{ “/”, “/login1” })public String staticResource(Model model) {return “login1”;}}


推荐阅读