-
Notifications
You must be signed in to change notification settings - Fork 2
/
I18n.js
170 lines (117 loc) · 4.13 KB
/
I18n.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Language and I18n methods
*
* @author Andrew Tereshko <[email protected]>
* @version 0.3.1
*/
define( ["./Logger"], function() {
app.I18n = function( groupAlias, text, ucfirst ){
ucfirst = ucfirst || false;
text = app.I18n.get( groupAlias, text, false );
return ucfirst ? app.I18n.ucfirst( text ) : text;
};
app.I18n.storage = {};
app.I18n.dictMonths = [
"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december"
];
app.I18n.dictDays = ["monday", "tuesday", "wednesday", "thursday", "friday","saturday","sunday"];
app.I18n.register = function( groupAlias, strings ){
this.storage[ groupAlias ] = strings;
};
app.I18n.ucfirst = function( str ){
var f = str.charAt(0).toUpperCase();
return f + str.substr(1, str.length-1);
};
app.I18n.monthByNum = function( num ){
return num > app.I18n.dictMonths.length - 1 ? false : app.I18n.dictMonths[ num ];
};
app.I18n.dayByNum = function( num ){
return num > app.I18n.dictDays.length - 1 ? false : app.I18n.dictDays[ num ];
};
/**
*
* @todo - check for en lang for auto adding 's' endings
*
* @param number
* @param groupAlias
* @param text
*/
app.I18n.formByNum = function( number, groupAlias, text ){
text = app.I18n._get( groupAlias, text, true );
var keys = [2, 0, 1, 1, 1, 2],
mod = number % 100,
suffix_key = Math.min( mod > 4 && mod < 20 ? 2 : keys[ Math.min( mod % 10, 5 ) ], text.length - 1 );
return text[suffix_key];
};
app.I18n.getForm = function( groupAlias, text, key ){
text = app.I18n._get( groupAlias, text, true );
return text[ Math.min( key, text.length ) ];
};
app.I18n._get = function( groupAlias, text, asArray ){
asArray = asArray || false;
var textNew = text + "";
if( app.I18n.storage[ groupAlias ] !== undefined && app.I18n.storage[ groupAlias ][ text ] !== undefined ){
textNew = app.I18n.storage[ groupAlias ][ text ];
}
if( asArray && !(textNew instanceof Array) ){
textNew = [textNew]
}
if( !asArray && textNew instanceof Array ){
textNew = textNew.length ? textNew[0] : text;
}
return textNew;
};
/**
*
*/
app.I18n.get = function( groupAlias, variable ){
var string = variable + '';
if( app.I18n.storage[ groupAlias ] !== undefined && app.I18n.storage[ groupAlias ][ variable ] !== undefined ){
string = app.I18n.storage[ groupAlias ][ variable ];
}
// process format
if( string ){
var args = Array.prototype.slice.call(arguments).splice( 2 );
args.unshift( string );
string = app.I18n.format.apply( app.I18n, args );
}
return string;
};
/**
* internal sprintf analog
*
*
*/
app.I18n.format = function( string ){
var args = Array.prototype.slice.call(arguments).splice( 1 ),
string = typeof string.substring != 'undefined' ? string : '';
// first step - replace simple forms
string = string.replace(/{%(\d+)}/g, function(match, number) {
number--;
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
// second step- find available tags and process them
string = string.replace( /\[(\w+)(:([^\]]+))?\]/g, function( match, tagName, s, tagArgs ){
var tagArgs = tagArgs ? tagArgs.split('|') : [],
tagFunc = '_' + tagName + 'Tag',
replaced = false;
if( typeof app.I18n[tagFunc] == 'function' ){
replaced = app.I18n[tagFunc]( args[0], tagArgs );
} else {
throw new Error('Unsupported tag"' + tagName + '"');
}
return replaced ? replaced : '';
});
return string;
};
app.I18n._countTag = function( number, args ){
var keys = [2, 0, 1, 1, 1, 2],
mod = number % 100,
suffix_key = Math.min( mod > 4 && mod < 20 ? 2 : keys[ Math.min( mod % 10, 5 ) ], args.length - 1 );
return args[ suffix_key ];
};
});