Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 118585a

Browse files
authored
Update weblateToCounterpart to be more resilient (#8633)
* Remove unused code for weblate->counterpart conversion Happens at build time instead now * Update `weblateToCounterpart` to be more resilient
1 parent d9b7e07 commit 118585a

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

__mocks__/browser-request.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
const en = require("../src/i18n/strings/en_EN");
218
const de = require("../src/i18n/strings/de_DE");
319
const lv = {
420
"Save": "Saglabāt",
521
"Uploading %(filename)s and %(count)s others|one": "Качване на %(filename)s и %(count)s друг",
622
};
723

24+
function weblateToCounterpart(inTrs) {
25+
const outTrs = {};
26+
27+
for (const key of Object.keys(inTrs)) {
28+
const keyParts = key.split('|', 2);
29+
if (keyParts.length === 2) {
30+
let obj = outTrs[keyParts[0]];
31+
if (obj === undefined) {
32+
obj = outTrs[keyParts[0]] = {};
33+
} else if (typeof obj === "string") {
34+
// This is a transitional edge case if a string went from singular to pluralised and both still remain
35+
// in the translation json file. Use the singular translation as `other` and merge pluralisation atop.
36+
obj = outTrs[keyParts[0]] = {
37+
"other": inTrs[key],
38+
};
39+
console.warn("Found entry in i18n file in both singular and pluralised form", keyParts[0]);
40+
}
41+
obj[keyParts[1]] = inTrs[key];
42+
} else {
43+
outTrs[key] = inTrs[key];
44+
}
45+
}
46+
47+
return outTrs;
48+
}
49+
850
// Mock the browser-request for the languageHandler tests to return
951
// Fake languages.json containing references to en_EN, de_DE and lv
1052
// en_EN.json
@@ -13,7 +55,7 @@ const lv = {
1355
module.exports = jest.fn((opts, cb) => {
1456
const url = opts.url || opts.uri;
1557
if (url && url.endsWith("languages.json")) {
16-
cb(undefined, {status: 200}, JSON.stringify({
58+
cb(undefined, { status: 200 }, JSON.stringify({
1759
"en": {
1860
"fileName": "en_EN.json",
1961
"label": "English",
@@ -24,16 +66,16 @@ module.exports = jest.fn((opts, cb) => {
2466
},
2567
"lv": {
2668
"fileName": "lv.json",
27-
"label": "Latvian"
28-
}
69+
"label": "Latvian",
70+
},
2971
}));
3072
} else if (url && url.endsWith("en_EN.json")) {
31-
cb(undefined, {status: 200}, JSON.stringify(en));
73+
cb(undefined, { status: 200 }, JSON.stringify(weblateToCounterpart(en)));
3274
} else if (url && url.endsWith("de_DE.json")) {
33-
cb(undefined, {status: 200}, JSON.stringify(de));
75+
cb(undefined, { status: 200 }, JSON.stringify(weblateToCounterpart(de)));
3476
} else if (url && url.endsWith("lv.json")) {
35-
cb(undefined, {status: 200}, JSON.stringify(lv));
77+
cb(undefined, { status: 200 }, JSON.stringify(weblateToCounterpart(lv)));
3678
} else {
37-
cb(true, {status: 404}, "");
79+
cb(true, { status: 404 }, "");
3880
}
3981
});

src/languageHandler.tsx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -556,35 +556,21 @@ function getLangsJson(): Promise<object> {
556556
});
557557
}
558558

559-
function weblateToCounterpart(inTrs: object): object {
560-
const outTrs = {};
561-
562-
for (const key of Object.keys(inTrs)) {
563-
const keyParts = key.split('|', 2);
564-
if (keyParts.length === 2) {
565-
let obj = outTrs[keyParts[0]];
566-
if (obj === undefined) {
567-
obj = {};
568-
outTrs[keyParts[0]] = obj;
569-
}
570-
obj[keyParts[1]] = inTrs[key];
571-
} else {
572-
outTrs[key] = inTrs[key];
573-
}
574-
}
575-
576-
return outTrs;
559+
interface ICounterpartTranslation {
560+
[key: string]: string | {
561+
[pluralisation: string]: string;
562+
};
577563
}
578564

579-
async function getLanguageRetry(langPath: string, num = 3): Promise<object> {
565+
async function getLanguageRetry(langPath: string, num = 3): Promise<ICounterpartTranslation> {
580566
return retry(() => getLanguage(langPath), num, e => {
581567
logger.log("Failed to load i18n", langPath);
582568
logger.error(e);
583569
return true; // always retry
584570
});
585571
}
586572

587-
function getLanguage(langPath: string): Promise<object> {
573+
function getLanguage(langPath: string): Promise<ICounterpartTranslation> {
588574
return new Promise((resolve, reject) => {
589575
request(
590576
{ method: "GET", url: langPath },
@@ -597,7 +583,7 @@ function getLanguage(langPath: string): Promise<object> {
597583
reject(new Error(`Failed to load ${langPath}, got ${response.status}`));
598584
return;
599585
}
600-
resolve(weblateToCounterpart(JSON.parse(body)));
586+
resolve(JSON.parse(body));
601587
},
602588
);
603589
});

0 commit comments

Comments
 (0)