Attempt to fix tests some more

This commit is contained in:
Michael Telatynski 2020-09-15 13:19:47 +01:00
parent 5202037eeb
commit 608249745a
2 changed files with 14 additions and 5 deletions

View File

@ -27,6 +27,7 @@ import PlatformPeg from "./PlatformPeg";
// @ts-ignore - $webapp is a webpack resolve alias pointing to the output directory, see webpack config // @ts-ignore - $webapp is a webpack resolve alias pointing to the output directory, see webpack config
import webpackLangJsonUrl from "$webapp/i18n/languages.json"; import webpackLangJsonUrl from "$webapp/i18n/languages.json";
import { SettingLevel } from "./settings/SettingLevel"; import { SettingLevel } from "./settings/SettingLevel";
import {retry} from "./utils/promise";
const i18nFolder = 'i18n/'; const i18nFolder = 'i18n/';
@ -327,7 +328,7 @@ export function setLanguage(preferredLangs: string | string[]) {
console.error("Unable to find an appropriate language"); console.error("Unable to find an appropriate language");
} }
return getLanguage(i18nFolder + availLangs[langToUse].fileName); return getLanguageRetry(i18nFolder + availLangs[langToUse].fileName);
}).then((langData) => { }).then((langData) => {
counterpart.registerTranslations(langToUse, langData); counterpart.registerTranslations(langToUse, langData);
counterpart.setLocale(langToUse); counterpart.setLocale(langToUse);
@ -336,7 +337,7 @@ export function setLanguage(preferredLangs: string | string[]) {
// Set 'en' as fallback language: // Set 'en' as fallback language:
if (langToUse !== "en") { if (langToUse !== "en") {
return getLanguage(i18nFolder + availLangs['en'].fileName); return getLanguageRetry(i18nFolder + availLangs['en'].fileName);
} }
}).then((langData) => { }).then((langData) => {
if (langData) counterpart.registerTranslations('en', langData); if (langData) counterpart.registerTranslations('en', langData);
@ -482,7 +483,15 @@ function weblateToCounterpart(inTrs: object): object {
return outTrs; return outTrs;
} }
function getLanguage(langPath: string): object { async function getLanguageRetry(langPath: string, num = 3): Promise<object> {
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<object> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request( request(
{ method: "GET", url: langPath }, { method: "GET", url: langPath },

View File

@ -12,11 +12,11 @@ describe('languageHandler', function() {
languageHandler.setMissingEntryGenerator(key => key.split("|", 2)[1]); 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() { languageHandler.setLanguage('de').then(function() {
const translated = languageHandler._t('Rooms'); const translated = languageHandler._t('Rooms');
expect(translated).toBe('Räume'); expect(translated).toBe('Räume');
}); }).then(done);
}); });
it('handles plurals', function() { it('handles plurals', function() {