Internationalization
Overview
Beaver IoT supports multiple languages by default and provides a complete internationalization solution.
Locale
Locale is used as the current language environment. The Locale
of the current thread is stored in the LocaleContext
.
Message Files
Storage directory: src/main/resources/i18n
Message file formats are messages.properties
and messages_{languageCode}[_{regionCode}].properties
, for example:
messages.properties: The fallback message file. This file is read when no corresponding language file is found for the current locale.
messages_en.properties: Read when the locale is ENGLISH
. The default locale is ENGLISH
.
messages_zh_CN.properties: Read when the locale is SIMPLIFIED_CHINESE
.
The content format of message files is {messageCode}={message}
, for example:
messages_en.properties
example-module.hello.world=Hello World
messages_zh_CN.properties
example-module.hello.world=你好,世界
Message Code Convention
To ensure messages between modules do not interfere with each other, message code naming is standardized. A module name must be added as a prefix to the message code. The message code format is: {moduleName}.{detailCode}
For example, if the module name is device-template-service
, the message file content would be:
device-template-service.code1=xxx
device-template-service.code2=yyy
device-template-service.code3=zzz
For use within integrations, the message code format must follow: integrations.{integrationId}.{detailCode}
For example, if the integration name is mqtt-device
, the message file content would be:
integrations.mqtt-device.code1=xxx
integrations.mqtt-device.code2=yyy
integrations.mqtt-device.code3=zzz
Message codes will be strictly reviewed as one of the development standards. Please follow the message code conventions during development.
Usage
Using the integration name example-integration
as an example.
Create Message Files
-
Create an
i18n
directory undersrc/main/resources
. -
In the message directory, create the fallback message file and language-specific files as shown below: messages.properties
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}
Import Message Source
Internationalized messages are read by injecting the message source MergedResourceBundleMessageSource
.
Constructor Injection
@Slf4j
@Service
public class ExampleService {
private final MergedResourceBundleMessageSource messageSource;
public ExampleService(MergedResourceBundleMessageSource messageSource) {
this.messageSource = messageSource;
}
}
@Autowired Annotation Injection
@Slf4j
@Service
public class ExampleService {
@Autowired
private MergedResourceBundleMessageSource messageSource;
}
Message Retrieval
After injecting the message source, you can retrieve messages corresponding to the locale using the provided methods:
// Get message by code, arguments, default message, and Locale
public String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
// Get message by code, arguments, and Locale
public String getMessage(String code, Object[] args, Locale locale);
// Get message by code, arguments, and default message, using the current thread's Locale
public String getMessage(String code, Object[] args, String defaultMessage);
// Get message by code and arguments, using the current thread's Locale
public String getMessage(String code, Object[] args);
// Get message by code, using the current thread's Locale
public String getMessage(String code);
Example 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));
}
}