Spring 国际化@Autowired MessageSource 配置文件读取顺序

打印 上一主题 下一主题

主题 455|帖子 455|积分 1365

代码逻辑示例
  1. {
  2.         // 遍历循环配置文件basename集合.
  3.         for (String baseName : Arrays.asList("spring.messages.basename")){
  4.             /**
  5.              * 根据 locale 遍历父级的locale对象,
  6.              * {@link ResourceBundle.Control.CandidateListCache#createObject(sun.util.locale.BaseLocale)}
  7.              * {@link ResourceBundle.Control.CandidateListCache#getDefaultList(java.lang.String, java.lang.String, java.lang.String, java.lang.String)}
  8.              * 当父级遍历完毕, 设置当前locale对象为系统默认locale对象.
  9.              * {@link ResourceBundleMessageSource#resolveCodeWithoutArguments(java.lang.String, java.util.Locale)}
  10.               */
  11.             for (String locale : Arrays.asList("locale", "parentLocale", "defaultLocale", "defaultParentLocale")) {
  12.                 // 如果当前keySet集合存在该元素则返回对应数据并结束遍历.
  13.                 if (ResourceBundle.getBundle(baseName, Locale.forLanguageTag(locale)).keySet().contains("目标读取key")) {
  14.                     return ResourceBundle.getBundle(baseName, Locale.forLanguageTag(locale)).getString("目标读取key");
  15.                 }
  16.             }
  17.         }
  18.         return "默认值";
  19.     }
复制代码
效果展示
  1. spring.messages.basename=i18n/messages,other,messages
  2. 当前配置文件:
  3. resources/i18n/messages.properties
  4. resources/i18n/messages_en.properties
  5. resources/i18n/messages_ja_JP.properties
  6. resources/i18n/messages_zh.properties
  7. resources/i18n/messages_zh_CN.properties
  8. resources/messages.properties
  9. resources/messages_en.properties
  10. resources/messages_ja_JP.properties
  11. resources/messages_zh.properties
  12. resources/messages_zh_CN.properties
  13. resources/other.properties
  14. resources/other_en.properties
  15. resources/other_ja_JP.properties
  16. resources/other_zh.properties
  17. resources/other_zh_CN.properties
  18. en-US读取配置顺序:i18n/messages_en_US.properties
  19. en-US读取配置顺序:i18n/messages_en.properties
  20. en-US读取配置顺序:i18n/messages_zh_CN.properties
  21. en-US读取配置顺序:i18n/messages_zh.properties
  22. en-US读取配置顺序:i18n/messages.properties
  23. en-US读取配置顺序:other_en_US.properties
  24. en-US读取配置顺序:other_en.properties
  25. en-US读取配置顺序:other_zh_CN.properties
  26. en-US读取配置顺序:other_zh.properties
  27. en-US读取配置顺序:other.properties
  28. en-US读取配置顺序:messages_en_US.properties
  29. en-US读取配置顺序:messages_en.properties
  30. en-US读取配置顺序:messages_zh_CN.properties
  31. en-US读取配置顺序:messages_zh.properties
  32. en-US读取配置顺序:messages.properties
  33. ja读取配置顺序:i18n/messages_ja.properties
  34. ja读取配置顺序:i18n/messages_zh_CN.properties
  35. ja读取配置顺序:i18n/messages_zh.properties
  36. ja读取配置顺序:i18n/messages.properties
  37. ja读取配置顺序:other_ja.properties
  38. ja读取配置顺序:other_zh_CN.properties
  39. ja读取配置顺序:other_zh.properties
  40. ja读取配置顺序:other.properties
  41. ja读取配置顺序:messages_ja.properties
  42. ja读取配置顺序:messages_zh_CN.properties
  43. ja读取配置顺序:messages_zh.properties
  44. ja读取配置顺序:messages.properties```
复制代码
源码示例

遍历basename源代码: org.springframework.context.support.ResourceBundleMessageSource#resolveCodeWithoutArguments
当前baseName获取不到对应的bundle 则根据默认的locale进行取值.
  1. 填充locale的父级locale源代码: java.util.ResourceBundle.Control.CandidateListCache#getDefaultList
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连密封材料

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表