Skip to content

Commit df67de5

Browse files
committed
fix(worker): update i18n resources after sync
1 parent 6ed859b commit df67de5

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/services/i18n/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ const appLang = getAppLang();
1111

1212
const languages = await getListLanguages();
1313

14-
// programatically load all locales
15-
for await (const record of languages) {
16-
const language = record.path;
17-
14+
const getLangTranslations = async (language: string) => {
1815
const activities = await import(`@locales/${language}/activities.json`).then(
1916
(module) => module.default
2017
);
@@ -62,7 +59,7 @@ for await (const record of languages) {
6259
(module) => module.default
6360
);
6461

65-
resources[record.locale] = {
62+
return {
6663
ui: {
6764
...activities,
6865
...congregation,
@@ -79,6 +76,15 @@ for await (const record of languages) {
7976
songs,
8077
releases,
8178
};
79+
};
80+
81+
// programatically load all locales
82+
for (const record of languages) {
83+
const language = record.path;
84+
85+
const resource = await getLangTranslations(language);
86+
87+
resources[record.locale] = resource;
8288
}
8389

8490
const supportedLangs = languages.map((l) => l.locale);
@@ -93,3 +99,21 @@ i18n.use(initReactI18next).init({
9399
});
94100

95101
export default i18n;
102+
103+
export const refreshLocalesResources = async () => {
104+
const supportedLangs = Object.keys(i18n.options.resources);
105+
106+
const languages = await getListLanguages();
107+
108+
const newLanguages = languages.filter(
109+
(record) => !supportedLangs.includes(record.locale)
110+
);
111+
112+
for (const language of newLanguages) {
113+
const resource = await getLangTranslations(language.path);
114+
115+
for (const [key, values] of Object.entries(resource)) {
116+
i18n.addResources(language.locale, key, values);
117+
}
118+
}
119+
};

src/wrapper/web_worker/useWebWorker.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { songsBuildList } from '@services/i18n/songs';
1616
import { setSongs } from '@services/states/songs';
1717
import { setPublicTalks } from '@services/states/publicTalks';
1818
import { publicTalksBuildList } from '@services/i18n/public_talks';
19+
import { refreshLocalesResources } from '@services/i18n';
1920
import worker from '@services/worker/backupWorker';
2021
import logger from '@services/logger';
2122

@@ -53,6 +54,8 @@ const useWebWorker = () => {
5354

5455
// sync complete -> refresh app data
5556

57+
await refreshLocalesResources();
58+
5659
// load songs
5760
const songs = songsBuildList(sourceLang);
5861
setSongs(songs);

0 commit comments

Comments
 (0)