forked from Stellar-IndigoPay/Stellar-IndigoPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.tsx
More file actions
128 lines (115 loc) · 3.05 KB
/
Copy pathi18n.tsx
File metadata and controls
128 lines (115 loc) · 3.05 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
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
/**
* lib/i18n.tsx — Lightweight i18n context with JSON locale files, pluralization, and interpolation.
*/
import {
createContext,
useContext,
useState,
useCallback,
type ReactNode,
} from "react";
import en from "@/locales/en.json";
import es from "@/locales/es.json";
import fr from "@/locales/fr.json";
export type Locale = "en" | "es" | "fr";
const locales: Record<Locale, Record<string, any>> = { en, es, fr };
interface I18nContextValue {
locale: Locale;
setLocale: (l: Locale) => void;
t: (key: string, params?: Record<string, string | number>) => string;
tPlural: (
key: string,
count: number,
params?: Record<string, string | number>
) => string;
}
function get(obj: Record<string, any>, path: string): any {
return path.split(".").reduce((acc: any, part) => acc?.[part], obj);
}
const defaultT = (
key: string,
params?: Record<string, string | number>
): string => {
let message = get(en, key) ?? key;
if (typeof message !== "string") {
message = key;
}
if (params) {
for (const [k, v] of Object.entries(params)) {
message = message.replace(
new RegExp(`\\{\\{${k}\\}\\}|\\{${k}\\}`, "g"),
String(v)
);
}
}
return message;
};
const defaultTPlural = (
key: string,
count: number,
params?: Record<string, string | number>
): string => {
const suffix = count === 1 ? "one" : "other";
const pluralKey = `${key}.${suffix}`;
return defaultT(pluralKey, { ...params, count });
};
const I18nContext = createContext<I18nContextValue>({
locale: "en",
setLocale: () => {},
t: defaultT,
tPlural: defaultTPlural,
});
export function I18nProvider({ children }: { children: ReactNode }) {
const [locale, setLocale] = useState<Locale>(() => {
if (typeof window !== "undefined") {
return (localStorage.getItem("locale") as Locale) || "en";
}
return "en";
});
const handleSetLocale = useCallback((l: Locale) => {
setLocale(l);
if (typeof window !== "undefined") {
localStorage.setItem("locale", l);
}
}, []);
const t = useCallback(
(key: string, params?: Record<string, string | number>): string => {
let message = get(locales[locale], key) ?? get(locales["en"], key) ?? key;
if (typeof message !== "string") {
message = key;
}
if (params) {
for (const [k, v] of Object.entries(params)) {
message = message.replace(
new RegExp(`\\{\\{${k}\\}\\}|\\{${k}\\}`, "g"),
String(v)
);
}
}
return message;
},
[locale]
);
const tPlural = useCallback(
(
key: string,
count: number,
params?: Record<string, string | number>
): string => {
const suffix = count === 1 ? "one" : "other";
const pluralKey = `${key}.${suffix}`;
return t(pluralKey, { ...params, count });
},
[t]
);
return (
<I18nContext.Provider
value={{ locale, setLocale: handleSetLocale, t, tPlural }}
>
{children}
</I18nContext.Provider>
);
}
export function useI18n() {
return useContext(I18nContext);
}