/
i18n.js
70 lines (64 loc) · 2.1 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
var i18n = require('../i18n')
, exec = require('child_process').exec
, path = require('path')
, fs = require('fs');
module.exports = new (function () {
var LOCALE_PAT = /([^\/]*).json$/;
var _loadLocaleData = function (directory, callback) {
};
this.init = function (callback) {
var localePaths = [
__dirname + '/../../templates/locales'
]
, loadLocaleData = function () {
directory = localePaths.shift();
if (directory) {
if (path.existsSync(directory)) {
exec('find ' + directory + ' | grep json$',
function (err, stdout, stderr) {
var files
, file
, locale
, data;
if (err) {
throw err;
}
else if (stderr) {
geddy.log.error('Error: ' + stderr);
}
else {
files = stdout.split('\n');
for (var i = 0; i < files.length; i++) {
file = files[i];
if (file) {
// Extract the locale-name from the filename (e.g.,
// foo/bar/baz/en-us.json => 'en-us'
if ((locale = LOCALE_PAT.exec(file)) && locale[1]) {
try {
data = fs.readFileSync(file).toString();
data = JSON.parse(data);
}
catch(e) {
throw new Error('Could not parse locale-data in file: ' +
file);
}
i18n.loadLocale(locale[1], data);
}
}
}
loadLocaleData();
}
});
}
else {
loadLocaleData();
}
}
else {
callback();
}
};
localePaths = localePaths.concat(geddy.config.i18n.loadPaths);
loadLocaleData();
};
})();