提供了默认的国际化解析XyLocaleResolver,

如果后台服务需要国际化功能,在请求header中需要添加Accept-Language的头信息,当前只提供中文和英文两种语言:

Accept-Language=en 英文 Accept-Language=zh 中文

后续若需要其他,参照java.util.Local中的语言修改。

  1. @Bean
  2. @ConditionalOnProperty(value = "xy.core.i18n.enable", havingValue = "true", matchIfMissing = true)
  3. public LocaleResolver localeResolver() {
  4. log.info("loading LocaleResolver............");
  5. return new XyLocaleResolver();
  6. }
  1. #是否开启配置
  2. xy.core.i18n.enable=true

语言国际化 - 图2

  1. public class MessageUtil implements BeanFactoryAware {
  2. private static MessageSource messageSource;
  3. public synchronized static String getMessage(String key) {
  4. Locale locale = LocaleContextHolder.getLocale();
  5. return messageSource.getMessage(key, null, locale);
  6. }
  7. @Override
  8. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  9. messageSource = beanFactory.getBean(MessageSource.class);
  10. }
  11. }