SpringBoot消息国际化配置实现过程解析
作者:与李 时间:2023-05-16 01:19:22
一、目的
针对不同地区,设置不同的语言信息。
SpringBoot国际化配置文件默认放在classpath:message.properties,如果自定义消息配置文件,需要application.properties或application.yml中设置spring.messages.basename的值。
二、步骤
在src/main/resources 下建i18n文件夹
在i18n文件夹中建立messages.properties 找不到语言配置时,使用此文件
hello=你好_默认
在i18n文件夹中建立messages_en_US.properties 英文语言配置
hello=hello_English
在i18n文件夹中建立messages_zh_CN.properties 中文语言配置
hello=你好_中文
MessageConfig.java
对消息的配置
package com.spring.security.config.spring;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.util.Assert;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AbstractLocaleContextResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class MessageConfig extends AbstractLocaleContextResolver{
@Value("${spring.messages.basename}")
public String[] basenames;
@Bean(name = "messageSource")
public ResourceBundleMessageSource resourceBundleMessageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
if (basenames != null) {
for (int i = 0; i < basenames.length; i++) {
String basename = basenames[i];
Assert.hasText(basename, "Basename must not be empty");
this.basenames[i] = basename.trim();
}
source.setBasenames(basenames);
} else {
this.basenames = new String[0];
source.setBasename(basenames[0]);
}
source.setDefaultEncoding("UTF-8");
source.setUseCodeAsDefaultMessage(true);
return source;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
}
/**
* 国际化,设置url识别参数
*
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public LocaleContext resolveLocaleContext(HttpServletRequest request) {
return null;
}
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response,
LocaleContext localeContext) {
}
}
SpringUtils.java
Spring工具类,用于获取ApplicationContext
package com.spring.security.common.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
/**
* Spring容器
*/
@Service
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (context == null) {
context = applicationContext;
}
}
/**
* 获取容器
*
* @return 容器
*/
public static ApplicationContext getContext() {
return context;
}
}
MessageUtils.java
封装获取message的工具类
package com.spring.security.common.utils;
import java.util.Locale;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
public class MessageUtils {
public static String getMessage(String code) {
Locale locale = LocaleContextHolder.getLocale();
ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
String message = reloadableResourceBundleMessageSource.getMessage(code, null, locale);
return message;
}
}
** WebMvcConfig.java**
mvc配置,解决跨域,接口中文乱码,添加语言 *
package com.spring.security.config.spring;
import java.nio.charset.Charset;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Autowired
private LocaleChangeInterceptor localeChangeInterceptor;
/**
* 解决跨域
*/
@Override
protected void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*")
.allowCredentials(true);
}
/**
* 配置消息转换器
* 解决返回String乱码
*/
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
}
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
return new StringHttpMessageConverter(Charset.forName("UTF-8"));
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(localeChangeInterceptor);
}
}
三、测试
测试接口:
package com.spring.security.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spring.security.common.utils.I18nUtils;
@RestController
public class TestController {
@GetMapping("/test")
public String doTest() {
return I18nUtils.getMessage("hello");
}
}
来源:https://www.cnblogs.com/yl-space/p/13383994.html
标签:Spring,Boot,消息,国际化,配置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java创建线程的七种方法总结(全网最全面)
2023-11-03 14:27:26
![](https://img.aspxhome.com/file/2023/8/58698_0s.png)
flutter实现扫码枪获取数据源禁止系统键盘弹窗示例详解
2023-07-23 01:52:41
![](https://img.aspxhome.com/file/2023/0/96760_0s.webp)
理解maven命令package、install、deploy的联系与区别
2022-08-09 05:11:39
![](https://img.aspxhome.com/file/2023/4/116134_0s.png)
java实现砸金蛋抽奖功能
2022-02-22 01:26:50
android6.0权限动态申请框架permissiondispatcher的方法
2023-07-31 10:51:57
Spring-Security实现登录接口流程
2023-03-24 05:35:40
![](https://img.aspxhome.com/file/2023/4/70334_0s.png)
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2022-02-28 02:43:26
![](https://img.aspxhome.com/file/2023/2/85542_0s.jpg)
java寻找迷宫路径的简单实现示例
2021-07-06 13:17:50
Android使用TextView跑马灯效果
2022-05-20 20:07:20
![](https://img.aspxhome.com/file/2023/0/84730_0s.gif)
详解Maven私 服Nexus的安装与使用
2023-11-24 12:34:02
svn 清理失败 (cleanup 失败) 的快速解决方法
2022-10-25 11:22:40
![](https://img.aspxhome.com/file/2023/3/60843_0s.jpg)
Android 系统语言切换监听和设置实例代码
2021-08-06 16:18:25
教你怎么用Idea打包jar包
2023-03-15 03:30:51
![](https://img.aspxhome.com/file/2023/0/68000_0s.png)
Java中如何调用cmd压缩文件
2023-12-09 13:37:02
Java nacos动态配置实现流程详解
2021-06-04 19:18:30
![](https://img.aspxhome.com/file/2023/5/83555_0s.png)
为何Linq的Distinct实在是不给力
2023-05-11 17:03:18
![](https://img.aspxhome.com/file/2023/8/113798_0s.png)
Android ViewPager相册横向移动的实现方法
2023-02-19 07:26:08
![](https://img.aspxhome.com/file/2023/6/139436_0s.jpg)
java9版本特性资源自动关闭的语法增强
2023-10-30 23:35:24
![](https://img.aspxhome.com/file/2023/9/63449_0s.png)
MP(MyBatis-Plus)实现乐观锁更新功能的示例代码
2022-10-27 15:41:24
Android AlertDialog六种创建方式案例详解
2022-08-30 01:55:47
![](https://img.aspxhome.com/file/2023/1/137691_0s.jpg)