i18n Message Source provides integration with Consul or Nacos
- To use i18n Message Source , you could add the following dependency:
<dependency>
<groupId>com.github.demo</groupId>
<artifactId>i18n-config-spring-boot-starter</artifactId>
<version>x.y.z</version>
</dependency>
- Use Nacos or Consul Config Server. create i18n namespace or folder, add messages source file.
NOTE: Message source config must use properties format and the UTF-8 encoding
- Then Config your project bootstrap or application config file
i18n:
consul:
# format consul folder/basename
basename: i18n/messages
i18n:
nacos:
namespace: i18n
# default basename: messages
# default group: DEFAULT_GROUP
- Configuration your LocaleResolver. example accept-language:
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
return new AcceptHeaderLocaleResolver();
}
}
- Then you can inject MessageSource Instance Or implements MessageSourceAware Interface
public class xxx {
@Autowired
private MessageSource messageSource;
@GetMapping(value = "/test")
public Wrapper test() {
return WrapMapper.ok(messageSource.getMessage("hello.world", new Object[]{"哈哈哈"}, Locale.CHINA));
}
}
public class xxxx implements MessageSourceAware {
private MessageSourceAccessor messageSourceAccessor;
@Override
public void setMessageSource(MessageSource messageSource) {
this.messageSourceAccessor = new MessageSourceAccessor(messageSource);
}
@GetMapping(value = "/test")
public IResponse test() {
return IResponse.success(messageSourceAccessor.getMessage("hello.world", new Object[]{"哈哈哈"}));
}
}