diff options
Diffstat (limited to 'packages/main/src/i18n/loadLocalization.ts')
-rw-r--r-- | packages/main/src/i18n/loadLocalization.ts | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/packages/main/src/i18n/loadLocalization.ts b/packages/main/src/i18n/loadLocalization.ts index 1408a30..ec3cf84 100644 --- a/packages/main/src/i18n/loadLocalization.ts +++ b/packages/main/src/i18n/loadLocalization.ts | |||
@@ -18,16 +18,22 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { fallbackLng } from '@sophie/shared'; | ||
21 | import i18next from 'i18next'; | 22 | import i18next from 'i18next'; |
23 | import { autorun } from 'mobx'; | ||
24 | import { addDisposer } from 'mobx-state-tree'; | ||
22 | 25 | ||
23 | import type Resources from '../infrastructure/resources/Resources'; | 26 | import type Resources from '../infrastructure/resources/Resources'; |
24 | import type MainStore from '../stores/MainStore'; | 27 | import type MainStore from '../stores/MainStore'; |
28 | import { getLogger } from '../utils/log'; | ||
25 | 29 | ||
26 | import I18nStore from './I18nStore'; | 30 | import I18nStore from './I18nStore'; |
27 | import RepositoryBasedI18nBackend from './RepositoryBasedI18nBackend'; | 31 | import RepositoryBasedI18nBackend from './RepositoryBasedI18nBackend'; |
28 | import i18nLog from './i18nLog'; | 32 | import i18nLog from './i18nLog'; |
29 | import LocalizationFiles from './impl/LocaltizationFiles'; | 33 | import LocalizationFiles from './impl/LocaltizationFiles'; |
30 | 34 | ||
35 | const log = getLogger('loadLocationzation'); | ||
36 | |||
31 | export default async function loadLocalization( | 37 | export default async function loadLocalization( |
32 | store: MainStore, | 38 | store: MainStore, |
33 | resources: Resources, | 39 | resources: Resources, |
@@ -37,14 +43,25 @@ export default async function loadLocalization( | |||
37 | const backend = new RepositoryBasedI18nBackend(repository, devMode); | 43 | const backend = new RepositoryBasedI18nBackend(repository, devMode); |
38 | const i18n = i18next | 44 | const i18n = i18next |
39 | .createInstance({ | 45 | .createInstance({ |
40 | lng: 'en', | 46 | lng: store.shared.language, |
41 | fallbackLng: ['en'], | 47 | fallbackLng, |
42 | debug: devMode, | 48 | debug: devMode, |
43 | saveMissing: devMode, | 49 | saveMissing: devMode, |
44 | }) | 50 | }) |
45 | .use(backend) | 51 | .use(backend) |
46 | .use(i18nLog); | 52 | .use(i18nLog); |
47 | await i18n.init(); | ||
48 | const i18nStore = new I18nStore(i18n); | 53 | const i18nStore = new I18nStore(i18n); |
49 | store.setI18n(i18nStore); | 54 | store.setI18n(i18nStore); |
55 | await i18n.init(); | ||
56 | const disposeChangeLanguage = autorun(() => { | ||
57 | const { | ||
58 | shared: { language }, | ||
59 | } = store; | ||
60 | if (i18n.language !== language) { | ||
61 | i18n.changeLanguage(language).catch((error) => { | ||
62 | log.error('Failed to change language', error); | ||
63 | }); | ||
64 | } | ||
65 | }); | ||
66 | addDisposer(store, disposeChangeLanguage); | ||
50 | } | 67 | } |