业务背景
用户上传一个 excel 文件,要对 excel 内容做校验,然后返回校验结果。校验通过之后,在执行导入。但是现在平台要做国际化,支持中英双语,目前校验结果是直接中文返回,为了完成国际化需求,需要根据请求头中的 Accept-language 来决定返回何种语言的内容。
当前处理方式

国际化方案
使用工具类获取国际化内容
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 
 | import org.springframework.context.MessageSource;import org.springframework.context.MessageSourceAware;
 import org.springframework.context.support.MessageSourceAccessor;
 import org.springframework.stereotype.Component;
 
 @Component
 public class I18nMessageUtil implements MessageSourceAware {
 
 private static MessageSourceAccessor accessor;
 
 
 
 
 
 
 
 
 
 public static String getMessage(String code, Locale locale, Object... args) {
 if (locale == null) {
 return accessor.getMessage(code, args);
 }
 return accessor.getMessage(code, args, locale);
 }
 
 
 
 
 
 
 
 
 public static String getMessage(String code, Object... args) {
 return accessor.getMessage(code, args);
 }
 
 @Override
 public void setMessageSource(MessageSource messageSource) {
 I18nMessageUtil.accessor = new MessageSourceAccessor(messageSource);
 }
 }
 
 | 
配置国际化文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | import org.springframework.context.MessageSource;import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.support.ResourceBundleMessageSource;
 
 import java.util.HashSet;
 import java.util.Set;
 
 
 @Configuration
 public class I18nConfig {
 @Bean
 public MessageSource messageSource() {
 ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
 Set<String> i18nFolder = new HashSet<>();
 
 i18nFolder.add("i18n.excelValidation");
 
 i18nFolder.add("i18n.messages");
 messageSource.setBasenames(i18nFolder.toArray(new String[0]));
 messageSource.setDefaultEncoding("UTF-8");
 messageSource.setUseCodeAsDefaultMessage(true);
 return messageSource;
 }
 }
 
 | 
编写国际化异常信息

替换校验异常信息
