没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot消息国际化配置实现过程解析
7 下载量 84 浏览量
2020-08-18
17:08:43
上传
评论
收藏 102KB PDF 举报
温馨提示


试读
4页
主要介绍了SpringBoot消息国际化配置实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
资源推荐
资源详情
资源评论










SpringBoot消息国际化配置实现过程解析消息国际化配置实现过程解析
主要介绍了SpringBoot消息国际化配置实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有
一定的参考学习价值,需要的朋友可以参考下
一、目的一、目的
针对不同地区,设置不同的语言信息。
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;
}
资源评论


weixin_38631599
- 粉丝: 9
- 资源: 945
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
