2022-05-17 16:44:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-12-17 11:24:37 +00:00
|
|
|
const en = require("../src/i18n/strings/en_EN");
|
2020-09-15 12:24:20 +00:00
|
|
|
const de = require("../src/i18n/strings/de_DE");
|
2022-01-05 10:37:28 +00:00
|
|
|
const lv = {
|
|
|
|
"Save": "Saglabāt",
|
2022-01-07 15:20:24 +00:00
|
|
|
"Uploading %(filename)s and %(count)s others|one": "Качване на %(filename)s и %(count)s друг",
|
2022-01-05 10:37:28 +00:00
|
|
|
};
|
2019-12-17 11:24:37 +00:00
|
|
|
|
2022-05-17 16:44:29 +00:00
|
|
|
function weblateToCounterpart(inTrs) {
|
|
|
|
const outTrs = {};
|
|
|
|
|
|
|
|
for (const key of Object.keys(inTrs)) {
|
|
|
|
const keyParts = key.split('|', 2);
|
|
|
|
if (keyParts.length === 2) {
|
|
|
|
let obj = outTrs[keyParts[0]];
|
|
|
|
if (obj === undefined) {
|
|
|
|
obj = outTrs[keyParts[0]] = {};
|
|
|
|
} else if (typeof obj === "string") {
|
|
|
|
// This is a transitional edge case if a string went from singular to pluralised and both still remain
|
|
|
|
// in the translation json file. Use the singular translation as `other` and merge pluralisation atop.
|
|
|
|
obj = outTrs[keyParts[0]] = {
|
|
|
|
"other": inTrs[key],
|
|
|
|
};
|
|
|
|
console.warn("Found entry in i18n file in both singular and pluralised form", keyParts[0]);
|
|
|
|
}
|
|
|
|
obj[keyParts[1]] = inTrs[key];
|
|
|
|
} else {
|
|
|
|
outTrs[key] = inTrs[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outTrs;
|
|
|
|
}
|
|
|
|
|
2020-09-24 09:09:34 +00:00
|
|
|
// Mock the browser-request for the languageHandler tests to return
|
2022-01-05 10:37:28 +00:00
|
|
|
// Fake languages.json containing references to en_EN, de_DE and lv
|
2020-09-24 09:09:34 +00:00
|
|
|
// en_EN.json
|
|
|
|
// de_DE.json
|
2022-01-05 10:37:28 +00:00
|
|
|
// lv.json - mock version with few translations, used to test fallback translation
|
2019-12-17 11:24:37 +00:00
|
|
|
module.exports = jest.fn((opts, cb) => {
|
2020-01-16 14:01:15 +00:00
|
|
|
const url = opts.url || opts.uri;
|
|
|
|
if (url && url.endsWith("languages.json")) {
|
2022-05-17 16:44:29 +00:00
|
|
|
cb(undefined, { status: 200 }, JSON.stringify({
|
2019-12-17 11:24:37 +00:00
|
|
|
"en": {
|
|
|
|
"fileName": "en_EN.json",
|
|
|
|
"label": "English",
|
|
|
|
},
|
2020-09-15 12:24:20 +00:00
|
|
|
"de": {
|
|
|
|
"fileName": "de_DE.json",
|
|
|
|
"label": "German",
|
|
|
|
},
|
2022-01-05 10:37:28 +00:00
|
|
|
"lv": {
|
|
|
|
"fileName": "lv.json",
|
2022-05-17 16:44:29 +00:00
|
|
|
"label": "Latvian",
|
|
|
|
},
|
2019-12-17 11:24:37 +00:00
|
|
|
}));
|
2020-01-16 14:01:15 +00:00
|
|
|
} else if (url && url.endsWith("en_EN.json")) {
|
2022-05-17 16:44:29 +00:00
|
|
|
cb(undefined, { status: 200 }, JSON.stringify(weblateToCounterpart(en)));
|
2020-09-15 12:24:20 +00:00
|
|
|
} else if (url && url.endsWith("de_DE.json")) {
|
2022-05-17 16:44:29 +00:00
|
|
|
cb(undefined, { status: 200 }, JSON.stringify(weblateToCounterpart(de)));
|
2022-01-05 10:37:28 +00:00
|
|
|
} else if (url && url.endsWith("lv.json")) {
|
2022-05-17 16:44:29 +00:00
|
|
|
cb(undefined, { status: 200 }, JSON.stringify(weblateToCounterpart(lv)));
|
2019-12-17 11:24:37 +00:00
|
|
|
} else {
|
2022-05-17 16:44:29 +00:00
|
|
|
cb(true, { status: 404 }, "");
|
2019-12-17 11:24:37 +00:00
|
|
|
}
|
|
|
|
});
|