SpringBoot消息国际化配置实现过程解析
作者:与李 发布时间:2023-05-16 01:19:22
标签:Spring,Boot,消息,国际化,配置
一、目的
针对不同地区,设置不同的语言信息。
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


猜你喜欢
- 1.利用 “+”(加号)运算符:string str = “Hello”+ “World”; console.WriteLine(str);
- Synchronized的用法在多线程并发问题中,常用Synchronized锁解决问题。Synchronized锁通常用于同步示例方法,同
- 解决@NotBlank不生效在项目开发中,发现一个类中包含有另外一个类,这种包含关系的类上的@NotBlank校验不生效,后来发现需要在内部
- 1、效果2、简介本文主角是ItemTouchHelper。它是RecyclerView对于item交互处理的一个「辅助类」,主要用于拖拽以及
- 一、背景即使我电脑安装的JDK版本是8,然而在idea运行中常常提示xxjdk1.5已过时之类的,why?明明是我装的JDK8啊二、解决鼠标
- 之前我们学习了如何使用Jpa访问关系型数据库。通过Jpa大大简化了我们对数据库的开发工作。但是,之前的例子中我们只提到了最简单的CRUD(增
- 前言在项目开发过程中,时常会碰到这种情况:1.同一个Project的同一个API,有几个不同的接口,比如内部测试用的Server,和当前版本
- 本文实例为大家分享了Android蒙版弹出框效果的具体代码,供大家参考,具体内容如下自定义package cn.lxsdb.yyd.app.
- 首先:因为工作需要,需要对接socket.io框架对接,所以目前只能使用netty-socketio。websocket是不支持对接sock
- 我贴c#的代码: namespace IWebs.Webs{ using System; using System.Web.Services
- java 中HttpClient传输xml字符串实例详解介绍:我现在有一个对象page,需要将page对象转换为xml格式并以binary方
- 这篇文章主要介绍了JAVA泛型的继承和实现、擦除原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 本文实例为大家分享了java实现文件归档和还原的具体代码,供大家参考,具体内容如下基本思路: 文件归档,换句话就是把多个文件的字节
- 菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(Co
- Spring 基于注解启动主要有两个Class实现注解启动AnnotationConfigApplicationContextAnnotat
- Java语言是简单的:Java语言的语法与C语言和C++语言很接近,使得大多数程序员很容易学习和使用。另一方面,Java丢弃了C++中很少使
- 对于数组来说,我们想要对其中的一个元素进行引用,那就离不开new的使用。大家在学习new的时候,一般是以新建和初始化的身份出现的。如果是用在
- 本文实例讲述了Java编程实现向文本文件中读取数据之Scanner用法。分享给大家供大家参考,具体如下:使用Scanner类来读取文件我们使
- feign.codec.DecodeException异常在微服务项目使用Feign进行远程服务调用时出现该异常:feign.codec.D
- 紧接上文所述,在这篇文章中我将对Mapper映射文件进行详细的说明。Mapper映射文件是一个xml格式文件,必须遵循相应的dtd文件规范,