2020-03-04 16:42:44 +00:00
< html >
< head >
2020-06-01 16:04:55 +00:00
< meta charset = "UTF-8" >
2020-03-04 16:42:44 +00:00
< link rel = "stylesheet" href = "../../css/spectre.min.css" >
2023-09-11 13:54:18 +00:00
< style >
table { width:100%;}
.table_t {font-weight:bold;width:40%;};
< / style >
2020-03-04 16:42:44 +00:00
< / head >
< body >
< p > Please choose a language from the following list:< / p >
< div class = "form-group" >
< select id = "languages" class = "form-select" >
< / select >
< / div >
2022-01-15 19:09:58 +00:00
< div class = "form-group" >
2022-02-11 11:40:44 +00:00
< input id = "translations" type = "checkbox" / > < label for = "translations" > Add common language translations like "Yes", "No", "On", "Off"< br / > < i > (Not recommended. For translations use the option under < code > More...< / code > in the app loader.< / i > < / label >
2022-01-15 19:09:58 +00:00
< / div >
2023-09-11 13:54:18 +00:00
< p >
< table id = "examples" >
< / p >
< / table >
2020-03-04 16:42:44 +00:00
< p > Then click < button id = "upload" class = "btn btn-primary" > Upload< / button > < / p >
2020-08-24 10:59:52 +00:00
< script src = "../../core/lib/customize.js" > < / script >
2022-04-07 15:08:39 +00:00
< script src = "../../core/js/utils.js" > < / script >
2020-06-01 16:04:55 +00:00
< script src = "locales.js" charset = "utf-8" > < / script >
2020-03-04 16:42:44 +00:00
< script >
2020-04-02 13:16:54 +00:00
/*
2024-03-25 10:16:03 +00:00
eg. the built-in en_GB at https://github.com/espruino/Espruino/blob/master/libs/js/banglejs/locale.js is:
2020-04-02 13:16:54 +00:00
2024-03-25 10:16:03 +00:00
function round(n, dp) {
if (dp===undefined) dp=1;
var p = Math.min(dp,dp - Math.floor(Math.log(n)/Math.log(10)));
return n.toFixed(p);
}
exports = { name : "system", currencySym:"£",
2020-04-02 13:16:54 +00:00
translate : str=>str, // as-is
date : (d,short) => short?("0"+d.getDate()).substr(-2)+"/"+("0"+(d.getMonth()+1)).substr(-2)+"/"+d.getFullYear():d.toString().substr(4,11), // Date to "Feb 28 2020" or "28/02/2020"(short)
time : (d,short) => { // Date to "4:15.28 pm" or "15:42"(short)
2024-03-25 10:16:03 +00:00
var h = d.getHours(), m = d.getMinutes()
if ((require('Storage').readJSON('setting.json',1)||{})["12hour"])
h = (h%12==0) ? 12 : h%12; // 12 hour
2020-04-02 13:16:54 +00:00
if (short)
2024-03-25 10:16:03 +00:00
return (" "+h).substr(-2)+":"+("0"+m).substr(-2);
2020-04-02 13:16:54 +00:00
else {
2024-03-25 10:16:03 +00:00
var r = "am";
2020-04-02 13:16:54 +00:00
if (h==0) { h=12; }
else if (h>=12) {
if (h>12) h-=12;
r = "pm";
}
return (" "+h).substr(-2)+":"+("0"+m).substr(-2)+"."+("0"+d.getSeconds()).substr(-2)+" "+r;
}
},
2024-03-25 10:16:03 +00:00
dow : (d,short) => "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(",")[d.getDay()].substr(0, short ? 3 : 10), // Date to "Monday" or "Mon"(short)
month : (d,short) => "January,February,March,April,May,June,July,August,September,October,November,December".split(",")[d.getMonth()].substr(0, short ? 3 : 10), // Date to "February" or "Feb"(short)
number: (n, dec) => {
if (dec == null) dec = 2;
var w = n.toFixed(dec),
k = w|0,
b = n < 0 ? 1 : 0 ,
u = Math.abs(w-k),
d = (''+u.toFixed(dec)).substr(2, dec),
s = ''+k,
i = s.length,
r = '';
while ((i-=3) > b)
r = ',' + s.substr(i, 3) + r;
return s.substr(0, i + 3) + r + (d ? '.' + d: '');
},
currency : n => {console.log("Warning: Currency information is deprecated");return "£"+n.toFixed(2)}, // number to "£1.00"
distance : (m,dp) => (m< 1000 ) ? round ( m , dp ) + " m " :round ( m / 1000 , dp ) + " km " , / / meters to " 123m " or " 1 . 2km " depending on size
speed : (s,dp) => round(s/1.60934,dp)+"mph",// kph to "123mph"
temp : (t,dp) => round(t,dp)+"'C", // degrees C to degrees C
meridian: d => (d.getHours() < = 12) ? "am":"pm" // Date to am/pm
2020-04-02 13:16:54 +00:00
};
2024-03-25 10:16:03 +00:00
2020-04-02 13:16:54 +00:00
*/
2020-06-01 16:04:55 +00:00
function codePageLookup(lang, codePage, ch) {
var chCode = ch.charCodeAt();
if (chCode>=32 & & chCode< 128 ) return ch ; / / ASCII - copy it
// not normal ASCII
var n; // default is a space
if (codePage.map.indexOf(ch)>=0) // look up in char map, escape that
n = 128+codePage.map.indexOf(ch);
/*else if (chCode< 256 ) / / it ' s non-ascii , but < 256 - just escape it
n = chCode;*/
else {
2022-04-07 15:08:39 +00:00
if (CODEPAGE_CONVERSIONS[ch]) return CODEPAGE_CONVERSIONS[ch];
2020-06-01 16:04:55 +00:00
console.error(`Locale ${lang}: Character ${ch} (${chCode}) is not in Code Page ${codePage.name}`);
return undefined;
}
// escape the char
2020-06-02 19:21:27 +00:00
return '\\x'+(n+256).toString(16).slice(-2);
2020-06-01 16:04:55 +00:00
}
2020-04-02 13:16:54 +00:00
// do some sanity checks
Object.keys(locales).forEach(function(localeName) {
var locale = locales[localeName];
2020-04-09 07:57:06 +00:00
if (locale.trans & & !locale.trans.on) console.error(localeName+": If translations are provided, 'on' *must* be included");
2020-04-02 13:16:54 +00:00
if (distanceUnits[locale.distance[0]]===undefined) console.error(localeName+": Unknown distance unit "+locale.distance[0]);
if (distanceUnits[locale.distance[1]]===undefined) console.error(localeName+": Unknown distance unit "+locale.distance[1]);
if (speedUnits[locale.speed]===undefined) console.error(localeName+": Unknown speed unit "+locale.speed);
2020-06-01 16:04:55 +00:00
if (locale.temperature!='°C' & & locale.temperature!='°F')
console.error(localeName+": Unknown temperature unit "+locale.temperature);
// Now check that codepage is ok and all chars in translation are in that codepage
const codePageName = "ISO8859-1";
if (locale.codePage) codePageName = locale.codePage;
const codePage = codePages[codePageName];
if (codePage===undefined) console.error(localeName+": Unknown codePage "+codePageName);
function checkChars(v,path) {
if ("object"==typeof v)
Object.keys(v).forEach(k=>checkChars(v[k], path+"."+k));
else if ("string"==typeof v)
for (var i=0;i< v.length ; i + + )
if (codePageLookup(localeName, codePage, v[i])===undefined)
console.error(` ... in ${path}[${i}]`);
}
checkChars(locale,localeName);
2020-04-02 13:16:54 +00:00
});
2020-03-04 16:42:44 +00:00
2023-09-11 13:54:18 +00:00
function createLocaleModule(lang) {
2020-06-01 16:04:55 +00:00
console.log(`Language ${lang}`);
2020-03-04 16:42:44 +00:00
2022-01-15 19:09:58 +00:00
const translations = document.getElementById('translations').checked;
console.log(`Translations: ${translations}`);
2020-06-01 16:04:55 +00:00
const locale = locales[lang];
2020-03-04 16:42:44 +00:00
if (!locale) {
2020-06-01 16:04:55 +00:00
alert(`Language ${lang} not found!`);
2020-03-04 16:42:44 +00:00
return;
}
2022-01-15 19:09:58 +00:00
if (!translations)
locale.trans = null;
2020-06-01 16:04:55 +00:00
const codePageName = "ISO8859-1";
if (locale.codePage)
codePageName = locale.codePage;
const codePage = codePages[codePageName];
if (!codePage) {
alert(`Code Page ${codePageName} not found!`);
return;
}
2020-04-02 13:16:54 +00:00
2020-06-01 16:04:55 +00:00
// Convert object to JSON, with codepage
function js(x) {
// do nortmal conversion
x = JSON.stringify(x);
/* assume any out of bounds character is going to
be inside a quoted string */
var s = '';
for (var i=0;i< x.length ; i + + ) {
var ch = codePageLookup(lang, codePage, x[i]);
s += (ch===undefined) ? "?" : ch;
}
return s;
}
2020-04-02 13:16:54 +00:00
2020-06-02 21:00:32 +00:00
function unitConv(x) {
return x === 1 ? 'n' : 'n/' + x
}
2020-03-04 16:42:44 +00:00
var replaceList = {
2020-04-02 13:16:54 +00:00
"%Y": "d.getFullYear()",
2023-09-11 13:54:18 +00:00
"%y": "d.getFullYear().toString().slice(-2)",
2020-06-02 19:21:27 +00:00
"%m": "('0'+(d.getMonth()+1).toString()).slice(-2)",
2020-04-02 13:16:54 +00:00
"%-m": "d.getMonth()+1",
2020-06-02 19:21:27 +00:00
"%d": "('0'+d.getDate()).slice(-2)",
2020-04-02 13:16:54 +00:00
"%-d": "d.getDate()",
2023-05-31 00:00:26 +00:00
"%HH": "getHours(d)",
2020-06-02 19:21:27 +00:00
"%MM": "('0'+d.getMinutes()).slice(-2)",
"%SS": "('0'+d.getSeconds()).slice(-2)",
2022-03-30 14:11:14 +00:00
"%A": `${js(locale.day)}.split(',')[d.getDay()]`,
"%a": `${js(locale.abday)}.split(',')[d.getDay()]`,
"%B": `${js(locale.month)}.split(',')[d.getMonth()]`,
2022-03-31 10:39:59 +00:00
"%b": `${js(locale.abmonth)}.split(',')[d.getMonth()]`,
2020-06-02 21:04:22 +00:00
"%p": `d.getHours()< 12 ? $ { js ( locale . ampm [ 0 ] . toUpperCase ( ) ) } : $ { js ( locale . ampm [ 1 ] . toUpperCase ( ) ) } ` ,
"%P": `d.getHours()< 12 ? $ { js ( locale . ampm [ 0 ] . toLowerCase ( ) ) } : $ { js ( locale . ampm [ 1 ] . toLowerCase ( ) ) } `
2020-03-04 16:42:44 +00:00
};
2020-06-03 22:19:23 +00:00
var timeN = locale.timePattern[0];
var timeS = locale.timePattern[1];
var dateN = locale.datePattern[0];
var dateS = locale.datePattern[1];
2020-03-04 16:42:44 +00:00
Object.keys(replaceList).forEach(e => {
2020-04-02 13:16:54 +00:00
timeN = timeN.replace(e,"${"+replaceList[e]+"}");
timeS = timeS.replace(e,"${"+replaceList[e]+"}");
dateN = dateN.replace(e,"${"+replaceList[e]+"}");
dateS = dateS.replace(e,"${"+replaceList[e]+"}");
2020-03-04 16:42:44 +00:00
});
2020-06-02 11:06:19 +00:00
var temperature = locale.temperature=='°F' ? '(t*9/5)+32' : 't';
2020-03-04 16:42:44 +00:00
2023-09-11 13:54:18 +00:00
function getLocaleModule(isLocal) {
return `
2022-03-30 14:11:14 +00:00
function round(n, dp) {
if (dp===undefined) dp=0;
2023-09-11 13:54:18 +00:00
var p = Math.max(0,Math.min(dp,dp - Math.floor(Math.log(n)/Math.log(10))));
2022-03-30 14:11:14 +00:00
return n.toFixed(p);
2020-06-06 11:49:52 +00:00
}
2021-11-26 20:29:58 +00:00
var is12;
function getHours(d) {
var h = d.getHours();
2023-09-11 13:54:18 +00:00
if (is12 === undefined) is12 = ${isLocal ? "false" : `(require('Storage').readJSON('setting.json', 1) || {})["12hour"]`};
2023-05-31 06:44:26 +00:00
if (!is12) return ('0' + h).slice(-2);
return ((h % 12 == 0) ? 12 : h % 12).toString();
2021-11-26 20:29:58 +00:00
}
2020-06-02 11:10:35 +00:00
exports = {
2020-04-02 13:16:54 +00:00
name: ${js(locale.lang)},
2024-03-20 12:52:43 +00:00
currencySym: ${js("£")},
2022-04-26 09:31:29 +00:00
dow: (d,short) => ${js(locale.day + ',' + locale.abday)}.split(',')[d.getDay() + (short ? 7 : 0)],
2022-03-30 14:11:14 +00:00
month: (d,short) => ${js(locale.month + ',' + locale.abmonth)}.split(',')[d.getMonth() + (short ? 12 : 0)],
2020-06-06 20:26:07 +00:00
number: (n, dec) => {
if (dec == null) dec = 2;
var w = n.toFixed(dec),
k = w|0,
b = n < 0 ? 1 : 0 ,
u = Math.abs(w-k),
d = (''+u.toFixed(dec)).substr(2, dec),
s = ''+k,
i = s.length,
r = '';
2024-03-25 10:16:03 +00:00
while ((i-=3) > b)
2020-06-06 20:26:07 +00:00
r = '${locale.thousands_sep}' + s.substr(i, 3) + r;
return s.substr(0, i + 3) + r + (d ? '${locale.decimal_point}' + d: '');
},
2024-03-21 10:29:02 +00:00
currency: n => {console.log("Warning: Currency information is deprecated, see https://github.com/espruino/BangleApps/issues/3269");return ${js("£")}+exports.number(n)},
2022-03-30 14:11:14 +00:00
distance: (n,dp) => n < ${distanceUnits[locale.distance[1]]} ? round(${unitConv(distanceUnits[locale.distance[0]])},dp) + ${js(locale.distance[0])} : round(${unitConv(distanceUnits[locale.distance[1]])},dp) + ${js(locale.distance[1])},
speed: (n,dp) => round(${unitConv(speedUnits[locale.speed])},dp) + ${js(locale.speed)},
temp: (t,dp) => round(${temperature},dp) + ${js(locale.temperature)},
2020-06-02 11:10:35 +00:00
translate: s => ${locale.trans?`{var t=${js(locale.trans)};s=''+s;return t[s]||t[s.toLowerCase()]||s;}`:`s`},
2020-06-02 21:00:02 +00:00
date: (d,short) => short ? \`${dateS}\` : \`${dateN}\`,
time: (d,short) => short ? \`${timeS}\` : \`${timeN}\`,
2020-06-02 21:04:22 +00:00
meridian: d => d.getHours() < 12 ? $ { js ( locale . ampm [ 0 ] ) } : $ { js ( locale . ampm [ 1 ] ) } ,
2020-06-02 20:59:38 +00:00
};
2023-09-11 13:54:18 +00:00
`.trim()
};
var exports;
eval(getLocaleModule(true));
console.log("exports:",exports);
var date = new Date();
document.getElementById("examples").innerHTML = `
< tr > < td class = "table_t" > < / td > < td style = "font-weight:bold" > Short< / td > < td style = "font-weight:bold" > Long< / td > < / tr >
< tr > < td class = "table_t" > Day< / td > < td > ${exports.dow(date,1)}< / td > < td > ${exports.dow(date,0)}< / td > < / tr >
< tr > < td class = "table_t" > Month< / td > < td > ${exports.month(date,1)}< / td > < td > ${exports.month(date,0)}< / td > < / tr >
< tr > < td class = "table_t" > Date< / td > < td > ${exports.date(date,1)}< / td > < td > ${exports.date(date,0)}< / td > < / tr >
< tr > < td class = "table_t" > Time< / td > < td > ${exports.time(date,1)}< / td > < td > ${exports.time(date,0)}< / td > < / tr >
< tr > < td class = "table_t" > Number< / td > < td > ${exports.number(12.3456789)}< / td > < td > ${exports.number(12.3456789,4)}< / td > < / tr >
< tr > < td class = "table_t" > Distance< / td > < td > ${exports.distance(12.34,0)}< / td > < td > ${exports.distance(12345.6,1)}< / td > < / tr >
< tr > < td class = "table_t" > Speed< / td > < td > < / td > < td > ${exports.speed(123)}< / td > < / tr >
< tr > < td class = "table_t" > Temperature< / td > < td > < / td > < td > ${exports.temp(12,0)}< / td > < / tr >
`;
return getLocaleModule(false);
}
var languageSelector = document.getElementById("languages");
languageSelector.innerHTML = Object.keys(locales).map(l=>{
var locale = locales[l];
var localeParts = l.split("_"); // en_GB -> ["en","GB"]
var icon = "";
// If we have a 2 char ISO country code, use it to get the unicode flag
if (locale.icon)
icon = locale.icon+" ";
else if (localeParts[1] & & localeParts[1].length==2)
icon = localeParts[1].toUpperCase().replace(/./g, char => String.fromCodePoint(char.charCodeAt(0)+127397) )+" ";
return `< option value = "${l}" > ${icon}${l}${locale.notes?" - "+locale.notes:""}< / option > `
}).join("\n");
languageSelector.addEventListener('change', function() {
const lang = languageSelector.options[languageSelector.selectedIndex].value;
createLocaleModule(lang);
});
// initial value
createLocaleModule(languageSelector.options[languageSelector.selectedIndex].value);
document.getElementById("upload").addEventListener("click", function() {
const lang = languageSelector.options[languageSelector.selectedIndex].value;
var localeModule = createLocaleModule(lang);
2020-03-04 16:42:44 +00:00
2020-06-06 20:26:07 +00:00
console.log("Locale Module is:",localeModule);
2020-03-04 16:42:44 +00:00
sendCustomizedApp({
storage:[
2020-09-01 13:43:54 +00:00
{name:"locale", url:"locale.js", content:localeModule}
2020-03-04 16:42:44 +00:00
]
});
});
< / script >
< / body >
< / html >