在springboot中实现国际化也是非常方便的。因为想在的项目一般都是前后端分离的,所以这里只写下在RestAPI中进行国际化的操作

1,配置:

spring:# 国际化 messages: # 国际化资源路径 basename: static/i18n/messages #相对路径 开头不要添加斜杠 encoding: UTF-8

这里面比较重要的配置,就是basename,指明我们的国际化资源所在的路径

2,新建国际化资源文件

和basename对应,我在resources下面,新建一个目录static/i18n

然后在下面新建了三个文件

springboot中后端服务的国际化

messages.properties是必须的,内容可以为空,但是必须有这个文件

messages_zh_CN.properties和messages_en_US.properties分别是中文和英文资源

文件名必须是这个格式

# useruser.register.err.username.repeat=用户名重复

3,配置国际化

springboot中后端服务的国际化

4,使用

在restAPi中测试下

Locale locale = LocaleContextHolder.getLocale();

String message = 

messageSource.getMessage("user.register.err.username.repeat", null, locale);

//获取转换后的字符。需要在

messages.properties,messages_enUS.properties,messages.properties 中配置。

5,测试

我们使用请求后面加lang参数,来指定语言

springboot中后端服务的国际化

但是,中文乱码了

springboot中后端服务的国际化

这里要改成UTF-8

springboot中后端服务的国际化

好了

这样我们就将国际化的选择权交给了前端。

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2021-08-10
  • 2021-08-17
  • 2022-12-23
猜你喜欢
  • 2022-02-21
  • 2021-07-30
  • 2021-12-04
  • 2022-12-23
  • 2021-08-27
  • 2021-11-02
  • 2022-12-23
相关资源
相似解决方案