aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/i18n/I18nStore.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/i18n/I18nStore.ts')
-rw-r--r--packages/main/src/i18n/I18nStore.ts31
1 files changed, 30 insertions, 1 deletions
diff --git a/packages/main/src/i18n/I18nStore.ts b/packages/main/src/i18n/I18nStore.ts
index c364f0e..54c3d20 100644
--- a/packages/main/src/i18n/I18nStore.ts
+++ b/packages/main/src/i18n/I18nStore.ts
@@ -18,7 +18,7 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import type { i18n, TFunction } from 'i18next'; 21import type { i18n, ResourceKey, TFunction } from 'i18next';
22import { IAtom, createAtom } from 'mobx'; 22import { IAtom, createAtom } from 'mobx';
23 23
24import { getLogger } from '../utils/log'; 24import { getLogger } from '../utils/log';
@@ -113,4 +113,33 @@ export default class I18nStore {
113 }); 113 });
114 log.debug('Reloaded translations'); 114 log.debug('Reloaded translations');
115 } 115 }
116
117 async getTranslation(
118 language: string,
119 namespace: string,
120 ): Promise<ResourceKey> {
121 if (!this.i18next.hasResourceBundle(language, namespace)) {
122 await this.i18next.loadLanguages([language]);
123 await this.i18next.loadNamespaces([namespace]);
124 }
125 const bundle = this.i18next.getResourceBundle(
126 language,
127 namespace,
128 ) as unknown;
129 if (typeof bundle !== 'object' || bundle === null) {
130 throw new Error(
131 `Failed to load ${namespace} resource bundle for language ${language}`,
132 );
133 }
134 return bundle as ResourceKey;
135 }
136
137 addMissingTranslation(
138 languages: string[],
139 namespace: string,
140 key: string,
141 value: string,
142 ): void {
143 this.i18next.modules.backend?.create?.(languages, namespace, key, value);
144 }
116} 145}