Skip to content

Commit 979414a

Browse files
Copilothotlong
andcommitted
fix: add error handling for i18n translation loading in AppPlugin
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 31ce8e9 commit 979414a

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

packages/runtime/src/app-plugin.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,5 +209,27 @@ describe('AppPlugin', () => {
209209

210210
expect(mockI18n.loadTranslations).toHaveBeenCalledTimes(3);
211211
});
212+
213+
it('should handle errors in loadTranslations gracefully', async () => {
214+
mockI18n.loadTranslations.mockImplementation((locale: string) => {
215+
if (locale === 'zh-CN') throw new Error('Disk read failed');
216+
});
217+
218+
const bundle = {
219+
id: 'com.test.error',
220+
translations: [
221+
{ en: { messages: { save: 'Save' } }, 'zh-CN': { messages: { save: '保存' } } },
222+
],
223+
};
224+
const plugin = new AppPlugin(bundle);
225+
await plugin.start!(mockContext);
226+
227+
// en should still be loaded despite zh-CN failure
228+
expect(mockI18n.loadTranslations).toHaveBeenCalledWith('en', { messages: { save: 'Save' } });
229+
expect(mockContext.logger.warn).toHaveBeenCalledWith(
230+
expect.stringContaining('Failed to load translations'),
231+
expect.objectContaining({ locale: 'zh-CN' })
232+
);
233+
});
212234
});
213235
});

packages/runtime/src/app-plugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,12 @@ export class AppPlugin implements Plugin {
233233
// Each bundle is a TranslationBundle: Record<locale, TranslationData>
234234
for (const [locale, data] of Object.entries(bundle)) {
235235
if (data && typeof data === 'object') {
236-
i18nService.loadTranslations(locale, data as Record<string, unknown>);
237-
loadedLocales++;
236+
try {
237+
i18nService.loadTranslations(locale, data as Record<string, unknown>);
238+
loadedLocales++;
239+
} catch (err: any) {
240+
ctx.logger.warn('[i18n] Failed to load translations', { appId, locale, error: err.message });
241+
}
238242
}
239243
}
240244
}

0 commit comments

Comments
 (0)