Skip to content

Commit 06400ad

Browse files
authored
fix: Use default messages from en as fallback if messages are missing in another locale (#7054)
* fix: Use default messages from `en` as fallback if messages are missing in another locale * chore: Remove `deepmerge` dependency in favor of a more optimized custom function * chore: Revert changes to lockfile
1 parent db37c53 commit 06400ad

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

apps/site/i18n.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@ import { getRequestConfig } from 'next-intl/server';
33

44
import { availableLocaleCodes } from '@/next.locales.mjs';
55

6+
import deepMerge from './util/deepMerge';
7+
68
// Loads the Application Locales/Translations Dynamically
79
const loadLocaleDictionary = async (locale: string) => {
10+
// This enables HMR on the English Locale, so that instant refresh
11+
// happens while we add/change texts on the source locale
12+
const defaultMessages = await import(
13+
'@node-core/website-i18n/locales/en.json'
14+
).then(f => f.default);
15+
816
if (locale === 'en') {
9-
// This enables HMR on the English Locale, so that instant refresh
10-
// happens while we add/change texts on the source locale
11-
return import('@node-core/website-i18n/locales/en.json').then(
12-
f => f.default
13-
);
17+
return defaultMessages;
1418
}
1519

1620
if (availableLocaleCodes.includes(locale)) {
1721
// Other languages don't really require HMR as they will never be development languages
1822
// so we can load them dynamically
19-
return importLocale(locale);
23+
const messages = await importLocale(locale);
24+
25+
// Use default messages as fallback
26+
return deepMerge(defaultMessages, messages);
2027
}
2128

2229
throw new Error(`Unsupported locale: ${locale}`);

apps/site/util/deepMerge.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default function deepMerge<Obj1 extends object, Obj2 extends object>(
2+
obj1: Obj1,
3+
obj2: Obj2
4+
): Obj1 & Obj2 {
5+
const result = { ...obj1 } as Obj1 & Obj2;
6+
7+
for (const key in obj2) {
8+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
9+
if (typeof obj2[key] === 'object' && obj2[key] !== null) {
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
result[key] = deepMerge(result[key] as any, obj2[key] as any);
12+
} else {
13+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14+
result[key] = obj2[key] as any;
15+
}
16+
}
17+
}
18+
19+
return result;
20+
}

0 commit comments

Comments
 (0)