forked from appwrite/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.ts
More file actions
78 lines (64 loc) · 2.19 KB
/
i18n.ts
File metadata and controls
78 lines (64 loc) · 2.19 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
import i18next from 'i18next';
import type { i18n } from 'i18next';
import { writable, type Readable, type Writable } from 'svelte/store';
import resourcesToBackend from 'i18next-resources-to-backend';
export const supportedLanguages = ['de', 'en'];
interface TranslationService {
i18n: Readable<i18n>;
}
export const isLoading = writable(true);
export class I18NextTranslationStore implements TranslationService {
public i18n: Readable<i18n>;
public isLoading: Writable<boolean>;
constructor(i18n: i18n) {
this.i18n = this.createInstance(i18n);
this.isLoading = this.createLoadingInstance(i18n);
}
private createInstance(i18n: i18n): Writable<i18n> {
const i18nWritable = writable(i18n);
i18n.on('initialized', () => {
i18nWritable.set(i18n);
});
i18n.on('loaded', () => {
i18nWritable.set(i18n);
});
i18n.on('added', () => i18nWritable.set(i18n));
i18n.on('languageChanged', () => {
i18nWritable.set(i18n);
});
return i18nWritable;
}
private createLoadingInstance(i18n: i18n): Writable<boolean> {
// if loaded resources are empty || {}, set loading to true
i18n.on('loaded', (resources) => {
Object.keys(resources).length !== 0 && isLoading.set(false);
});
// if resources failed loading, set loading to true
i18n.on('failedLoading', () => {
isLoading.set(true);
});
return isLoading;
}
}
export const createI18nStore = () => {
i18next
.use(
resourcesToBackend((language, _namespace, callback) => {
import(`$lib/translations/${language}.json`)
.then((resources) => {
callback(null, resources);
})
.catch((error) => {
callback(error, null);
});
})
)
.init({
// lng: 'de',
lng: window.navigator.language,
fallbackLng: 'en'
});
const i18nStore = new I18NextTranslationStore(i18next);
return i18nStore.i18n;
};
export const _ = createI18nStore();