-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.ts
50 lines (41 loc) · 1.43 KB
/
i18n.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Remove server-only import as it's not needed for this i18n configuration
// import 'server-only'
import linguiConfig from '@/lingui.config'
import { I18n, Messages, setupI18n } from '@lingui/core'
const { locales } = linguiConfig
// optionally use a stricter union type
type SupportedLocales = string
async function loadCatalog(locale: SupportedLocales): Promise<{
[k: string]: Messages
}> {
const { messages } = await import(`./locales/${locale}.json`)
return {
[locale]: messages
}
}
const catalogs = await Promise.all(locales.map(loadCatalog))
// transform array of catalogs into a single object
export const allMessages = catalogs.reduce((acc, oneCatalog) => {
return { ...acc, ...oneCatalog }
}, {})
type AllI18nInstances = { [K in SupportedLocales]: I18n }
export const allI18nInstances: AllI18nInstances = locales.reduce(
(acc, locale) => {
const messages = allMessages[locale] ?? {}
const i18n = setupI18n({
locale,
messages: { [locale]: messages }
})
return { ...acc, [locale]: i18n }
},
{}
)
export const getI18nInstance = (locale: SupportedLocales): I18n => {
if (!allI18nInstances[locale]) {
console.warn(`No i18n instance found for locale "${locale}"`)
}
return allI18nInstances[locale]! || allI18nInstances['en']!
}
// export function canonicalUrl(path: string, lang: SupportedLocales) {
// return `${baseUrl}${lang === linguiConfig.sourceLocale ? '' : `/${lang}`}${path}`
// }