1
function translate(msg) {
4
if (!arguments || arguments.length < 2 || !RegExp) {
8
var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
9
var a = b = [], numSubstitutions = 0, numMatches = 0;
10
while (a = re.exec(str))
12
var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
13
var pPrecision = a[5], pType = a[6], rightPart = a[7];
23
if (numSubstitutions >= arguments.length)
25
alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
27
var param = arguments[numSubstitutions];
29
if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
30
else if (pPad) pad = pPad;
31
var justifyRight = true;
32
if (pJustify && pJustify === "-") justifyRight = false;
34
if (pMinLength) minLength = parseInt(pMinLength);
36
if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
38
if (pType == 'b') subst = parseInt(param).toString(2);
39
else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
40
else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
41
else if (pType == 'u') subst = Math.abs(param);
42
else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
43
else if (pType == 'o') subst = parseInt(param).toString(8);
44
else if (pType == 's') subst = param;
45
else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
46
else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
48
str = leftpart + subst + rightPart;
53
// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
56
if (!arguments || arguments.length < 1 || !RegExp)
60
var str = arguments[0];
61
var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
62
var a = b = [], numSubstitutions = 0, numMatches = 0;
63
while (a = re.exec(str))
65
var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
66
var pPrecision = a[5], pType = a[6], rightPart = a[7];
68
//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);
78
if (numSubstitutions >= arguments.length)
80
alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
82
var param = arguments[numSubstitutions];
84
if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
85
else if (pPad) pad = pPad;
86
var justifyRight = true;
87
if (pJustify && pJustify === "-") justifyRight = false;
89
if (pMinLength) minLength = parseInt(pMinLength);
91
if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
93
if (pType == 'b') subst = parseInt(param).toString(2);
94
else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
95
else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
96
else if (pType == 'u') subst = Math.abs(param);
97
else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
98
else if (pType == 'o') subst = parseInt(param).toString(8);
99
else if (pType == 's') subst = param;
100
else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
101
else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
103
str = leftpart + subst + rightPart;