From 608249745ae3215c51acb6f28a1af61640f0f9e7 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 15 Sep 2020 13:19:47 +0100 Subject: [PATCH] Attempt to fix tests some more --- src/languageHandler.tsx | 15 ++++++++++++--- test/i18n-test/languageHandler-test.js | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/languageHandler.tsx b/src/languageHandler.tsx index d9feec95b1..e699f8e301 100644 --- a/src/languageHandler.tsx +++ b/src/languageHandler.tsx @@ -27,6 +27,7 @@ import PlatformPeg from "./PlatformPeg"; // @ts-ignore - $webapp is a webpack resolve alias pointing to the output directory, see webpack config import webpackLangJsonUrl from "$webapp/i18n/languages.json"; import { SettingLevel } from "./settings/SettingLevel"; +import {retry} from "./utils/promise"; const i18nFolder = 'i18n/'; @@ -327,7 +328,7 @@ export function setLanguage(preferredLangs: string | string[]) { console.error("Unable to find an appropriate language"); } - return getLanguage(i18nFolder + availLangs[langToUse].fileName); + return getLanguageRetry(i18nFolder + availLangs[langToUse].fileName); }).then((langData) => { counterpart.registerTranslations(langToUse, langData); counterpart.setLocale(langToUse); @@ -336,7 +337,7 @@ export function setLanguage(preferredLangs: string | string[]) { // Set 'en' as fallback language: if (langToUse !== "en") { - return getLanguage(i18nFolder + availLangs['en'].fileName); + return getLanguageRetry(i18nFolder + availLangs['en'].fileName); } }).then((langData) => { if (langData) counterpart.registerTranslations('en', langData); @@ -482,7 +483,15 @@ function weblateToCounterpart(inTrs: object): object { return outTrs; } -function getLanguage(langPath: string): object { +async function getLanguageRetry(langPath: string, num = 3): Promise { + return retry(() => getLanguage(langPath), num, e => { + console.log("Failed to load i18n", langPath); + console.error(e); + return true; // always retry + }); +} + +function getLanguage(langPath: string): Promise { return new Promise((resolve, reject) => { request( { method: "GET", url: langPath }, diff --git a/test/i18n-test/languageHandler-test.js b/test/i18n-test/languageHandler-test.js index 7968186e9e..b9bc955269 100644 --- a/test/i18n-test/languageHandler-test.js +++ b/test/i18n-test/languageHandler-test.js @@ -12,11 +12,11 @@ describe('languageHandler', function() { languageHandler.setMissingEntryGenerator(key => key.split("|", 2)[1]); }); - it('translates a string to german', function() { + it('translates a string to german', function(done) { languageHandler.setLanguage('de').then(function() { const translated = languageHandler._t('Rooms'); expect(translated).toBe('Räume'); - }); + }).then(done); }); it('handles plurals', function() {