forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n-provider.tsx
More file actions
60 lines (53 loc) · 1.4 KB
/
i18n-provider.tsx
File metadata and controls
60 lines (53 loc) · 1.4 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
import React, {
Context,
createContext,
FunctionComponent,
ReactNode,
useEffect,
useState,
} from "react";
import { getTranslations, Translations, messageFormat } from "./translations";
export const I18NContext: Context<Translations> = createContext(
new Translations({}),
);
interface I18NProviderProps {
children: ReactNode;
}
function defaultTranslations() {
const fmt = messageFormat("en");
const messages = {
"Util.millisecond": "{0} ms",
"Util.second": "{0} sec",
"Util.minute": "{0} min",
"Util.hour": "{0} hr",
"Util.day": "{0} {0,choice,0#days|1#day|1<days}",
"Util.month": "{0} mo",
"Util.year": "{0} yr",
};
return new Translations(
Object.fromEntries(
Object.entries(messages).map(([key, value]) => [key, fmt.compile(value)]),
),
);
}
export const I18NProvider: FunctionComponent<I18NProviderProps> = ({
children,
}) => {
const locale =
document
.querySelector("div[data-user-locale]")
?.getAttribute("data-user-locale") ?? "en";
const [translations, setTranslations] = useState<Translations>(
defaultTranslations(),
);
useEffect(() => {
const fetchTranslations = async () => {
const translations = await getTranslations(locale);
setTranslations(translations);
};
fetchTranslations();
}, []);
return (
<I18NContext.Provider value={translations}>{children}</I18NContext.Provider>
);
};