Skip to content

Commit c0e8479

Browse files
committed
fix: Add support for the fallbackLang on SSR
1 parent 16bfa62 commit c0e8479

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function isLoaded(lang?: string): boolean {
4242
return loaded.some((row) => row.lang.replace(/[-_]/g, '-') === lang.replace(/[-_]/g, '-'))
4343
}
4444

45-
function loadLanguage(lang: string): void {
45+
function loadLanguage(lang: string, dashLangTry: boolean = false): void {
4646
const loadedLang: LanguageInterface = loaded.find((row) => row.lang === lang)
4747

4848
if (loadedLang) {
@@ -52,6 +52,20 @@ function loadLanguage(lang: string): void {
5252
}
5353

5454
const { default: messages } = resolveLang(options.resolve, lang);
55+
56+
if (Object.keys(messages).length < 1) {
57+
if (/[-_]/g.test(lang) && !dashLangTry) {
58+
return loadLanguage(
59+
lang.replace(/[-_]/g, (char) => (char === '-' ? '_' : '-')),
60+
true
61+
);
62+
}
63+
64+
if (lang !== options.fallbackLang) {
65+
return loadLanguage(options.fallbackLang);
66+
}
67+
}
68+
5569
const data: LanguageInterface = { lang, messages }
5670
loaded.push(data)
5771
setLanguage(data)

0 commit comments

Comments
 (0)