From f4e57b58c38a3041a425f8ac97577a129402a64d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=91=A8?= Date: Sun, 23 Feb 2025 04:22:46 +0000 Subject: [PATCH] =?UTF-8?q?update=20I18nLocaleResolver.java.=20content-lan?= =?UTF-8?q?guage=20=E6=98=AF=E4=B8=80=E4=B8=AA=20HTTP=20=E5=A4=B4=E9=83=A8?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=8C=87=E5=AE=9A=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E4=BD=93=E7=9A=84=E8=87=AA=E7=84=B6=E8=AF=AD=E8=A8=80=E3=80=82?= =?UTF-8?q?=E5=AE=83=E7=9A=84=E5=80=BC=E9=80=9A=E5=B8=B8=E6=98=AF=E7=94=A8?= =?UTF-8?q?=E9=80=97=E5=8F=B7=E5=88=86=E9=9A=94=E7=9A=84=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E5=88=97=E8=A1=A8=EF=BC=88=E4=BE=8B=E5=A6=82?= =?UTF-8?q?=EF=BC=9Azh-CN,en;q=3D0.9=EF=BC=89=E3=80=82=E7=84=B6=E8=80=8C?= =?UTF-8?q?=EF=BC=8CLocale=20=E7=9A=84=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=9A=84=E6=98=AF=E8=AF=AD=E8=A8=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=92=8C=E5=9B=BD=E5=AE=B6=E4=BB=A3=E7=A0=81=EF=BC=88?= =?UTF-8?q?=E7=94=A8=E4=B8=8B=E5=88=92=E7=BA=BF=20=5F=20=E5=88=86=E9=9A=94?= =?UTF-8?q?=EF=BC=89=E3=80=82=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=20-=20?= =?UTF-8?q?=E5=88=86=E9=9A=94=E7=AC=A6=E5=B9=B6=E6=9E=84=E9=80=A0=20Locale?= =?UTF-8?q?=20=E6=98=AF=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 老周 --- .../common/web/core/I18nLocaleResolver.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/im-admin/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/core/I18nLocaleResolver.java b/im-admin/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/core/I18nLocaleResolver.java index 98ddd06..c877cd3 100644 --- a/im-admin/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/core/I18nLocaleResolver.java +++ b/im-admin/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/core/I18nLocaleResolver.java @@ -15,12 +15,35 @@ public class I18nLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest httpServletRequest) { - String language = httpServletRequest.getHeader("content-language"); - Locale locale = Locale.getDefault(); - if (language != null && language.length() > 0) { - String[] split = language.split("_"); - locale = new Locale(split[0], split[1]); + String languageHeader = httpServletRequest.getHeader("content-language"); + Locale locale; + + if (languageHeader != null && !languageHeader.isEmpty()) { + // 处理可能的逗号分隔的语言列表 + String[] languages = languageHeader.split(","); + String preferredLanguage = languages[0]; // 取第一个语言标签作为首选 + + // 处理语言标签的格式(可能包含语言、国家、区域) + String[] parts = preferredLanguage.split("-"); + switch (parts.length) { + case 1: // 只有语言代码 + locale = new Locale(parts[0]); + break; + case 2: // 语言代码和国家代码 + locale = new Locale(parts[0], parts[1]); + break; + case 3: // 语言代码、国家代码和区域代码 + locale = new Locale(parts[0], parts[1], parts[2]); + break; + default: // 默认回退到默认语言环境 + locale = Locale.getDefault(); + break; + } + } else { + // 如果没有 content-language 头部,使用默认语言环境 + locale = Locale.getDefault(); } + return locale; } @@ -28,4 +51,4 @@ public class I18nLocaleResolver implements LocaleResolver { public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } -} +} \ No newline at end of file