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
0
投稿
猜你喜欢
- 1.<constant name="struts.i18n.encoding" value="UTF-8
- 这篇文章主要介绍了SpringBoot跨域Access-Control-Allow-Origin实现解析,文中通过示例代码介绍的非常详细,对
- 利用Java语言中的集合、Swing、线程等知识点编写一个坦克大战游戏。(1) 画出敌我坦克的原理:在坦克类里面有一个布尔类型变量good。
- SpringBoot web项目启动后立即关闭我们在写spring boot web项目时,有时会遇到启动后立即关闭的情况,或者是无法加载某
- Mybatis typeAlias标签在实际的工程之中,类的全限定名称很长,当我们需要大量使用的时候,这非常不方便的,然而mybatis提供
- 定义与结构 备忘录(Memento)模式又称标记(Token)模式。GOF给备忘录模式的定义为:在不破坏
- 线程安全当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要任何额外的同步或协调,这个类
- @EqualsAndHashCode注解@EqualsAndHashCode注解的作用就是自动实现model类的equals方法和hashc
- 为什么要使用路由在之前我们的代码中,页面跳转使用的代码如下所示:Navigator.of(context).push( Mate
- 我们知道java程序是运行在JVM中的,而JVM就是构建在内存上的虚拟机,那么内存模型JMM是做什么用的呢?我们考虑一个简单的赋值问题:in
- 本文实例为大家分享了java实现简单石头剪刀布游戏的具体代码,供大家参考,具体内容如下问题描述Alice, Bob和Cindy一起玩猜拳的游
- 1 需求描述我们现在要干一个什么事情呢,我们要在浏览器输入一个请求地址,然后我们的后端就给我返回一个User对象即可,并且我希望以Json的
- 数组:数组可以用来保存多个基本数据类型的数据,也可以用来保存多个对象。数组的长度是不可改变的,一旦初始化数组时就指定了数组的长度(无论是静态
- 项目结构项目路径可以自己定义,只要路径映射正确就可以pom.xml <properties> <spring.versio
- 通过子类调用父类的变量,有两种方法:1、把父类的变量设置成public:package triangle.opengl.wlz.stu.ch
- 使用Java的方式配置Spring我们现在要完全不使用Spring的xml配置,全权使用Java来配置Spring!JavaConfig是S
- Java获取环境变量Java 获取环境变量的方式很简单: System.getEnv() 得到所有的环境变量Syste
- 1. List1.1 List 的常见方法方法描述boolean add(E e)尾插 evoid add(int index, E ele
- 本文实例讲述了java版微信公众平台消息接口应用方法。分享给大家供大家参考,具体如下:微信公众平台现在推出自动回复消息接口,但是由于是接口内
- 之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,