-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathAppContext.tsx
More file actions
218 lines (184 loc) · 5.84 KB
/
AppContext.tsx
File metadata and controls
218 lines (184 loc) · 5.84 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as i18n from '@solid-primitives/i18n';
import * as storage from '@solid-primitives/storage';
import { ResourceMetadata, getGuideDirectory } from '@solid.js/docs';
import * as router from '@solidjs/router';
import {
ParentComponent,
Suspense,
createContext,
createEffect,
createResource,
startTransition,
useContext,
} from 'solid-js';
import { createStore } from 'solid-js/store';
import { Meta, Title } from '@solidjs/meta';
// en dictionary is loaded by default
import { dict as en_dict } from '../lang/en/en';
type RawDictionary = typeof en_dict;
export type Locale =
| 'en'
| 'az'
| 'it'
| 'de'
| 'pt'
| 'ja'
| 'fr'
| 'id'
| 'he'
| 'ru'
| 'ar'
| 'fa'
| 'tr'
| 'tl'
| 'ko-kr'
| 'zh-cn'
| 'zh-tw'
| 'es'
| 'pl'
| 'uk';
/*
for validating of other dictionaries have same keys as en dictionary
some might be missing, but the shape should be the same
*/
type DeepPartial<T> =
T extends Record<string, unknown> ? { [K in keyof T]?: DeepPartial<T[K]> } : T;
const raw_dict_map: Record<Locale, () => Promise<{ dict: DeepPartial<RawDictionary> }>> = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
en: () => null as any, // en is loaded by default
az: () => import('../lang/az/az'),
it: () => import('../lang/it/it'),
de: () => import('../lang/de/de'),
pt: () => import('../lang/pt/pt'),
ja: () => import('../lang/ja/ja'),
fr: () => import('../lang/fr/fr'),
id: () => import('../lang/id/id'),
he: () => import('../lang/he/he'),
ru: () => import('../lang/ru/ru'),
ar: () => import('../lang/ar/ar'),
fa: () => import('../lang/fa/fa'),
tr: () => import('../lang/tr/tr'),
tl: () => import('../lang/tl/tl'),
'ko-kr': () => import('../lang/ko-kr/ko-kr'),
'zh-cn': () => import('../lang/zh-cn/zh-cn'),
'zh-tw': () => import('../lang/zh-tw/zh-tw'),
es: () => import('../lang/es/es'),
pl: () => import('../lang/pl/pl'),
uk: () => import('../lang/uk/uk'),
};
export type Dictionary = i18n.Flatten<RawDictionary>;
const en_flat_dict: Dictionary = i18n.flatten(en_dict);
async function fetchDictionary(locale: Locale): Promise<Dictionary> {
if (locale === 'en') return en_flat_dict;
const { dict } = await raw_dict_map[locale]();
const flat_dict = i18n.flatten(dict) as RawDictionary;
return { ...en_flat_dict, ...flat_dict };
}
// Some browsers does not map correctly to some locale code
// due to offering multiple locale code for similar language (e.g. tl and fil)
// This object maps it to correct `langs` key
const LANG_ALIASES: Partial<Record<string, Locale>> = {
fil: 'tl',
};
const toLocale = (string: string): Locale | undefined =>
string in raw_dict_map
? (string as Locale)
: string in LANG_ALIASES
? (LANG_ALIASES[string] as Locale)
: undefined;
interface Settings {
locale: Locale;
dark: boolean;
}
function initialLocale(location: router.Location): Locale {
let locale: Locale | undefined;
locale = toLocale(location.query.locale);
if (locale) return locale;
locale = toLocale(navigator.language.slice(0, 2));
if (locale) return locale;
locale = toLocale(navigator.language.toLocaleLowerCase());
if (locale) return locale;
return 'en';
}
function initialSettings(location: router.Location): Settings {
return {
locale: initialLocale(location),
dark: window.matchMedia('(prefers-color-scheme: dark)').matches,
};
}
function deserializeSettings(value: string, location: router.Location): Settings {
const parsed = JSON.parse(value) as unknown;
if (!parsed || typeof parsed !== 'object') return initialSettings(location);
return {
locale:
('locale' in parsed && typeof parsed.locale === 'string' && toLocale(parsed.locale)) ||
initialLocale(location),
dark: 'dark' in parsed && typeof parsed.dark === 'boolean' ? parsed.dark : false,
};
}
interface AppState {
get isDark(): boolean;
setDark(value: boolean): void;
get locale(): Locale;
setLocale(value: Locale): void;
t: i18n.Translator<Dictionary>;
get dir(): 'ltr' | 'rtl';
get guides(): ResourceMetadata[] | undefined;
}
const AppContext = createContext<AppState>({} as AppState);
export const useAppState = () => useContext(AppContext);
export const AppContextProvider: ParentComponent = (props) => {
const location = router.useLocation();
const now = new Date();
const cookieOptions: storage.CookieOptions = {
expires: new Date(now.getFullYear() + 1, now.getMonth(), now.getDate()),
};
const [settings, set] = storage.makePersisted(createStore(initialSettings(location)), {
storageOptions: cookieOptions,
storage: storage.cookieStorage,
deserialize: (value) => deserializeSettings(value, location),
});
const locale = () => settings.locale;
const [dict] = createResource(locale, fetchDictionary, { initialValue: en_flat_dict });
const [guidesList] = createResource(locale, getGuideDirectory);
createEffect(() => {
document.documentElement.lang = settings.locale;
});
createEffect(() => {
if (settings.dark) document.documentElement.classList.add('dark');
else document.documentElement.classList.remove('dark');
});
const t = i18n.translator(dict, i18n.resolveTemplate);
const state: AppState = {
get isDark() {
return settings.dark;
},
setDark(value) {
set('dark', value);
},
get locale() {
return settings.locale;
},
setLocale(value) {
void startTransition(() => {
set('locale', value);
});
},
t,
get dir() {
return t('global.dir') === 'ltr' ? 'ltr' : 'rtl';
},
get guides() {
return guidesList();
},
};
return (
<Suspense>
<AppContext.Provider value={state}>
<Title>{t('global.title')}</Title>
<Meta name="lang" content={locale()} />
<div dir={state.dir}>{props.children}</div>
</AppContext.Provider>
</Suspense>
);
};