-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
191 lines (176 loc) · 4.32 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import React, { createContext, useContext, ReactNode, useState } from "react";
import en_US from "antd/lib/locale/en_US";
import vi_VN from "antd/lib/locale/vi_VN";
import ar_EG from "antd/lib/locale/ar_EG";
import az_AZ from "antd/lib/locale/az_AZ";
import en from "./locale/en";
import vi from "./locale/vi";
import fr from "./locale/fr";
import de from "./locale/de";
import ja from "./locale/ja";
import zh from "./locale/zh";
import es from "./locale/es";
import ru from "./locale/ru";
import it from "./locale/it";
import pt from "./locale/pt";
import ko from "./locale/ko";
import th from "./locale/th";
import id from "./locale/id";
import tr from "./locale/tr";
import pl from "./locale/pl";
import nl from "./locale/nl";
import sv from "./locale/sv";
import fi from "./locale/fi";
import no from "./locale/no";
import da from "./locale/da";
import cs from "./locale/cs";
import hu from "./locale/hu";
import ro from "./locale/ro";
import el from "./locale/el";
import he from "./locale/he";
import uk from "./locale/uk";
import ms from "./locale/ms";
import hi from "./locale/hi";
import bn from "./locale/bn";
import ta from "./locale/ta";
import ur from "./locale/ur";
import fa from "./locale/fa";
import ka from "./locale/ka";
import az from "./locale/az";
import kk from "./locale/kk";
import uz from "./locale/uz";
import ky from "./locale/ky";
import tg from "./locale/tg";
import tk from "./locale/tk";
import mn from "./locale/mn";
import ps from "./locale/ps";
import sd from "./locale/sd";
import si from "./locale/si";
import ne from "./locale/ne";
import my from "./locale/my";
import km from "./locale/km";
import lo from "./locale/lo";
import { supportedLocales } from "./supportedLocales";
// Map our locales to Ant Design locales
const antLocales: any = {
en: en_US,
vi: vi_VN,
ar: ar_EG,
az: az_AZ,
};
const messages: any = {
en: en,
vi: vi,
fr: fr,
de: de,
ja: ja,
zh: zh,
es: es,
ru: ru,
it: it,
pt: pt,
ko: ko,
th: th,
id: id,
tr: tr,
pl: pl,
nl: nl,
sv: sv,
fi: fi,
no: no,
da: da,
cs: cs,
hu: hu,
ro: ro,
el: el,
he: he,
uk: uk,
ms: ms,
hi: hi,
bn: bn,
ta: ta,
ur: ur,
fa: fa,
ka: ka,
az: az,
kk: kk,
uz: uz,
ky: ky,
tg: tg,
tk: tk,
mn: mn,
ps: ps,
sd: sd,
si: si,
ne: ne,
my: my,
km: km,
lo: lo,
};
type LocaleType = {
name: string;
messages: any;
antd: any;
};
export const locales: { [key: string]: LocaleType } = supportedLocales.reduce(
(acc: any, curr: any) => {
const { key } = curr;
acc[key] = {
name: curr.label,
messages: messages[key] || en,
antd: antLocales[key] || en_US,
};
return acc;
},
{}
);
type LocaleContextType = {
locale: string;
messages: any;
antdLocale: any;
changeLocale: (locale: string) => void;
};
const LocaleContext = createContext<LocaleContextType>({
locale: "en",
messages: en,
antdLocale: en_US,
changeLocale: () => { },
});
export const LocaleProvider = ({ children }: { children: ReactNode }) => {
const [locale, setLocale] = useState("en");
const currentLocale = locales[locale as keyof typeof locales];
const changeLocale = (newLocale: string) => {
if (locales[newLocale as keyof typeof locales]) {
setLocale(newLocale);
}
};
return (
<LocaleContext.Provider
value={{
locale,
messages: currentLocale.messages,
antdLocale: currentLocale.antd,
changeLocale,
}}
>
{children}
</LocaleContext.Provider>
);
};
export const useLocale = (key: string = '') => {
const { locale, messages, antdLocale, changeLocale } = useContext(LocaleContext);
const t = React.useCallback((keys: string, vars: Record<string, string | number> = {}) => {
const pathKeys = `${key}.${keys}`.split('.').filter(p => p.length > 0);
let translation = pathKeys.reduce((obj, k) => {
return obj && typeof obj === 'object' && obj.hasOwnProperty(k) ? obj[k] : undefined;
}, messages);
if (typeof translation === 'string' && Object.keys(vars).length > 0) {
Object.keys(vars).forEach(varKey => {
const regex = new RegExp(`{${varKey}}`, 'g');
translation = translation.replace(regex, String(vars[varKey]));
});
}
return translation !== undefined ? translation : pathKeys.join('.');
}, [key, messages]);
return { locale, messages, antdLocale, changeLocale, t };
};