Skip to content

Commit aab0e03

Browse files
committed
fix(web): ensure i18n are loaded before marking ready
1 parent 3f789bd commit aab0e03

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/utils/i18n.utils.ts

+22-17
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
import type { Locale } from '~/models/i18n.model';
1+
import type { Locale, Locales } from '~/models/i18n.model';
22

33
import { useI18nStore } from '~/stores/i18n.store';
44
import { useRouterStore } from '~/stores/router.store';
55
import { useI18nTranslate } from '~/utils/browser/browser-i18n.utils';
66
import { chromeI18n } from '~/utils/browser/browser.utils';
77

8+
export const initLocalI18n = () => {
9+
const store = useI18nStore();
10+
const router = useRouterStore();
11+
let promise: Promise<Locales | void> | undefined;
12+
if (import.meta.hot) {
13+
console.info('Listening to i18n HMR changes');
14+
import.meta.hot.send('fetch:i18n');
15+
import.meta.hot.on('update:i18n', (data: { lang: string; locale: Locale }[]) => {
16+
data?.forEach(({ lang, locale }) => store.addLocale(locale, lang));
17+
});
18+
} else if (!store.locales?.[store.lang]) {
19+
promise = fetch(new URL(`${router.baseUrl ?? './'}_locales/${store.lang}/messages.json`, new URL(import.meta.url).origin))
20+
.then(r => r.json())
21+
.then((locale: Locale) => store.addLocale(locale))
22+
.catch(err => console.error(`Failed to fetch locale '${store.lang}'`, err));
23+
}
24+
25+
return { store, router, promise };
26+
};
27+
828
/**
929
* Setup i18n function to either use chrome i18n resolver or a local store (for web use).
1030
* @param roots modules names
1131
* @see chrome.i18n.getMessage
1232
*/
1333
export const useI18n = (...roots: string[]): ReturnType<typeof useI18nTranslate> => {
1434
if (!chromeI18n) {
15-
const store = useI18nStore();
16-
const router = useRouterStore();
17-
18-
if (import.meta.hot) {
19-
console.info('Listening to i18n HMR changes');
20-
import.meta.hot.send('fetch:i18n');
21-
import.meta.hot.on('update:i18n', (data: { lang: string; locale: Locale }[]) => {
22-
data?.forEach(({ lang, locale }) => store.addLocale(locale, lang));
23-
});
24-
} else if (!store.locales?.[store.lang]) {
25-
fetch(new URL(`${router.baseUrl ?? './'}_locales/${store.lang}/messages.json`, new URL(import.meta.url).origin))
26-
.then(r => r.json())
27-
.then((locale: Locale) => store.addLocale(locale))
28-
.catch(err => console.error(`Failed to fetch locale '${store.lang}'`, err));
29-
}
30-
35+
const { store } = initLocalI18n();
3136
return (value, ...modules) => store.i18n(value, ...(modules?.length ? modules : roots));
3237
}
3338

src/web/init-services.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TraktService } from '~/services/trakt.service';
22
import { useAppStateStore } from '~/stores/app-state.store';
33
import { useAuthSettingsStore } from '~/stores/settings/auth.store';
44
import { useUserSettingsStore } from '~/stores/settings/user.store';
5+
import { initLocalI18n } from '~/utils';
56

67
export const initServices = async () => {
78
const { setAppReady } = useAppStateStore();
@@ -15,6 +16,8 @@ export const initServices = async () => {
1516

1617
TraktService.listen();
1718

19+
await initLocalI18n().promise;
20+
1821
setAppReady(true);
1922

2023
await syncRestoreAllUsers();

0 commit comments

Comments
 (0)