forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalization.ts
More file actions
84 lines (76 loc) · 3.13 KB
/
Copy pathlocalization.ts
File metadata and controls
84 lines (76 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Localization functions. Please port any modifications over to pxtlib/commonutil.ts
namespace pxsim.localization {
let _localizeStrings: Map<string> = {};
export function setLocalizedStrings(strs: Map<string>) {
_localizeStrings = strs || {};
}
let sForPlural = true;
export function lf(s: string, ...args: any[]): string { // @ignorelf@
let lfmt = _localizeStrings[s] || s;
if (!sForPlural && lfmt != s && /\d:s\}/.test(lfmt)) {
lfmt = lfmt.replace(/\{\d+:s\}/g, "")
}
lfmt = lfmt.replace(/^\{(id|loc):[^\}]+\}/g, '');
return fmt_va(lfmt, args);
}
// from pxtlib/commonutil.ts
export function fmt_va(f: string, args: any[]): string {
if (args.length == 0) return f;
return f.replace(/\{([0-9]+)(\:[^\}]+)?\}/g, function (s: string, n: string, spec: string): string {
let v = args[parseInt(n)];
let r = "";
let fmtMatch = /^:f(\d*)\.(\d+)/.exec(spec);
if (fmtMatch) {
let precision = parseInt(fmtMatch[2])
let len = parseInt(fmtMatch[1]) || 0
let fillChar = /^0/.test(fmtMatch[1]) ? "0" : " ";
let num = (<number>v).toFixed(precision)
if (len > 0 && precision > 0) len += precision + 1;
if (len > 0) {
while (num.length < len) {
num = fillChar + num;
}
}
r = num;
} else if (spec == ":x") {
r = "0x" + v.toString(16);
} else if (v === undefined) r = "(undef)";
else if (v === null) r = "(null)";
else if (v.toString) r = v.toString();
else r = v + "";
if (spec == ":a") {
if (/^\s*[euioah]/.test(r.toLowerCase()))
r = "an " + r;
else if (/^\s*[bcdfgjklmnpqrstvwxz]/.test(r.toLowerCase()))
r = "a " + r;
} else if (spec == ":s") {
if (v == 1) r = ""
else r = "s"
} else if (spec == ":q") {
r = htmlEscape(r);
} else if (spec == ":jq") {
r = jsStringQuote(r);
} else if (spec == ":uri") {
r = encodeURIComponent(r).replace(/'/g, "%27").replace(/"/g, "%22");
} else if (spec == ":url") {
r = encodeURI(r).replace(/'/g, "%27").replace(/"/g, "%22");
} else if (spec == ":%") {
r = (v * 100).toFixed(1).toString() + '%';
}
return r;
});
}
// from pxtlib/commonutil.ts
export function htmlEscape(_input: string) {
if (!_input) return _input; // null, undefined, empty string test
return _input.replace(/([^\w .!?\-$])/g, c => "&#" + c.charCodeAt(0) + ";");
}
// from pxtlib/commonutil.ts
export function jsStringQuote(s: string) {
return s.replace(/[^\w .!?\-$]/g,
(c) => {
let h = c.charCodeAt(0).toString(16);
return "\\u" + "0000".substr(0, 4 - h.length) + h;
});
}
}