forked from geddy/geddy
/
i18n.js
53 lines (46 loc) · 1.65 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
var i18n = require('../i18n')
, exec = require('child_process').exec
, path = require('path')
, fs = require('fs')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, fileUtils = require('../utils/file');
module.exports = new (function () {
var LOCALE_PAT = /([^\/]*).json$/;
var _loadLocaleData = function (directory, callback) {
};
this.init = function (callback) {
var localePaths = [
__dirname + '/../../templates/locales'
]
, loadLocaleData = function () {
localePaths.forEach(function (directory) {
directory = path.normalize(directory);
if (existsSync(directory)) {
var files = fileUtils.readdirR(directory);
for (var i = 0; i < files.length; i++) {
file = files[i];
if (file && /.json$/.test(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);
}
}
}
}
});
callback();
};
localePaths = localePaths.concat(geddy.config.i18n.loadPaths || []);
loadLocaleData();
};
})();