-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
150 lines (124 loc) · 3.65 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
import * as React from 'react'
interface ResourseProviderFunc<TResource> {
(strings: TResource): React.ReactNode
}
interface LocalizeFunc<TResource> {
(key: keyof TResource | ResourseProviderFunc<TResource>): React.ReactNode
}
const LangContext = React.createContext({
l: (key: any) => key.toString() as React.ReactNode,
lang: 'en' as any,
onLangChange: (_: any) => {},
isLoadingLang: false,
})
interface FetchResourceFunc<TResource> {
(): Promise<TResource>
}
export type ResourceRecord<
TLangs extends keyof any,
TResource extends object
> = Record<TLangs, TResource | FetchResourceFunc<TResource>>
export interface LangProviderProps<
TLangs extends keyof any,
TResource extends object
> {
initialLang: TLangs
initialResources: ResourceRecord<TLangs, TResource>
}
interface LangProviderState<
TLangs extends keyof any,
TResource extends object
> {
lang: TLangs
onLangChange: (newLang: TLangs) => void
l: LocalizeFunc<TResource>
isLoadingLang: boolean
}
export class LangProvider<
TLangs extends keyof any,
TResource extends object
> extends React.Component<
LangProviderProps<TLangs, TResource>,
LangProviderState<TLangs, TResource>
> {
private resources: ResourceRecord<TLangs, TResource>
constructor(props: LangProviderProps<TLangs, TResource>) {
super(props)
this.resources = props.initialResources
this.state = {
lang: props.initialLang,
onLangChange: this.handleLangChange,
l: this.localize,
isLoadingLang: false,
}
const selectedResouce = this.resources[props.initialLang]
if (this.isFetchFunc(selectedResouce)) {
;(this.state as LangProviderState<TLangs, TResource>).isLoadingLang = true
this.enrichResources(selectedResouce, props.initialLang).then(() =>
this.setState({ isLoadingLang: false }),
)
}
}
private isKeyof(
key: keyof TResource | ResourseProviderFunc<TResource>,
): key is keyof TResource {
return typeof key === 'string'
}
private isObject(
resource: TResource | FetchResourceFunc<TResource>,
): resource is TResource {
return typeof resource === 'object'
}
private isFetchFunc(
resource: TResource | FetchResourceFunc<TResource>,
): resource is FetchResourceFunc<TResource> {
return resource instanceof Function
}
private handleLangChange = (newLang: TLangs) => {
const resource = this.resources[newLang]
if (this.isFetchFunc(resource)) {
this.setState({ isLoadingLang: true })
this.enrichResources(resource, newLang).then(() =>
this.setState({ lang: newLang, isLoadingLang: false }),
)
} else {
this.setState({ lang: newLang })
}
}
private enrichResources = (
resource: FetchResourceFunc<TResource>,
lang: TLangs,
) =>
resource().then(res => {
this.resources = { ...(this.resources as any), [lang]: res }
})
private localize: LocalizeFunc<TResource> = key => {
const { lang } = this.state
const resource = this.resources[lang]
if (this.isObject(resource)) {
if (this.isKeyof(key)) return resource[key]
else return key(resource)
}
if (this.isFetchFunc(resource))
throw Error('resource should be loaded at this moment')
throw Error('resource is neither object nor promise create function')
}
render() {
return (
<LangContext.Provider value={this.state}>
{this.props.children}
</LangContext.Provider>
)
}
}
export class LangConsumer<
TLangs extends keyof any,
TResource extends object
> extends React.Component<
React.ConsumerProps<LangProviderState<TLangs, TResource>>,
{}
> {
render() {
return <LangContext.Consumer {...this.props} />
}
}