Fix the problem of resolving locale context by language parameter (#6647)

#### What type of PR is this?

/kind bug
/area theme
/milestone 2.20.x

#### What this PR does / why we need it:

If we pass a query `language` while requesting index page, we will get the wrong header `Content-Language`. Please see the result below:

```bash
http https://www.halo.run/\?language\=zh-CN -p h

HTTP/1.1 200 OK
Content-Language: und
...
```

After fixing, we will get the right header `Content-Language`.

```bash
http http://localhost:8090/\?language\=zh-CN -p h

HTTP/1.1 200 OK
Content-Language: zh-CN
...
```

#### Does this PR introduce a user-facing change?

```release-note
修复主题端区域和语言解析错误的问题
```
pull/6648/head
John Niang 2024-09-12 17:12:22 +08:00 committed by GitHub
parent 697963d628
commit 6a5e9c4932
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 1 deletions

View File

@ -51,7 +51,7 @@ public class ThemeLocaleContextResolver extends AcceptHeaderLocaleContextResolve
Locale locale; Locale locale;
if (StringUtils.isNotBlank(language)) { if (StringUtils.isNotBlank(language)) {
locale = new Locale(language); locale = Locale.forLanguageTag(language);
} else if (exchange.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) != null) { } else if (exchange.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME) != null) {
locale = exchange.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME); locale = exchange.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
} else { } else {

View File

@ -62,6 +62,10 @@ class ThemeLocaleContextResolverTest {
.isEqualTo(ENGLISH); .isEqualTo(ENGLISH);
assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh")).getLocale()) assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh")).getLocale())
.isEqualTo(CHINESE); .isEqualTo(CHINESE);
assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh-CN")).getLocale())
.isEqualTo(CHINA);
assertThat(this.resolver.resolveLocaleContext(exchangeForParam("zh-cn")).getLocale())
.isEqualTo(CHINA);
} }
@Test @Test