Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internationalization of alarm app #202

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
i18n also alarm.js
  • Loading branch information
DerGuteWolf committed Apr 21, 2020
commit 470f1e64df16ffd8874d2567522df999e43a8c49
37 changes: 21 additions & 16 deletions apps/alarm/alarm.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,34 @@
xhr.send();
});

let appjsTranslated;
let translate = () => {
let translated; = {};
let translate = () => {
Promise.all(translateFile("app.js"), translateFile("alarm.js")).then(() => {
document.getElementById("upload").style.visibility = 'visible';
}).catch( error => {
console.log(error);
document.getElementById("upload").style.visibility = 'hidden';
});
};
let translateFile = (fileName) => {
var languageSelector = document.getElementById("languages");
var lang = languageSelector.options[languageSelector.selectedIndex].value;
console.log(lang);
request("app.js").then( appjs =>
request(fileName).then( fileContent =>
request("app_" + lang + ".json")
.catch( () => request("app_" + lang.substring(0,2) + ".json"))
.catch( () => request("app.json"))
.then( langjson => {
var trans = JSON.parse(langjson);
appjs = appjs.replace(/"([^"]*?)"\/\*LANG\*\//g, function(m, p1) { return '"' + (trans[p1]||p1) + '"' + (trans[p1]?'':'/*LANG*/'); });
appjs = appjs.replace(/'([^']*?)'\/\*LANG\*\//g, function(m, p1) { return "'" + (trans[p1]||p1) + "'" + (trans[p1]?'':'/*LANG*/'); });
return appjs;
fileContent = fileContent.replace(/"([^"]*?)"\/\*LANG\*\//g, function(m, p1) { return '"' + (trans[p1]||p1) + '"' + (trans[p1]?'':'/*LANG*/'); });
fileContent = fileContent.replace(/'([^']*?)'\/\*LANG\*\//g, function(m, p1) { return "'" + (trans[p1]||p1) + "'" + (trans[p1]?'':'/*LANG*/'); });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clever, but I think it would be better to just use a function in the callsite, something like https://www.i18next.com/translation-function/essentials#passing-a-default-value (and many others) use:

locale.t('new', 'New Alarm')

Using the English (here) as the key is not good, if the English text “New Alarm” changes, then you can't reuse the translations properly.

Actually, there's already a locale.translate function: https://www.espruino.com/Bangle.js+Locale#translation
but it uses the English text, and doesn't support Application resources.

Needs some work!

return fileContent;
}).catch( error => {
console.log(error);
return appjs; // unmodified
return fileContent; // unmodified
})
).then( appjs => {
appjsTranslated = appjs;
document.getElementById("upload").style.visibility = 'visible';
}).catch( error => {
console.log(error);
document.getElementById("upload").style.visibility = 'hidden';
).then( fileContent => {
translated[fileName] = fileContent;
});
};

Expand Down Expand Up @@ -82,12 +86,13 @@
});

document.getElementById("upload").addEventListener("click", function() {
console.log("app.js is:", appjsTranslated);
console.log("app.js is:", translated["app.js"];
console.log("alarm.js is:", translated["alarm.js"];
sendCustomizedApp({
storage:[
{"name":"alarm.app.js",content: appjsTranslated},
{"name":"alarm.app.js",content: translated["app.js"]},
{"name":"alarm.boot.js","url":"boot.js"},
{"name": "alarm.js", "url":"alarm.js"},
{"name": "alarm.js", content: translated["alarm.js"]},
//{"name":"alarm.json","content":"[]"},
{"name":"alarm.img","url":"app-icon.js","evaluate":true},
{"name":"alarm.wid.js","url":"widget.js"}
Expand Down
10 changes: 7 additions & 3 deletions apps/alarm/alarm.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const locale = require("locale");
// Chances are boot0.js got run already and scheduled *another*
// 'load(alarm.js)' - so let's remove it first!
clearInterval();

function formatTime(t) {
var hrs = 0|t;
var mins = Math.round((t-hrs)*60);
return hrs+":"+("0"+mins).substr(-2);
var d = new Date();
d.setHours(hrs);
d.setMinutes(mins);
return locale.time(d, true);
}

function getCurrentHr() {
Expand All @@ -19,8 +23,8 @@ function showAlarm(alarm) {
if (alarm.msg)
msg += "\n"+alarm.msg;
E.showPrompt(msg,{
title:"ALARM!",
buttons : {"Sleep":true,"Ok":false} // default is sleep so it'll come back in 10 mins
title:"ALARM!"/*LANG*/,
buttons : {"Sleep"/*LANG*/:true,locale.translate("Ok"):false} // default is sleep so it'll come back in 10 mins
}).then(function(sleep) {
buzzCount = 0;
if (sleep) {
Expand Down
4 changes: 2 additions & 2 deletions apps/alarm/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function showMainMenu() {
'New Alarm'/*LANG*/: ()=>editAlarm(-1)
};
alarms.forEach((alarm,idx)=>{
txt = alarm.on?locale.translate("on"):locale.translate("off");
txt += " ".repeat(Math.max(locale.translate("on").length,locale.translate("off").length) - txt.length);
txt = alarm.on?locale.translate("On"):locale.translate("Off");
txt += " ".repeat(Math.max(locale.translate("On").length,locale.translate("Off").length) - txt.length);
txt += " " + formatTime(alarm.hr);
if (alarm.rp) txt += " (" + "rpt"/*LANG*/ + ")";
menu[txt] = function() {
Expand Down
6 changes: 4 additions & 2 deletions apps/alarm/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save":"Save",
"Back":"Back",
"Repeat":"Repeat",
"repeat":"repeat",
"rpt":"rpt"
"Delete":"Delete",
"rpt":"rpt",
"ALARM!":"ALARM!",
"Sleep":"Sleep"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Speichern",
"Back" : "Zurück",
"Repeat" : "Wiederholen",
"repeat" : "Wiederholen",
"rpt" : "Wdh."
"Delete" : "Löschen",
"rpt" : "Wdh.",
"ALARM!" : "ALARM!",
"Sleep" : "Schlummern"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_en_GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Save",
"Back" : "Back",
"Repeat" : "Repeat",
"repeat" : "repeat",
"rpt" : "repeat"
"Delete" : "Delete",
"rpt" : "repeat",
"ALARM!" : "ALARM!",
"Sleep" : "Sleep"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_es.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Grabar",
"Back" : "Atrás",
"Repeat" : "Repetición",
"repeat" : "repetición",
"rpt" : "rep."
"Delete" : "Borrar",
"rpt" : "rep.",
"ALARM!" : "ALARM",
"Sleep" : "Dormir"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Tallenna",
"Back" : "Paluu",
"Repeat" : "Toista",
"repeat" : "toistaa",
"rpt" : "toistaa"
"Delete" : "Poista",
"rpt" : "toistaa",
"ALARM!" : "ALARM",
"Sleep" : "Nukkuminen"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Sauvegarder",
"Back" : "Retour",
"Repeat" : "Répétition",
"repeat" : "répétition",
"rpt" : "rép."
"Delete" : "Supprimer",
"rpt" : "rép.",
"ALARM!" : "ALARM!",
"Sleep" : "Sommeil"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Mentés",
"Back" : "Vissza",
"Repeat" : "Ismétlés",
"repeat" : "ismétlés",
"rpt" : "ismétlés"
"Delete" : "Törlés",
"rpt" : "ismétlés",
"ALARM!" : "ALARM!",
"Sleep" : "Alvás"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_it.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Salvare",
"Back" : "Indietro",
"Repeat" : "Ripetere",
"repeat" : "ripetere",
"rpt" : "ripetere"
"Delete" : "Cancellare",
"rpt" : "ripetere",
"ALARM!" : "ALARM!",
"Sleep" : "Dormire"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Opslaan",
"Back" : "Terug",
"Repeat" : "Herhalen",
"repeat" : "herhalen",
"rpt" : "herhalen"
"Delete" : "Verwijderen",
"rpt" : "herhalen",
"ALARM!" : "ALARV.",
"Sleep" : "Stand-by"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Spara",
"Back" : "Tillbaka",
"Repeat" : "Upprepning",
"repeat" : "upprepning",
"rpt" : "uppr."
"Delete" : "Radera",
"rpt" : "uppr.",
"ALARM!" : "ALURH!",
"Sleep" : "Sömn"
}
6 changes: 4 additions & 2 deletions apps/alarm/app_tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Save" : "Sakla",
"Back" : "Geriye",
"Repeat" : "Yineleme",
"repeat" : "yineleme",
"rpt" : "yineleme"
"Delete" : "Sil",
"rpt" : "yineleme",
"ALARM!" : "ALARM!",
"Sleep" : "Uyku"
}