diff --git a/application/src/main/java/run/halo/app/theme/message/ThemeMessageResolver.java b/application/src/main/java/run/halo/app/theme/message/ThemeMessageResolver.java index 673770023..18c9819fc 100644 --- a/application/src/main/java/run/halo/app/theme/message/ThemeMessageResolver.java +++ b/application/src/main/java/run/halo/app/theme/message/ThemeMessageResolver.java @@ -1,7 +1,10 @@ package run.halo.app.theme.message; +import java.util.Collections; +import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.Optional; import org.thymeleaf.messageresolver.StandardMessageResolver; import org.thymeleaf.templateresource.ITemplateResource; import run.halo.app.theme.ThemeContext; @@ -22,7 +25,12 @@ public class ThemeMessageResolver extends StandardMessageResolver { protected Map resolveMessagesForTemplate(String template, ITemplateResource templateResource, Locale locale) { - return ThemeMessageResolutionUtils.resolveMessagesForTemplate(locale, theme); + var properties = new HashMap(); + Optional.ofNullable(ThemeMessageResolutionUtils.resolveMessagesForTemplate(locale, theme)) + .ifPresent(properties::putAll); + Optional.ofNullable(super.resolveMessagesForTemplate(template, templateResource, locale)) + .ifPresent(properties::putAll); + return Collections.unmodifiableMap(properties); } @Override diff --git a/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java b/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java index 924f1edcf..b7966673b 100644 --- a/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java +++ b/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolutionUtilsTest.java @@ -29,16 +29,16 @@ class ThemeMessageResolutionUtilsTest { void resolveMessagesForTemplateForDefault() throws URISyntaxException { Map properties = ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.CHINESE, getTheme()); - assertThat(properties).hasSize(1); - assertThat(properties).containsEntry("index.welcome", "欢迎来到首页"); + assertThat(properties).isEqualTo(Map.of("index.welcome", "欢迎来到首页", + "title", "来自 i18n/zh.properties 的标题")); } @Test void resolveMessagesForTemplateForEnglish() throws URISyntaxException { Map properties = ThemeMessageResolutionUtils.resolveMessagesForTemplate(Locale.ENGLISH, getTheme()); - assertThat(properties).hasSize(1); - assertThat(properties).containsEntry("index.welcome", "Welcome to the index"); + assertThat(properties).isEqualTo(Map.of("index.welcome", "Welcome to the index", + "title", "这是来自 i18n/default.properties 的标题")); } @Test diff --git a/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java b/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java index 6948c4e96..4c461ff6b 100644 --- a/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java +++ b/application/src/test/java/run/halo/app/theme/message/ThemeMessageResolverIntegrationTest.java @@ -93,6 +93,9 @@ public class ThemeMessageResolverIntegrationTest { .expectStatus() .isOk() .expectBody() + // make sure the "templates/index.properties" file is precedence over the + // "i18n/default.properties". + .xpath("/html/head/title").isEqualTo("Title from index.properties") .xpath("/html/body/div[1]").isEqualTo("foo") .xpath("/html/body/div[2]").isEqualTo("欢迎来到首页"); } @@ -105,7 +108,7 @@ public class ThemeMessageResolverIntegrationTest { .expectStatus() .isOk() .expectBody() - .xpath("/html/head/title").isEqualTo("Title") + .xpath("/html/head/title").isEqualTo("来自 index_zh.properties 的标题") .xpath("/html/body/div[1]").isEqualTo("zh") .xpath("/html/body/div[2]").isEqualTo("欢迎来到首页") ; diff --git a/application/src/test/resources/themes/default/i18n/default.properties b/application/src/test/resources/themes/default/i18n/default.properties index 0321c8140..352794999 100644 --- a/application/src/test/resources/themes/default/i18n/default.properties +++ b/application/src/test/resources/themes/default/i18n/default.properties @@ -1 +1,2 @@ -index.welcome=\u6B22\u8FCE\u6765\u5230\u9996\u9875 \ No newline at end of file +index.welcome=欢迎来到首页 +title=这是来自 i18n/default.properties 的标题 diff --git a/application/src/test/resources/themes/default/i18n/zh.properties b/application/src/test/resources/themes/default/i18n/zh.properties new file mode 100644 index 000000000..dbc13ede3 --- /dev/null +++ b/application/src/test/resources/themes/default/i18n/zh.properties @@ -0,0 +1 @@ +title=来自 i18n/zh.properties 的标题 diff --git a/application/src/test/resources/themes/default/templates/index.html b/application/src/test/resources/themes/default/templates/index.html index 7d38411c4..08c0bc430 100644 --- a/application/src/test/resources/themes/default/templates/index.html +++ b/application/src/test/resources/themes/default/templates/index.html @@ -2,7 +2,7 @@ - Title + Title index diff --git a/application/src/test/resources/themes/default/templates/index.properties b/application/src/test/resources/themes/default/templates/index.properties new file mode 100644 index 000000000..44e41acd8 --- /dev/null +++ b/application/src/test/resources/themes/default/templates/index.properties @@ -0,0 +1 @@ +title=Title from index.properties diff --git a/application/src/test/resources/themes/default/templates/index_zh.properties b/application/src/test/resources/themes/default/templates/index_zh.properties new file mode 100644 index 000000000..300ca1cfc --- /dev/null +++ b/application/src/test/resources/themes/default/templates/index_zh.properties @@ -0,0 +1 @@ +title=来自 index_zh.properties 的标题