Skip to content

Commit

Permalink
include translations for about buttons and implement translation fall…
Browse files Browse the repository at this point in the history
…back if used translation is not complete
  • Loading branch information
schlagmichdoch committed Jul 7, 2023
1 parent f50d743 commit ba46bef
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 36 deletions.
14 changes: 7 additions & 7 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<use xlink:href="#edit-pair-devices-icon" />
</svg>
</div>
<div id="cancel-paste-mode" class="button" data-i18n-key="header.done" data-i18n-attrs="text" hidden>Done</div>
<div id="cancel-paste-mode" class="button" data-i18n-key="header.cancel-paste-mode" data-i18n-attrs="text" hidden>Done</div>
</header>
<!-- Center -->
<div id="center">
Expand Down Expand Up @@ -161,7 +161,7 @@ <h1 id="room-key" class="center">000 000</h1>
<x-background class="full center text-center">
<x-paper shadow="2">
<h2 class="center" data-i18n-key="dialogs.edit-paired-devices-title" data-i18n-attrs="text">Edit Paired Devices</h2>
<div class="paired-devices-wrapper" data-i18n-key="dialogs.paired-devices-empty" data-i18n-attrs="data-empty" data-empty="No paired devices."></div>
<div class="paired-devices-wrapper" data-i18n-key="dialogs.paired-devices-wrapper" data-i18n-attrs="data-empty" data-empty="No paired devices."></div>
<div class="font-subheading center">
<p>
<span data-i18n-key="dialogs.auto-accept-instructions-1" data-i18n-attrs="text">
Expand Down Expand Up @@ -287,7 +287,7 @@ <h2 class="text-center" data-i18n-key="dialogs.receive-text-title" data-i18n-att
<!-- About Page -->
<x-about id="about" class="full center column">
<header class="row-reverse fade-in">
<a href="#" class="close icon-button" data-i18n-key="about.close-about" data-i18n-attrs="text" aria-label="Close About PairDrop">
<a href="#" class="close icon-button" data-i18n-key="about.close-about" data-i18n-attrs="aria-label" aria-label="Close About PairDrop">
<svg class="icon">
<use xlink:href="#close-icon" />
</svg>
Expand All @@ -303,22 +303,22 @@ <h1>PairDrop</h1>
</div>
<div class="font-subheading" data-i18n-key="about.claim" data-i18n-attrs="text">The easiest way to transfer files across devices</div>
<div class="row">
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop" title="PairDrop on Github" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop" title="PairDrop on Github" rel="noreferrer" data-i18n-key="about.github" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#github" />
</svg>
</a>
<a class="icon-button" target="_blank" href="https://www.buymeacoffee.com/pairdrop" title="Buy me a coffee!" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://www.buymeacoffee.com/pairdrop" title="Buy me a coffee!" rel="noreferrer" data-i18n-key="about.buy-me-a-coffee" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#monetarization" />
</svg>
</a>
<a class="icon-button" target="_blank" href="https://twitter.com/intent/tweet?text=https%3A%2F%2Fpairdrop.net%20by%20https%3A%2F%2Fgithub.com%2Fschlagmichdoch%2F&" title="Tweet about PairDrop" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://twitter.com/intent/tweet?text=https%3A%2F%2Fpairdrop.net%20by%20https%3A%2F%2Fgithub.com%2Fschlagmichdoch%2F&" title="Tweet about PairDrop" rel="noreferrer" data-i18n-key="about.tweet" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#twitter" />
</svg>
</a>
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop/blob/master/docs/faq.md" title="Frequently asked questions" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop/blob/master/docs/faq.md" title="Frequently asked questions" rel="noreferrer" data-i18n-key="about.faq" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#help-outline" />
</svg>
Expand Down
8 changes: 6 additions & 2 deletions public/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@
"download-again": "Download again"
},
"about": {
"close-about-aria-label": "Close About PairDrop",
"claim": "The easiest way to transfer files across devices"
"close-about_aria-label": "Close About PairDrop",
"claim": "The easiest way to transfer files across devices",
"github_title": "PairDrop on Github",
"buy-me-a-coffee_title": "Buy me a coffee!",
"tweet_title": "Tweet about PairDrop",
"faq_title": "Frequently asked questions"
},
"notifications": {
"display-name-changed-permanently": "Display name is changed permanently.",
Expand Down
30 changes: 21 additions & 9 deletions public/scripts/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Localization {
constructor() {
Localization.defaultLocale = "en";
Localization.supportedLocales = ["en"];

Localization.translations = {};
Localization.defaultTranslations = {};

const initialLocale = Localization.supportedOrDefault(Localization.browserLocales());

Expand All @@ -29,13 +29,13 @@ class Localization {

static async setLocale(newLocale) {
if (newLocale === Localization.locale) return false;
const firstTranslation = !Localization.locale

Localization.defaultTranslations = await Localization.fetchTranslationsFor(Localization.defaultLocale);
const newTranslations = await Localization.fetchTranslationsFor(newLocale);

if(!newTranslations) return false;

const firstTranslation = !Localization.locale

Localization.locale = newLocale;
Localization.translations = newTranslations;

Expand Down Expand Up @@ -65,18 +65,20 @@ class Localization {
for (let i in attrs) {
let attr = attrs[i];
if (attr === "text") {
element.innerText = await Localization.getTranslation(key);
element.innerText = Localization.getTranslation(key);
} else {
element.attr = await Localization.getTranslation(key, attr);
element.attr = Localization.getTranslation(key, attr);
}
}

}

static getTranslation(key, attr, data) {
static getTranslation(key, attr, data, useDefault=false) {
const keys = key.split(".");

let translationCandidates = Localization.translations;
let translationCandidates = useDefault
? Localization.defaultTranslations
: Localization.translations;

for (let i=0; i<keys.length-1; i++) {
translationCandidates = translationCandidates[keys[i]]
Expand All @@ -87,8 +89,18 @@ class Localization {

let translation = translationCandidates[lastKey];

for (key in data) {
translation = translation.replace(`{{${key}}}`, data[key]);
for (let j in data) {
translation = translation.replace(`{{${j}}}`, data[j]);
}

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("Help translating PairDrop: https://hosted.weblate.org/projects/pairdrop/pairdrop-spa/");
} else {
console.warn("Missing translation in default language:", key, attr);
}
}

return Localization.escapeHTML(translation);
Expand Down
14 changes: 7 additions & 7 deletions public_included_ws_fallback/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<use xlink:href="#edit-pair-devices-icon" />
</svg>
</div>
<div id="cancel-paste-mode" class="button" data-i18n-key="header.done" data-i18n-attrs="text" hidden>Done</div>
<div id="cancel-paste-mode" class="button" data-i18n-key="header.cancel-paste-mode" data-i18n-attrs="text" hidden>Done</div>
</header>
<!-- Center -->
<div id="center">
Expand Down Expand Up @@ -166,7 +166,7 @@ <h1 id="room-key" class="center">000 000</h1>
<x-background class="full center text-center">
<x-paper shadow="2">
<h2 class="center" data-i18n-key="dialogs.edit-paired-devices-title" data-i18n-attrs="text">Edit Paired Devices</h2>
<div class="paired-devices-wrapper" data-i18n-key="dialogs.paired-devices-empty" data-i18n-attrs="data-empty" data-empty="No paired devices."></div>
<div class="paired-devices-wrapper" data-i18n-key="dialogs.paired-devices-wrapper" data-i18n-attrs="data-empty" data-empty="No paired devices."></div>
<div class="font-subheading center">
<p>
<span data-i18n-key="dialogs.auto-accept-instructions-1" data-i18n-attrs="text">
Expand Down Expand Up @@ -292,7 +292,7 @@ <h2 class="text-center" data-i18n-key="dialogs.receive-text-title" data-i18n-att
<!-- About Page -->
<x-about id="about" class="full center column">
<header class="row-reverse fade-in">
<a href="#" class="close icon-button" data-i18n-key="about.close-about" data-i18n-attrs="text" aria-label="Close About PairDrop">
<a href="#" class="close icon-button" data-i18n-key="about.close-about" data-i18n-attrs="aria-label" aria-label="Close About PairDrop">
<svg class="icon">
<use xlink:href="#close-icon" />
</svg>
Expand All @@ -308,22 +308,22 @@ <h1>PairDrop</h1>
</div>
<div class="font-subheading" data-i18n-key="about.claim" data-i18n-attrs="text">The easiest way to transfer files across devices</div>
<div class="row">
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop" title="PairDrop on Github" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop" title="PairDrop on Github" rel="noreferrer" data-i18n-key="about.github" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#github" />
</svg>
</a>
<a class="icon-button" target="_blank" href="https://www.buymeacoffee.com/pairdrop" title="Buy me a coffee!" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://www.buymeacoffee.com/pairdrop" title="Buy me a coffee!" rel="noreferrer" data-i18n-key="about.buy-me-a-coffee" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#monetarization" />
</svg>
</a>
<a class="icon-button" target="_blank" href="https://twitter.com/intent/tweet?text=https%3A%2F%2Fpairdrop.net%20by%20https%3A%2F%2Fgithub.com%2Fschlagmichdoch%2F&" title="Tweet about PairDrop" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://twitter.com/intent/tweet?text=https%3A%2F%2Fpairdrop.net%20by%20https%3A%2F%2Fgithub.com%2Fschlagmichdoch%2F&" title="Tweet about PairDrop" rel="noreferrer" data-i18n-key="about.tweet" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#twitter" />
</svg>
</a>
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop/blob/master/docs/faq.md" title="Frequently asked questions" rel="noreferrer">
<a class="icon-button" target="_blank" href="https://github.com/schlagmichdoch/pairdrop/blob/master/docs/faq.md" title="Frequently asked questions" rel="noreferrer" data-i18n-key="about.faq" data-i18n-attrs="title">
<svg class="icon">
<use xlink:href="#help-outline" />
</svg>
Expand Down
8 changes: 6 additions & 2 deletions public_included_ws_fallback/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@
"download-again": "Download again"
},
"about": {
"close-about-aria-label": "Close About PairDrop",
"claim": "The easiest way to transfer files across devices"
"close-about_aria-label": "Close About PairDrop",
"claim": "The easiest way to transfer files across devices",
"github_title": "PairDrop on Github",
"buy-me-a-coffee_title": "Buy me a coffee!",
"tweet_title": "Tweet about PairDrop",
"faq_title": "Frequently asked questions"
},
"notifications": {
"display-name-changed-permanently": "Display name is changed permanently.",
Expand Down
30 changes: 21 additions & 9 deletions public_included_ws_fallback/scripts/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Localization {
constructor() {
Localization.defaultLocale = "en";
Localization.supportedLocales = ["en"];

Localization.translations = {};
Localization.defaultTranslations = {};

const initialLocale = Localization.supportedOrDefault(Localization.browserLocales());

Expand All @@ -29,13 +29,13 @@ class Localization {

static async setLocale(newLocale) {
if (newLocale === Localization.locale) return false;
const firstTranslation = !Localization.locale

Localization.defaultTranslations = await Localization.fetchTranslationsFor(Localization.defaultLocale);
const newTranslations = await Localization.fetchTranslationsFor(newLocale);

if(!newTranslations) return false;

const firstTranslation = !Localization.locale

Localization.locale = newLocale;
Localization.translations = newTranslations;

Expand Down Expand Up @@ -65,18 +65,20 @@ class Localization {
for (let i in attrs) {
let attr = attrs[i];
if (attr === "text") {
element.innerText = await Localization.getTranslation(key);
element.innerText = Localization.getTranslation(key);
} else {
element.attr = await Localization.getTranslation(key, attr);
element.attr = Localization.getTranslation(key, attr);
}
}

}

static getTranslation(key, attr, data) {
static getTranslation(key, attr, data, useDefault=false) {
const keys = key.split(".");

let translationCandidates = Localization.translations;
let translationCandidates = useDefault
? Localization.defaultTranslations
: Localization.translations;

for (let i=0; i<keys.length-1; i++) {
translationCandidates = translationCandidates[keys[i]]
Expand All @@ -87,8 +89,18 @@ class Localization {

let translation = translationCandidates[lastKey];

for (key in data) {
translation = translation.replace(`{{${key}}}`, data[key]);
for (let j in data) {
translation = translation.replace(`{{${j}}}`, data[j]);
}

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("Help translating PairDrop: https://hosted.weblate.org/projects/pairdrop/pairdrop-spa/");
} else {
console.warn("Missing translation in default language:", key, attr);
}
}

return Localization.escapeHTML(translation);
Expand Down

0 comments on commit ba46bef

Please sign in to comment.