-
Notifications
You must be signed in to change notification settings - Fork 50
/
i18nContext.tsx
40 lines (34 loc) · 1.02 KB
/
i18nContext.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
import React, { createContext, useContext } from 'react'
import { getStringTranslator, Locale, StringTranslator, i18nContextType } from './translator'
const dummyContext: i18nContextType = {
locale: { id: 'en-US', label: 'English' },
locales: [
{ id: 'en-US', label: 'English' },
{ id: 'es-ES', label: 'Español' }
],
getString: (x: string) => ({
t: x,
tHtml: x,
fallback: x,
locale: { id: 'en-US', label: 'English' }
})
}
export const I18nContext = createContext(dummyContext)
export const I18nContextProvider = ({ locale, locales, children }) => {
const getString = getStringTranslator(locale)
const value = {
locale,
locales,
// translate,
getString,
foo: 4356
}
return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>
}
export const useI18n = () => {
return useContext<{
locale?: Locale
locales?: Locale[]
getString?: StringTranslator
}>(I18nContext)
}