From 38b429a7741e0184e6cc2d0eda7219b60a1ba713 Mon Sep 17 00:00:00 2001 From: JohnNiang Date: Thu, 12 Sep 2024 16:52:06 +0800 Subject: [PATCH] Fix the problem of resolving locale context by language parameter Signed-off-by: JohnNiang --- .../java/run/halo/app/theme/ThemeLocaleContextResolver.java | 2 +- .../run/halo/app/theme/ThemeLocaleContextResolverTest.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/run/halo/app/theme/ThemeLocaleContextResolver.java b/application/src/main/java/run/halo/app/theme/ThemeLocaleContextResolver.java index cf82601a5f..65d7e2289c 100644 --- a/application/src/main/java/run/halo/app/theme/ThemeLocaleContextResolver.java +++ b/application/src/main/java/run/halo/app/theme/ThemeLocaleContextResolver.java @@ -51,7 +51,7 @@ private Locale getLocale(ServerWebExchange exchange) { Locale locale; if (StringUtils.isNotBlank(language)) { - locale = new Locale(language); + locale = Locale.forLanguageTag(language); } else if (exchange.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) != null) { locale = exchange.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME); } else { diff --git a/application/src/test/java/run/halo/app/theme/ThemeLocaleContextResolverTest.java b/application/src/test/java/run/halo/app/theme/ThemeLocaleContextResolverTest.java index 8ab62e89e2..8662382e49 100644 --- a/application/src/test/java/run/halo/app/theme/ThemeLocaleContextResolverTest.java +++ b/application/src/test/java/run/halo/app/theme/ThemeLocaleContextResolverTest.java @@ -62,6 +62,10 @@ public void resolveFromParam() { .isEqualTo(ENGLISH); assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh")).getLocale()) .isEqualTo(CHINESE); + assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh-CN")).getLocale()) + .isEqualTo(CHINA); + assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh-cn")).getLocale()) + .isEqualTo(CHINA); } @Test