-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathuseLocale.tsx
More file actions
81 lines (71 loc) · 2.08 KB
/
Copy pathuseLocale.tsx
File metadata and controls
81 lines (71 loc) · 2.08 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
import { createContext, useContext, useMemo } from 'react';
import defaultLocaleData from './zh-CN';
export interface Locale {
locale: string;
BlockHeader: { expand: string; collapse: string };
Catalogue: { searchPlaceholder: string; inputPlaceholder: string };
Chat: {
stopped: string;
stop: string;
regenerate: string;
conversationEmpty: string;
today: string;
yesterday: string;
recent7Days: string;
recent15Days: string;
recent30Days: string;
other: string;
};
Copy: { copied: string; copy: string };
Dropdown: { selectAll: string; resetText: string; okText: string };
ErrorBoundary: {
please: string;
get: string;
refresh: string;
title: string;
};
FilterRules: {
message: string;
and: string;
or: string;
};
Fullscreen: { exitFull: string; full: string };
GlobalLoading: { loading: string };
Input: {
case: string;
precise: string;
front: string;
tail: string;
};
NotFound: {
description: string;
};
Modal: {
okText: string;
cancelText: string;
notYetText: string;
};
SpreadSheet: {
description: string;
copy: string;
copyCol: string;
copyAll: string;
};
}
export interface LocaleContextProps {
locale: Locale;
}
export const LocaleContext = createContext<LocaleContextProps>({ locale: {} as Locale });
export type LocaleComponentName = keyof Locale;
const useLocale = <C extends LocaleComponentName = LocaleComponentName>(
componentName: C
): NonNullable<Locale[C]> => {
const fullLocale = useContext<LocaleContextProps>(LocaleContext);
const getLocale = useMemo(() => {
const locale = defaultLocaleData[componentName] ?? {};
const localeFromContext = fullLocale?.locale[componentName] ?? {};
return Object.assign({}, locale, localeFromContext) as NonNullable<Locale[C]>;
}, [componentName, fullLocale]);
return getLocale;
};
export default useLocale;