Use default translation if any placeholder is missing in the translation string

This commit is contained in:
schlagmichdoch 2023-12-05 18:51:56 +01:00
parent 29bede6109
commit 25d6595a8f
2 changed files with 30 additions and 7 deletions

View file

@ -118,7 +118,7 @@ class Localization {
}
}
static getTranslation(key, attr=null, data={}, useDefault=false) {
static getTranslation(key, attr = null, data = {}, useDefault = false) {
const keys = key.split(".");
let translationCandidates = useDefault
@ -139,27 +139,45 @@ class Localization {
translation = translationCandidates[lastKey];
for (let j in data) {
translation = translation.replace(`{{${j}}}`, data[j]);
if (translation.includes(`{{${j}}}`)) {
translation = translation.replace(`{{${j}}}`, data[j]);
} else {
console.warn(`Translation for your language ${Localization.locale.toUpperCase()} misses at least one data placeholder:`, key, attr, data);
Localization.logHelpCallKey(key);
Localization.logHelpCall();
translation = "";
break;
}
}
} catch (e) {
console.error(e);
translation = "";
}
if (!translation) {
if (!useDefault) {
translation = this.getTranslation(key, attr, data, true);
console.warn(`Missing translation entry for your language ${Localization.locale.toUpperCase()}. Using ${Localization.defaultLocale.toUpperCase()} instead.`, key, attr);
console.warn(`Translate this string here: https://hosted.weblate.org/browse/pairdrop/pairdrop-spa/${Localization.locale.toLowerCase()}/?q=${key}`)
console.log("Help translating PairDrop: https://hosted.weblate.org/engage/pairdrop/");
Localization.logHelpCallKey(key);
Localization.logHelpCall();
translation = this.getTranslation(key, attr, data, true);
}
else {
console.warn("Missing translation in default language:", key, attr);
Localization.logHelpCall();
}
}
return Localization.escapeHTML(translation);
}
static logHelpCall() {
console.log("Help translating PairDrop: https://hosted.weblate.org/engage/pairdrop/");
}
static logHelpCallKey(key) {
console.warn(`Translate this string here: https://hosted.weblate.org/browse/pairdrop/pairdrop-spa/${Localization.locale.toLowerCase()}/?q=${key}`);
}
static escapeHTML(unsafeText) {
let div = document.createElement('div');
div.innerText = unsafeText;

View file

@ -235,6 +235,7 @@ class PeersUI {
Events.on('share-mode-pointerdown', this._activateCallback);
const sharedText = Localization.getTranslation("instructions.activate-share-mode-shared-text");
const andOtherFilesPlural = Localization.getTranslation("instructions.activate-share-mode-and-other-files-plural", null, {count: files.length-1});
const andOtherFiles = Localization.getTranslation("instructions.activate-share-mode-and-other-file");
@ -257,7 +258,7 @@ class PeersUI {
else {
// text shared
descriptorItem = text.replace(/\s/g," ");
descriptorComplete = descriptorItem;
descriptorComplete = sharedText;
}
if (files.length > 0) {
@ -308,6 +309,7 @@ class PeersUI {
this.$shareModeDescriptorItem.innerText = descriptorItem;
this.shareMode.active = true;
this.shareMode.descriptor = descriptorComplete;
this.shareMode.files = files;
this.shareMode.text = text;
@ -334,6 +336,7 @@ class PeersUI {
if (!this.shareMode.active) return;
this.shareMode.active = false;
this.shareMode.descriptor = "";
this.shareMode.files = [];
this.shareMode.text = "";
@ -412,7 +415,9 @@ class PeerUI {
}
html() {
let title = Localization.getTranslation("peer-ui.click-to-send", null, {descriptor: PeerUI._shareMode.descriptor});
let title= PeerUI._shareMode.active
? Localization.getTranslation("peer-ui.click-to-send-share-mode", null, {descriptor: PeerUI._shareMode.descriptor})
: Localization.getTranslation("peer-ui.click-to-send");
this.$el.innerHTML = `
<label class="column center pointer" title="${title}">