SpringBoot下国际化配置

简介: SpingBoot实现国际化配置步骤

SpringBoot下使用MessageSource实现国际化配置

1.1在ResourceBundle下创建国际化配置文件

(1)在【resources】目录下创建【i18n】目录(名称随意起),在目录上【右键】-->【new】-->【Resource Bundle】-->【起名message】(起名随意)。

image.png(2)在message下创建国际化配置文件

messages.properties (默认的语言配置文件,当找不到其他语言的配置的时候,使用该文件进行展示)。

i18n.user.name=孙大圣

messages_zh_CN.properties(中文)

i18n.user.name=孙行者

messages_en_US.properties(英文)

i18n.user.name=SevenSun

1.2 yml配置文件中配置刚才创建的国际化配置文件

spring:
  messages:
    # 国际化资源文件路径 eg:"messages,config.i18n.messages"
    basename: i18n/messages
    encoding: UTF-8

image.png

*basename: i18n/messages //这里即为配置文件路径,如果创建时候改名了,这里记得要一致!!

1.3 配置拦截器

packagecom.tab343.myspringboot.internationalization;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.LocaleResolver;
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;
importjava.util.Locale;
@ConfigurationpublicclassI18nConfigimplementsWebMvcConfigurer{
//两种方式区一中即可@BeanpublicLocaleResolverlocaleResolver()
    {
//(1)Cookie方式/* CookieLocaleResolver localeResolver = new CookieLocaleResolver();localeResolver.setCookieName("localeCookie");//设置默认区域localeResolver.setDefaultLocale(Locale.ENGLISH);localeResolver.setCookieMaxAge(3600);//设置cookie有效期.return localeResolver;*///(2)Session方式SessionLocaleResolverslr=newSessionLocaleResolver();
// 默认语言slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
returnslr;
    }
@BeanpublicLocaleChangeInterceptorlocaleChangeInterceptor()
    {
LocaleChangeInterceptorlci=newLocaleChangeInterceptor();
// 参数名实现国际化效果lci.setParamName("lang");
returnlci;
    }
@OverridepublicvoidaddInterceptors(InterceptorRegistryregistry)
    {
registry.addInterceptor(localeChangeInterceptor());
    }
}

1.4 编写国际化测试Controller

packagecom.tab343.myspringboot.internationalization;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.MessageSource;
importorg.springframework.context.i18n.LocaleContextHolder;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importjava.util.Locale;
/*** @Description : 国际化Controller*/@RestController@RequestMapping("/i18n")
publicclassI18nController {
@AutowiredprivateMessageSourcemessageSource;
@GetMapping("/hello")
publicStringhello(){
Localelocale=LocaleContextHolder.getLocale();
returnmessageSource.getMessage("i18n.user.name", null, locale);
    }
}

这里通过MessageSource获取配置文件中key为"i18n.user.name"的值

1.5使用PostMan访问

(1)英文

image.png

(2)中文

image.png

目录
相关文章
|
1月前
|
Java 调度 Spring
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
286 0
|
2月前
|
SQL Java 数据库连接
springboot中配置mybatis别名该怎么写?
springboot中配置mybatis别名该怎么写?
46 0
|
2月前
|
druid Java 数据库
druid+springboot加解密Druid链接池配置加密密码链接数据库
druid+springboot加解密Druid链接池配置加密密码链接数据库
81 0
|
1月前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
3天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
10 1
|
6天前
|
Java 文件存储 Spring
【springboot】logback配置
【springboot】logback配置
17 1
|
11天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
21 2
|
14天前
|
Web App开发 前端开发 Java
SpringBoot配置HTTPS及开发调试
在实际开发过程中,如果后端需要启用https访问,通常项目启动后配置nginx代理再配置https,前端调用时高版本的chrome还会因为证书未信任导致调用失败,通过摸索整理一套开发调试下的https方案,特此分享
20 0
SpringBoot配置HTTPS及开发调试
|
16天前
|
存储 Java 数据库
SpringBoot使用jasypt实现数据库配置加密
这样,你就成功地使用Jasypt实现了Spring Boot中的数据库配置加密,确保敏感信息在配置文件中以加密形式存储,并在应用启动时自动解密。
46 2
|
1月前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
40 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析