diff --git a/ballcat-common/ballcat-common-i18n/src/main/java/com/hccake/ballcat/common/i18n/WildcardReloadableResourceBundleMessageSource.java b/ballcat-common/ballcat-common-i18n/src/main/java/com/hccake/ballcat/common/i18n/WildcardReloadableResourceBundleMessageSource.java index 2cbd8d76..45dab6fc 100644 --- a/ballcat-common/ballcat-common-i18n/src/main/java/com/hccake/ballcat/common/i18n/WildcardReloadableResourceBundleMessageSource.java +++ b/ballcat-common/ballcat-common-i18n/src/main/java/com/hccake/ballcat/common/i18n/WildcardReloadableResourceBundleMessageSource.java @@ -26,24 +26,48 @@ public class WildcardReloadableResourceBundleMessageSource extends ReloadableRes private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + /** + * Calculate all filenames for the given bundle basename and Locale. Will calculate + * filenames for the given Locale, the system Locale (if applicable), and the default + * file. + * @param basename the basename of the bundle + * @param locale the locale + * @return the List of filenames to check + * @see #setFallbackToSystemLocale + * @see #calculateFilenamesForLocale + */ + @Override + protected List calculateAllFilenames(String basename, Locale locale) { + // 父类默认的方法会将 basename 也放入 filenames 列表 + List filenames = super.calculateAllFilenames(basename, locale); + // 当 basename 有匹配符时,从 filenames 中移除,否则扫描文件将抛出 Illegal char <*> 的异常 + if (basename.contains("*")) { + filenames.remove(basename); + } + return filenames; + } + @Override protected List calculateFilenamesForLocale(String basename, Locale locale) { + // 支持 basename 用 . 表示文件层级 basename = basename.replace(".", "/"); - List filenames = super.calculateFilenamesForLocale(basename, locale); - List add = new ArrayList<>(); - for (String filename : filenames) { + + // 资源文件名 + List fileNames = new ArrayList<>(); + // 获取到待匹配的国际化信息文件名集合 + List matchFilenames = super.calculateFilenamesForLocale(basename, locale); + for (String matchFilename : matchFilenames) { try { - Resource[] resources = resolver.getResources(filename + PROPERTIES_SUFFIX); + Resource[] resources = resolver.getResources("classpath*:" + matchFilename + PROPERTIES_SUFFIX); for (Resource resource : resources) { String sourcePath = resource.getURI().toString().replace(PROPERTIES_SUFFIX, ""); - add.add(sourcePath); + fileNames.add(sourcePath); } } catch (IOException ignored) { } } - filenames.addAll(add); - return filenames; + return fileNames; } } diff --git a/ballcat-starters/ballcat-spring-boot-starter-i18n/src/main/java/com/hccake/ballcat/autoconfigure/i18n/CustomMessageSourceAutoConfiguration.java b/ballcat-starters/ballcat-spring-boot-starter-i18n/src/main/java/com/hccake/ballcat/autoconfigure/i18n/CustomMessageSourceAutoConfiguration.java index 8390b971..cef501ac 100644 --- a/ballcat-starters/ballcat-spring-boot-starter-i18n/src/main/java/com/hccake/ballcat/autoconfigure/i18n/CustomMessageSourceAutoConfiguration.java +++ b/ballcat-starters/ballcat-spring-boot-starter-i18n/src/main/java/com/hccake/ballcat/autoconfigure/i18n/CustomMessageSourceAutoConfiguration.java @@ -103,14 +103,8 @@ public class CustomMessageSourceAutoConfiguration { private Resource[] getResources(ClassLoader classLoader, String name) { String target = name.replace('.', '/'); try { - String locationPattern; - if (target.startsWith("classpath")) { - locationPattern = target + ".properties"; - } - else { - locationPattern = "classpath*:" + target + ".properties"; - } - return new PathMatchingResourcePatternResolver(classLoader).getResources(locationPattern); + return new PathMatchingResourcePatternResolver(classLoader) + .getResources("classpath*:" + target + ".properties"); } catch (Exception ex) { return NO_RESOURCES;