/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to js/strings.js

  • Committer: Suren A. Chilingaryan
  • Date: 2008-10-30 01:27:40 UTC
  • mto: This revision was merged to the branch mainline in revision 116.
  • Revision ID: csa@dside.dyndns.org-20081030012740-svt0yigxj5uzwxyj
Few steps on source tree integration in javascript frontend

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function translate(msg) {
 
2
    var str = msg;
 
3
 
 
4
    if (!arguments || arguments.length < 2 || !RegExp) {
 
5
        return str;
 
6
    }
 
7
    
 
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))
 
11
    {
 
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];
 
14
 
 
15
        numMatches++;
 
16
        if (pType == '%')
 
17
        {
 
18
            subst = '%';
 
19
        }
 
20
        else
 
21
        {
 
22
            numSubstitutions++;
 
23
            if (numSubstitutions >= arguments.length)
 
24
            {
 
25
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
 
26
            }
 
27
            var param = arguments[numSubstitutions];
 
28
            var pad = '';
 
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;
 
33
            var minLength = -1;
 
34
            if (pMinLength) minLength = parseInt(pMinLength);
 
35
            var precision = -1;
 
36
            if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
 
37
            var subst = param;
 
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();
 
47
        }
 
48
        str = leftpart + subst + rightPart;
 
49
    }
 
50
    return str;
 
51
}
 
52
 
 
53
// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
 
54
function sprintf()
 
55
{
 
56
    if (!arguments || arguments.length < 1 || !RegExp)
 
57
    {
 
58
        return;
 
59
    }
 
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))
 
64
    {
 
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];
 
67
 
 
68
        //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);
 
69
 
 
70
        numMatches++;
 
71
        if (pType == '%')
 
72
        {
 
73
            subst = '%';
 
74
        }
 
75
        else
 
76
        {
 
77
            numSubstitutions++;
 
78
            if (numSubstitutions >= arguments.length)
 
79
            {
 
80
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
 
81
            }
 
82
            var param = arguments[numSubstitutions];
 
83
            var pad = '';
 
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;
 
88
            var minLength = -1;
 
89
            if (pMinLength) minLength = parseInt(pMinLength);
 
90
            var precision = -1;
 
91
            if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
 
92
            var subst = param;
 
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();
 
102
        }
 
103
        str = leftpart + subst + rightPart;
 
104
    }
 
105
    return str;
 
106
}