-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathI18nProvider.tsx
43 lines (39 loc) · 1.46 KB
/
I18nProvider.tsx
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
import React, { createContext, useContext } from 'react'
import { useRouter } from 'next/router'
import I18nContext from './context'
import transCore from './transCore'
import useTranslation from './useTranslation'
import { I18nDictionary, I18nProviderProps } from '.'
export const InternalContext = createContext({ ns: {}, config: {} })
export default function I18nProvider({
lang: lng,
namespaces = {},
children,
config: newConfig = {},
}: I18nProviderProps) {
const { lang: parentLang } = useTranslation()
const { locale, defaultLocale } = useRouter() || {}
const internal = useContext(InternalContext)
const allNamespaces: Record<string, I18nDictionary> = {
...initialBrowserNamespaces(),
...internal.ns,
...namespaces,
}
const lang = lng || parentLang || locale || defaultLocale || ''
const config = { ...internal.config, ...newConfig }
const localesToIgnore = config.localesToIgnore || ['default']
const ignoreLang = localesToIgnore.includes(lang)
const pluralRules = new Intl.PluralRules(ignoreLang ? undefined : lang)
const t = transCore({ config, allNamespaces, pluralRules, lang })
return (
<I18nContext.Provider value={{ lang, t }}>
<InternalContext.Provider value={{ ns: allNamespaces, config }}>
{children}
</InternalContext.Provider>
</I18nContext.Provider>
)
}
function initialBrowserNamespaces() {
if (typeof window === 'undefined') return {}
return window.__NEXT_DATA__?.props?.__namespaces || {}
}