-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
220 lines (183 loc) · 5.14 KB
/
index.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Module dependencies
*/
var plural = {
cardinal: require('lang-js-cardinal'),
ordinal: require('lang-js-ordinal')
};
var number = require('lang-js-number');
var interpolate = require('lang-js-interpolate');
/**
* Expose the translate function
*/
module.exports = translate;
/**
* Expose the number formatting function
*/
translate.number = function(locale, opts) {
opts = opts || {};
format = opts.style || 'decimal';
return lookup(locale, format, number);
};
/**
* Compile a translation function
*
* @param {String|Array|Object} cldr
* @param {String} locale
* @param {Object?} opts
* @return {Function}
*/
function translate(cldr, locale, opts) {
if (typeof cldr === 'string') return augment(interpolate(cldr, opts));
opts = opts || {};
var pluralize = lookup(locale, cldr._format || opts.pluralFormat, plural);
if (Array.isArray(cldr)) cldr = convertArray(cldr, pluralize, opts);
validate(cldr, pluralize);
var paramsObj = {};
var cases = toFunctions(cldr, pluralize, opts, paramsObj);
var paramsKeys = Object.keys(paramsObj);
var key = cldr._plural_key ||
opts.pluralKey ||
discoverKey(paramsKeys,
opts.discoverableKeys || {'smart_count':1, 'count':1, 'length':1, 'items':1, 'total':1},
opts.defaultPluralKey || 'smart_count');
var validatePluralKey = opts.validatePluralKey !== false;
var silentValidation = !!opts.validatePluralSilent;
var decimal = number.decimal[locale] || number.decimal.en;
var toLocaleString = opts.toLocaleString !== false;
return augment(function(params) {
if (typeof params === 'number') params = convertSmartCount(params, key);
var count = parseInt(params[key], 10);
if (validatePluralKey && isNaN(count)) {
if (silentValidation) return [];
throw new Error('expected "' + key + '" to be a number. got "' + (typeof params[key]) + '".');
}
if (toLocaleString) params = formatNumbers(params, decimal);
return (cases[count] || cases[pluralize(count || 0)])(params);
}, paramsKeys);
}
/**
* Validate a cldr against a pluralize function
*
* @param {Object} cldr
* @param {Function} pluralize
*/
function validate(cldr, pluralize) {
pluralize.formats.forEach(function(key) {
if (!cldr[key]) throw new Error('translation object missing required key "' + key + '"');
});
}
/**
* Convert a cldr object to an object of functions
*
* @param {Object} cldr
* @param {Function} pluralize
* @param {Object} opts
* @param {Object} paramsObj
* @return {Object}
*/
function toFunctions(cldr, pluralize, opts, paramsObj) {
return Object.keys(cldr).reduce(function(acc, key) {
if (key.indexOf('_') === 0) return acc;
var value = cldr[key];
if (typeof value !== 'string') return acc;
var t = acc[key] = interpolate(value, opts);
merge(paramsObj, t.params);
return acc;
}, {});
}
/**
* Auto-discover the plural key
*
* @param {Array} keys
* @param {Object} discoverableKeys
* @param {String} defaultKey
* @return {String}
*/
function discoverKey(arr, discoverableKeys, defaultKey) {
if (arr.length === 0) return defaultKey;
if (arr.length === 1) return arr[0];
for (var i = 0; i < arr.length; i++) {
if (discoverableKeys[arr[i]]) return arr[i];
}
return defaultKey;
}
/**
* Augment translate functions with params reduce functions
*
* @param {Function} fn
* @param {Array} keys
* @return {Function}
*/
function augment(fn, keys) {
keys = keys || fn.params || [];
if (!Array.isArray(keys)) keys = Object.keys(keys);
fn.params = keys;
return fn;
}
/**
* Lookup the plural function given a locale
*
* @param {String} locale
* @param {String} format
* @return {Function}
*/
function lookup(locale, format, obj) {
if (!locale) throw new Error('missing required "locale" parameter');
locale = locale.toLowerCase().replace('-', '_');
format = format || 'cardinal';
var p = obj[format];
if (!p) throw new Error('unsupported format "' + format + '"');
var fn = p[locale] || p[locale.split('_')[0]];
if (fn) return fn;
throw new Error('unsupported locale "' + locale + '"');
}
/**
* Convert an array input to a CLDR object
*
* @param {Array} arr
* @param {Function} pluralize
* @param {Object} opts
* @return {Object}
*/
function convertArray(arr, pluralize, opts) {
if (arr.length !== pluralize.count) throw new Error('missing required length of plural formats: expected ' + pluralize.count + '; got ' + arr.length);
return pluralize.formats.reduce(function(acc, key, i) {
acc[key] = arr[i];
return acc;
}, {});
}
/**
* Convert a number to a smart_count object
*
* @param {Number} val
* @param {String} key
* @return {Object}
*/
function convertSmartCount(val, key) {
var obj = {};
obj[key] = val;
return obj;
}
/**
* Format numbers with lang-js-number
*/
function formatNumbers(prevParams, decimal) {
var params = {}, value;
for (var k in prevParams) {
value = prevParams[k];
params[k] = typeof value === 'number' ? decimal(value) : value;
}
return params;
}
/**
* Merge b into a
*
* @param {Object} a
* @param {Object} b
*/
function merge(a, b) {
for (var key in b) {
a[key] = b[key];
};
}