SpringBoot系列之i18n国际化多语言支持教程

SpringBoot系列之i18n国际化多语言支持教程
文章目录
1、环境搭建
2、resource bundle资源配置
3、LocaleResolver类
4、I18n配置类
5、Thymeleaf集成
1、环境搭建
本博客介绍一下SpringBoot集成i18n,实现系统语言国际化处理,ok,先创建一个SpringBoot项目,具体的参考我的博客专栏:SpringBoot系列博客专栏链接

环境准备:

IntelliJ IDEA
Maven
项目集成:

Thymeleaf(模板引擎,也可以选jsp或者freemark)
SpringBoot2.2.1.RELEASE
2、resource bundle资源配置
ok,要实现国际化语言,先要创建resource bundle文件:
在resources文件夹下面创建一个i18n的文件夹,其中:

messages.properties是默认的配置
messages_zh_CN.properties是(中文/中国)
messages_en_US.properties是(英文/美国)
etc.
在这里插入图片描述

IDEA工具就提供了很简便的自动配置功能,如图,只要点击新增按钮,手动输入,各配置文件都会自动生成属性

messages.properties:

  1. messages.loginBtnName=登录~
  2. messages.password=密码~
  3. messages.rememberMe=记住我~
  4. messages.tip=请登录~
  5. messages.username=用户名~

5](https://img-blog.csdnimg.cn/20200427170021848.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQwMzQ3Ng==,size_16,color_FFFFFF,t_70)

messages_zh_CN.properties:

messages.loginBtnName=登录
messages.password=密码
messages.rememberMe=记住我
messages.tip=请登录
messages.username=用户名

messages_en_US.properties:

messages.loginBtnName=login
messages.password=password
messages.rememberMe=Remember me
messages.tip=Please login in
messages.username=userName

1
2
3
4
5
6
在项目的application.properties修改默认配置,让SpringBoot的自动配置能读取到resource bundle资源文件

配置i18n

默认是i18n(中文/中国)

spring.mvc.locale=zh_CN

配置resource bundle资源文件的前缀名eg:i18n是文件夹名,messages是资源文件名,支持的符号有.号或者/

spring.messages.basename=i18n.messages

设置缓存时间,2.2.1是s为单位,之前版本才是毫秒

spring.messages.cache-duration=1

设置资源文件编码格式为utf8

spring.messages.encoding=utf-8

1
2
3
4
5
6
7
8
9
10
注意要点:

spring.messages.basename必须配置,否则SpringBoot的自动配置将失效
MessageSourceAutoConfiguration.ResourceBundleCondition 源码:
protected static class ResourceBundleCondition extends SpringBootCondition {
//定义一个map缓存池
private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();

	@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		String basename = context.getEnvironment().getProperty("spring.messages.basename", "messages");
		ConditionOutcome outcome = cache.get(basename);//缓存拿得到,直接从缓存池读取
		if (outcome == null) {//缓存拿不到,重新读取
			outcome = getMatchOutcomeForBasename(context, basename);
			cache.put(basename, outcome);
		}
		return outcome;
	}

	private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context, String basename) {
		ConditionMessage.Builder message = ConditionMessage.forCondition("ResourceBundle");
		for (String name : StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(basename))) {
			for (Resource resource : getResources(context.getClassLoader(), name)) {
				if (resource.exists()) {
				//匹配resource bundle资源
					return ConditionOutcome.match(message.found("bundle").items(resource));
				}
			}
		}
		return ConditionOutcome.noMatch(message.didNotFind("bundle with basename " + basename).atAll());
	}
	//解析资源文件
	private Resource[] getResources(ClassLoader classLoader, String name) {
		String target = name.replace('.', '/');//spring.messages.basename参数值的点号换成斜杆
		try {
			return new PathMatchingResourcePatternResolver(classLoader)
					.getResources("classpath*:" + target + ".properties");
		}
		catch (Exception ex) {
			return NO_RESOURCES;
		}
	}

}

cache-duration在2.2.1版本,指定的是s为单位,找到SpringBoot的MessageSourceAutoConfiguration自动配置类

3、LocaleResolver类
SpringBoot默认采用AcceptHeaderLocaleResolver类作为默认LocaleResolver,LocaleResolver类的作用就是作为i18n的分析器,获取对应的i18n配置,当然也可以自定义LocaleResolver类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
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;

/**

  • 自定义LocaleResolver类
  • @author nicky
  • 修改记录
  • 修改后版本: 修改人: 修改日期: 2019年11月23日 修改内容:

*/
public class CustomLocalResolver implements LocaleResolver {

Logger LOG = LoggerFactory.getLogger(this.getClass());

@Nullable
private Locale defaultLocale;

public void setDefaultLocale(@Nullable Locale defaultLocale) {
    this.defaultLocale = defaultLocale;
}

@Nullable
public Locale getDefaultLocale() {
    return this.defaultLocale;
}

@Override
public Locale resolveLocale(HttpServletRequest request) {
    Locale defaultLocale = this.getDefaultLocale();//获取application.properties默认的配置
    if(defaultLocale != null && request.getHeader("Accept-Language") == null) {
        return defaultLocale;//http请求头没获取到Accept-Language才采用默认配置
    } else {//request.getHeader("Accept-Language")获取得到的情况
        Locale requestLocale = request.getLocale();//获取request.getHeader("Accept-Language")的值
        String localeFlag = request.getParameter("locale");//从URL获取的locale值
        //LOG.info("localeFlag:{}",localeFlag);
        //url链接有传locale参数的情况,eg:zh_CN
        if (!StringUtils.isEmpty(localeFlag)) {
            String[] split = localeFlag.split("_");
            requestLocale = new Locale(split[0], split[1]);
        }
        //没传的情况,默认返回request.getHeader("Accept-Language")的值
        return requestLocale;
    }
}

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

}

}

4、I18n配置类
I18n还是要继承WebMvcConfigurer,注意,2.2.1版本才是实现接口就可以,之前1.+版本是要实现WebMvcConfigurerAdapter适配器类的

import com.example.springboot.i18n.component.CustomLocalResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**

  • I18nConfig配置类
  • @author nicky.ma
  • 修改记录
  • 修改后版本: 修改人: 修改日期: 2019/11/24 11:15 修改内容:

*/
//Configuration必须加上,不然不能加载到Spring容器
@Configuration
//使WebMvcProperties配置类可用,这个可以不加上,本博客例子才用
@EnableConfigurationProperties({ WebMvcProperties.class})
public class I18nConfig implements WebMvcConfigurer{

//装载WebMvcProperties 属性
@Autowired
WebMvcProperties webMvcProperties;
/**
 * 定义SessionLocaleResolver
 * @Author nicky.ma
 * @Date 2019/11/24 13:52
 * @return org.springframework.web.servlet.LocaleResolver
 */

// @Bean
// public LocaleResolver localeResolver() {
// SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
// // set default locale
// sessionLocaleResolver.setDefaultLocale(Locale.US);
// return sessionLocaleResolver;
// }

/**
 * 定义CookieLocaleResolver
 * @Author nicky.ma
 * @Date 2019/11/24 13:51
 * @return org.springframework.web.servlet.LocaleResolver
 */

// @Bean
// public LocaleResolver localeResolver() {
// CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
// cookieLocaleResolver.setCookieName(“Language”);
// cookieLocaleResolver.setCookieMaxAge(1000);
// return cookieLocaleResolver;
// }

/**
 * 自定义LocalResolver
 * @Author nicky.ma
 * @Date 2019/11/24 13:45
 * @return org.springframework.web.servlet.LocaleResolver
 */
@Bean
public LocaleResolver localeResolver(){
    CustomLocalResolver localResolver = new CustomLocalResolver();
    localResolver.setDefaultLocale(webMvcProperties.getLocale());
    return localResolver;
}

/**
 * 定义localeChangeInterceptor
 * @Author nicky.ma
 * @Date 2019/11/24 13:45
 * @return org.springframework.web.servlet.i18n.LocaleChangeInterceptor
 */
@Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    //默认的请求参数为locale,eg: login?locale=zh_CN
    localeChangeInterceptor.setParamName(LocaleChangeInterceptor.DEFAULT_PARAM_NAME);
    return localeChangeInterceptor;
}

/**
 * 注册拦截器
 * @Author nicky.ma
 * @Date 2019/11/24 13:47
 * @Param [registry]
 * @return void
 */
@Override
public void addInterceptors(InterceptorRegistry registry) {
 registry.addInterceptor(localeChangeInterceptor()).addPathPatterns("/**");
}

}

注意要点:

旧版代码可以不加LocaleChangeInterceptor 拦截器,2.2.1版本必须通过拦截器
如下代码,bean的方法名必须为localeResolver,否则会报错
@Bean
public LocaleResolver localeResolver(){
CustomLocalResolver localResolver = new CustomLocalResolver();
localResolver.setDefaultLocale(webMvcProperties.getLocale());
return localResolver;
}
1
2
3
4
5
6
原理:
跟一下源码,点进LocaleChangeInterceptor类

DispatcherServlet是Spring一个很重要的分发器类,在DispatcherServlet的一个init方法里找到这个LocaleResolver的init方法

这个IOC获取的bean类名固定为localeResolver,写例子的时候,我就因为改了bean类名,导致一直报错,跟了源码才知道Bean类名要固定为localeResolver

抛异常的时候,也是会获取默认的LocaleResolver的

找到资源文件,确认,还是默认为AcceptHeaderLocaleResolver

配置了locale属性的时候,还是选用AcceptHeaderLocaleResolver作为默认的LocaleResolver

spring.mvc.locale=zh_CN
1
WebMvcAutoConfiguration.localeResolver方法源码,ConditionalOnMissingBean主键的意思是LocaleResolver没有自定义的时候,才作用,ConditionalOnProperty的意思,有配了属性才走这里的逻辑

拦截器拦截的请求参数默认为locale,要使用其它参数,必须通过拦截器设置 ,eg:localeChangeInterceptor.setParamName(“lang”);

LocalResolver种类有:CookieLocaleResolver(Cookie)、SessionLocaleResolver(会话)、FixedLocaleResolver、AcceptHeaderLocaleResolver(默认)、.etc
5、Thymeleaf集成
本博客的模板引擎采用Thymeleaf的,所以新增项目时候就要加上maven相关依赖,没有的话,自己加上:

org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web 1 2 3 4 5 6 7 8 ok,然后去找个bootstrap的登录页面,本博客已尚硅谷老师的例子为例,进行拓展,引入静态资源文件:

Thymeleaf的i18n支持是采用#符号的

SpringBoot i18n example
<body class="text-center">
	<form class="form-signin" action="dashboard.html">
		<img class="mb-4" th:src="@{asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
		<h1 class="h3 mb-3 font-weight-normal" th:text="#{messages.tip}">Please sign in</h1>
		<label class="sr-only" th:text="#{messages.username}">Username</label>
		<input type="text" class="form-control" th:placeholder="#{messages.username}" required="" autofocus="">
		<label class="sr-only" th:text="#{messages.password} ">Password</label>
		<input type="password" class="form-control" th:placeholder="#{messages.password}" required="">
		<div class="checkbox mb-3">
			<label>
      <input type="checkbox" value="remember-me" > [[#{messages.rememberMe}]]
    </label>
		</div>
		<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{messages.loginBtnName}">Sign in</button>
		<p class="mt-5 mb-3 text-muted">© 2019</p>
		<a class="btn btn-sm" th:href="@{/login(locale='zh_CN')} ">中文</a>
		<a class="btn btn-sm" th:href="@{/login(locale='en_US')} ">English</a>
	</form>

</body>

切换中文网页:

切换英文网页:

当然不点链接传locale的方式也是可以自动切换的,浏览器设置语言:

原理localeResolver类会获取Accept language参数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了简单易用的国际化i18n支持。以下是使用Spring Boot进行国际化的步骤: 1. 在src/main/resources目录下创建一个新的文件夹,命名为"i18n"。在该文件夹下创建多个属性文件,分别对应不同的语言。例如,可以创建messages.properties(默认语言)和messages_zh.properties(中文)。 2. 在属性文件中添加键值对,以便将文本翻译成不同的语言。例如,在messages.properties中可以添加"welcome.message=Welcome!",在messages_zh.properties中可以添加"welcome.message=欢迎!"。 3. 在Spring Boot的配置文件(application.properties或application.yml)中添加以下配置: ``` spring.messages.basename=i18n/messages spring.messages.fallback-to-system-locale=false ``` 这样配置后,Spring Boot将会自动加载位于i18n文件夹下的属性文件。 4. 在需要进行国际化的地方使用`@Autowired`注解注入`org.springframework.context.MessageSource`对象,并使用`getMessage`方法获取对应的文本。例如: ```java @Autowired private MessageSource messageSource; public String getWelcomeMessage() { return messageSource.getMessage("welcome.message", null, LocaleContextHolder.getLocale()); } ``` `LocaleContextHolder.getLocale()`方法可用于获取当前请求的语言环境。 5. 运行应用程序并访问相应的接口或页面,Spring Boot将会根据请求的语言环境自动加载对应的属性文件,实现国际化效果。 这些是使用Spring Boot进行国际化的基本步骤,你可以根据需要进行进一步的定制和扩展。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值