I18NContext.tsx
1.15 KB
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
import React, { useState, useEffect, FC, useContext, useCallback } from 'react';
import * as RNLocalize from 'react-native-localize';
import { I18NStrings, en } from './en';
import { zh } from './zh';
import { I18nManager } from 'react-native';
const availableTranslations = { en, zh };
const I18NContext = React.createContext<I18NStrings>(en);
export const I18NProvider: FC = ({ children }) => {
const [translation, setTranslation] = useState(en);
const updateTranslation = useCallback(() => {
const fallback = { languageTag: 'en', isRTL: false } as const;
const { languageTag, isRTL } =
RNLocalize.findBestAvailableLanguage(['en', 'zh']) || fallback;
I18nManager.forceRTL(isRTL);
setTranslation(availableTranslations[languageTag]);
}, []);
useEffect(() => {
updateTranslation();
RNLocalize.addEventListener('change', updateTranslation);
return () => {
RNLocalize.removeEventListener('change', updateTranslation);
};
}, [updateTranslation]);
return (
<I18NContext.Provider value={translation}>{children}</I18NContext.Provider>
);
};
export function useI18nStrings() {
return useContext(I18NContext);
}