跳到主要内容

国际化

概述

Beaver IoT 默认支持多语言,并且提供了一套完整的国际化解决方案。

语言环境

采用Locale作为当前语言环境,当前线程的Locale保存在LocaleContext上下文中。

消息文件

存储目录:src/main/resources/i18n

消息文件格式为messages.propertiesmessages_{languageCode}[_{regionCode}].properties,如:

信息

messages.properties:兜底消息文件,根据当前语言环境找不到对应的语言文件时,读取该文件

messages_en.properties:语言环境为ENGLISH时读取该文件,默认语言环境为ENGLISH

messages_zh_CN.properties:语言环境为SIMPLIFIED_CHINESE时读取该文件

消息文件内容格式为:{messageCode}={message},如:

messages_en.properties

example-module.hello.world=Hello World

messages_zh_CN.properties

example-module.hello.world=你好,世界

消息代码规范

为了保证各模块之间的消息互不干扰,规范消息代码的命名,规定统一在消息代码的最前面加上模块名称作为前缀,消息代码的格式:{moduleName}.{detailCode}

例如,模块名称为device-template-service,则消息文件内容:

device-template-service.code1=xxx
device-template-service.code2=yyy
device-template-service.code3=zzz

集成内使用,须遵循消息代码的格式:integrations.{integrationId}.{detailCode}

例如,如集成名称为mqtt-device,则消息文件内容:

integrations.mqtt-device.code1=xxx
integrations.mqtt-device.code2=yyy
integrations.mqtt-device.code3=zzz
注意

消息代码将作为开发规范之一进行严格审查,请遵循消息代码规范进行开发

使用

以集成名称example-integration为例

创建消息文件

  1. src/main/resources目录下创建i18n目录

  2. 在消息目录下创建如下的兜底消息文件和各国语言文件

    messages.properties

    integrations.example-integration.hello.world=Hello world
    integrations.example-integration.hello.message=Hello {0}, welcome to {1}

    messages_en.properties

    integrations.example-integration.hello.world=Hello world
    integrations.example-integration.hello.message=Hello {0}, welcome to {1}

    messages_zh_CN.properties

    integrations.example-integration.hello.world=你好,世界
    integrations.example-integration.hello.message=你好 {0},欢迎来到 {1}

引入消息源

通过注入消息源MergedResourceBundleMessageSource实现国际化消息的读取

构造函数注入

@Slf4j
@Service
public class ExampleService {
private final MergedResourceBundleMessageSource messageSource;

public ExampleService(MergedResourceBundleMessageSource messageSource) {
this.messageSource = messageSource;
}
}

@Autowired 注解注入

@Slf4j
@Service
public class ExampleService {
@Autowired
private MergedResourceBundleMessageSource messageSource;
}

消息获取

引入消息解析器后,使用消息源的方法就可以获取语言环境对应的消息,消息源的方法如下:

// 根据 code,参数,默认消息和 Locale 获取消息
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
// 根据 code,参数和 Locale 获取消息
public String getMessage(String code, Object[] args, Locale locale);
// 根据 code,参数和默认消息,以当前线程的 Locale 获取消息
public String getMessage(String code, Object[] args, String defaultMessage);
// 根据 code 和参数,以当前线程的 Locale 获取消息
public String getMessage(String code, Object[] args);
// 根据 code,以当前线程的 Locale 获取消息
public String getMessage(String code);

示例代码

@Slf4j
@Service
public class ExampleService {
private final MergedResourceBundleMessageSource messageSource;

public ExampleService(MergedResourceBundleMessageSource messageSource) {
this.messageSource = messageSource;
}

public void testSimpleMessage() {
log.info(messageSource.getMessage("hello.world"));
}

public void testMessageWithArgs() {
log.info(messageSource.getMessage("hello.message", new Object[]{"Panda", "Milesight"}));
}

public void testChineseMessageWithArgs() {
log.info(messageSource.getMessage("hello.message", new Object[]{"Panda", "Milesight"},
Locale.SIMPLIFIED_CHINESE));
}
}