1、國際化基本原理
- 第一步,獲取語言標識 - 通過前端調用傳入的參數
- 第二步,獲取資源文件 - 通過語言標識獲取對應資源文件
- 第三步,獲取錯誤描述 - 從資源文件中根據錯誤編碼獲取錯誤描述,同時可支持參數替換描述中的占位符
- 第四步,何處攔截處理 - 在何處執行錯誤碼翻譯工作
2、SpringBoot實現國際化原理
在SpringBoot啟動類中,@SpringBootApplication注解會引用@EnableAutoConfiguration注解。
- 第一步,獲取語言標識,如下為幾種實現方式:
- AcceptHeaderLocaleResolver:@EnableAutoConfiguration注解會自動加載配置WebMvcAutoConfiguration,該類中會直接配置AcceptHeaderLocaleResolver,
@Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "spring.mvc",name = {"locale"} ) public LocaleResolver localeResolver() { if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.WebMvcProperties.LocaleResolver.FIXED) { return new FixedLocaleResolver(this.mvcProperties.getLocale()); } else {
// 基於瀏覽器,從HttpServletRequest的Header中屬性Accept-Language中獲取語言標識 AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver(); localeResolver.setDefaultLocale(this.mvcProperties.getLocale()); return localeResolver; } }
-
- CookieLocaleResolver:基於Cookie,從cookie中獲取語言標識
- SessionLocaleResolver:基於Session,從session中獲取語言標識
-
- FixedLocaleResolver:返回固定的語言標識
- 第二步,獲取資源文件,如下為幾種方式:
- Yml配置:@EnableAutoConfiguration注解會自動加載配置MessageSourceAutoConfiguration,該類中會直接配置MessageSourceProperties,默認讀取spring.message
Spring: messages: ## 資源文件路徑 basename: i18n.messages ## 編碼格式 encoding: UTF-8 ## 緩存有效期(單位:秒),-1:永久 cacheSeconds: -1 ## 當找不到當前語言的資源文件時,若為true,則默認找當前系統的語言對應的資源文件如messages_zh_CN.properties,如果為false即加載系統默認的如messages.properties文件。 fallbackToSystemLocale: true
- Yml配置:@EnableAutoConfiguration注解會自動加載配置MessageSourceAutoConfiguration,該類中會直接配置MessageSourceProperties,默認讀取spring.message
- 自定義配置:在Configuration中自行配置方式,
@Configuration public class MessageSourceConfig { @Bean(name = "messageSource") public ResourceBundleMessageSource getMessageSource() throws Exception { ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource(); resourceBundleMessageSource.setDefaultEncoding("UTF-8");
// 指定了i18n文件的位置,這是個在i18n文件夾下的Resources Bundle文件集合,新建的時候在IDEA里面選擇的是Resource Bundle類型,名字寫的是messages。這個很重要,對應配置文件! resourceBundleMessageSource.setBasenames("i18n.messages"); return resourceBundleMessageSource; } }
如下為國際化資源文件:
【默認】messages.properties
【英文】messages_en_US.properties
【中文】messages_zh_CN.properties
- 第三步,獲取錯誤描述,如下為實現方式:
代碼當中使用MessageSource注入的方式:
@Autowired private MessageSource messageSource;
// 通過如下方式可以獲取Locale
Locale locale = LocaleContextHolder.getLocale(); // 然后使用如下方式獲取信息,同時args可以支持占位符替換 messageSource.getMessage(code, args, defaultMessage, locale); -
第四步,何處攔截處理,如下為實現方式:
- 實現接口HandlerExceptionResolver,比如在發生異常時返回統一ModelAndView
public class GenericResponseHandler implements HandlerExceptionResolver{ public ModelAndView resolveException(HttpServletRequest request,HttpServletReponse response,Object handler,Exception ex){ // 處理異常,並返回統一ModelAndView,同時,應用中Exception可以包一層,比如AppRtException,方便通過錯誤碼獲取錯誤描述
return new ModelAndView(....);
}
}
-
- ControllerAdvice下的ExceptionHandler
@ControllerAdvice public class GenericExceptionHandler { @Autowired private MessageSource messageSource;
/** * 全局處理業務的異常 * * @param ex * @return */ @ExceptionHandler(Exception.class) public ResponseEntity handleGenericException(Exception ex) {
// 同時,應用中Exception可以包一層,比如AppRtException,方便通過錯誤碼獲取錯誤描述
return ......
}
}
- ControllerAdvice下的ExceptionHandler
3、github代碼鏈接地址:......