Skip to content

Commit 505e112

Browse files
committed
🌐 根据前缀去匹配
1 parent a4f5dd4 commit 505e112

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/locales/locales.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ export async function matchLanguage() {
7676
return lng;
7777
}
7878
}
79+
// 根据前缀去匹配
80+
const prefixMap = i18n.languages.reduce(
81+
(acc, lng) => {
82+
const prefix = lng.split("-")[0];
83+
if (!acc[prefix]) {
84+
acc[prefix] = [];
85+
}
86+
acc[prefix].push(lng);
87+
return acc;
88+
},
89+
{} as Record<string, string[]>
90+
);
91+
for (let i = 0; i < acceptLanguages.length; i += 1) {
92+
const lng = acceptLanguages[i];
93+
const prefix = lng.split("-")[0];
94+
if (prefixMap[prefix] && prefixMap[prefix].length > 0) {
95+
return prefixMap[prefix][0]; // 返回第一个匹配的语言
96+
}
97+
}
7998
return "";
8099
}
81100

0 commit comments

Comments
 (0)