i18n实现SpringBoot后端多语言化(前后端分离)

2022-08-19 08:46:45

最近注意到许多项目都是支持语种切换的,于是用springboot实现了前后端分离的语种切换

依赖

在pom.xml中添加如下依赖

<dependency><groupId>org.webjars.bower</groupId><artifactId>jquery-i18n-properties</artifactId><version>1.2.7</version></dependency>

开始配置

说到配置就令人头疼,所幸的是springboot极大的简化了开发中繁琐的配置。


1.创建.propertis语言包

  • message.properties
  • message_en_US.properties
  • message_zh_CN.properties
    命名规则:前缀_语种类型.properties,其中message.properties文件内容可为空。但必须定义该文件优先级最高

在这里插入图片描述

添加内容

在这里插入图片描述
在这里插入图片描述

由于文件格式非UTF-8,我这里将汉字转为了unicode字符,但这并不影响读取。message.properties中未定义任何内容。

2.在application.properties中添加如下内容

	#i18n
	spring.messages.basename=static.i18n.message
	spring.messages.cache-duration=3600
	spring.messages.encoding=UTF-8

其中最主要的是basename,指定了语言包所在位置。basename的值结尾最后需要加上properties语言包文件名的前缀

3.创建解析器和拦截器

importjava.util.Locale;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;importorg.springframework.web.servlet.i18n.LocaleChangeInterceptor;importorg.springframework.web.servlet.i18n.SessionLocaleResolver;@ConfigurationpublicclassLocaleConfig{/** 
 	*	默认解析器 其中locale表示默认语言,当请求中未包含语种信息,则设置默认语种
 	*	当前默认为CHINA,zh_CN
 	*/@BeanpublicSessionLocaleResolverlocaleResolver(){SessionLocaleResolver localeResolver=newSessionLocaleResolver();
		localeResolver.setDefaultLocale(Locale.CHINA);return localeResolver;}/** 
	   *  默认拦截器 其中lang表示切换语言的参数名 
	   *  拦截请求,获取请求参数lang种包含的语种信息并重新注册语种信息
	 */@BeanpublicWebMvcConfigurerlocaleInterceptor(){returnnewWebMvcConfigurer(){@OverridepublicvoidaddInterceptors(InterceptorRegistry registry){LocaleChangeInterceptor localeInterceptor=newLocaleChangeInterceptor();
				localeInterceptor.setParamName("lang");
				registry.addInterceptor(localeInterceptor);}};}}

localeResolver其中定义了默认语种。localeInterceptor前端请求参数lang中包含指定语种时,将该语种注册到Locale中


4.编写控制器

importjava.util.HashMap;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.MessageSource;importorg.springframework.context.i18n.LocaleContextHolder;importorg.springframework.web.bind.annotation.CrossOrigin;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/domain")//解决跨域问题:指定允许跨域的域名@CrossOrigin(origins="http://test.do")publicclassDomainControl{@AutowiredprivateMessageSource messageSource;/**
	 * 多语言测试
	 */@RequestMapping("/i18ntest")publicMap<Object,Object>i18nTest(){Map<Object,Object> result=newHashMap<Object,Object>();
		result.put("code",5001);
		result.put("msg", messageSource.getMessage("error.error_5001",null,LocaleContextHolder.getLocale()));return result;}}

请求结果

lang=zh_CN

在这里插入图片描述

lang=en_US

在这里插入图片描述

  • 作者:全世界的见闻
  • 原文链接:https://blog.csdn.net/JavaLiXL/article/details/107883555
    更新时间:2022-08-19 08:46:45