mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-03 03:26:12 +01:00
Add English fallback for i18n messages
Introduced a mergeDeep helper to merge locale-specific messages with the English defaults. src/i18n.ts now loads both the en-US messages and the selected locale’s messages, merges them, and supplies the result so missing translations appear in English instead of as placeholders.
This commit is contained in:
23
src/i18n.ts
23
src/i18n.ts
@@ -1,6 +1,19 @@
|
|||||||
import { getRequestConfig } from 'next-intl/server'
|
import { getRequestConfig } from 'next-intl/server'
|
||||||
import { getUserLocale } from './lib/locale'
|
import { getUserLocale } from './lib/locale'
|
||||||
|
|
||||||
|
function mergeDeep<T>(target: T, source: Partial<T>): T {
|
||||||
|
for (const key in source) {
|
||||||
|
const value = (source as any)[key]
|
||||||
|
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
||||||
|
;(target as any)[key] = mergeDeep((target as any)[key] || {}, value)
|
||||||
|
} else {
|
||||||
|
;(target as any)[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return target
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export const localeLabels = {
|
export const localeLabels = {
|
||||||
'en-US': 'English',
|
'en-US': 'English',
|
||||||
fi: 'Suomi',
|
fi: 'Suomi',
|
||||||
@@ -29,8 +42,14 @@ export const defaultLocale: Locale = 'en-US'
|
|||||||
export default getRequestConfig(async () => {
|
export default getRequestConfig(async () => {
|
||||||
const locale = await getUserLocale()
|
const locale = await getUserLocale()
|
||||||
|
|
||||||
|
const defaultMessages = (await import('../messages/en-US.json')).default
|
||||||
|
const localeMessages = (await import(`../messages/${locale}.json`)).default
|
||||||
|
|
||||||
|
const messages = mergeDeep(structuredClone(defaultMessages), localeMessages)
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
locale,
|
locale,
|
||||||
messages: (await import(`../messages/${locale}.json`)).default,
|
messages,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user