|
|
@ -4,7 +4,7 @@ var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var path = require('path');
|
|
|
|
var async = require('async');
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
|
|
|
|
var Languages = {};
|
|
|
|
var Languages = module.exports;
|
|
|
|
var languagesPath = path.join(__dirname, '../build/public/language');
|
|
|
|
var languagesPath = path.join(__dirname, '../build/public/language');
|
|
|
|
|
|
|
|
|
|
|
|
Languages.init = function (next) {
|
|
|
|
Languages.init = function (next) {
|
|
|
@ -27,10 +27,13 @@ Languages.get = function (language, namespace, callback) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Languages.list = function (callback) {
|
|
|
|
var codeCache = null;
|
|
|
|
var languages = [];
|
|
|
|
Languages.listCodes = function (callback) {
|
|
|
|
|
|
|
|
if (codeCache && codeCache.length) {
|
|
|
|
|
|
|
|
return callback(null, codeCache);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fs.readdir(languagesPath, function (err, files) {
|
|
|
|
fs.readFile(path.join(languagesPath, 'metadata.json'), function (err, buffer) {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
return callback(null, []);
|
|
|
|
return callback(null, []);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -38,43 +41,59 @@ Languages.list = function (callback) {
|
|
|
|
return callback(err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async.each(files, function (folder, next) {
|
|
|
|
var parsed;
|
|
|
|
fs.stat(path.join(languagesPath, folder), function (err, stat) {
|
|
|
|
try {
|
|
|
|
|
|
|
|
parsed = JSON.parse(buffer.toString());
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
return callback(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var langs = parsed.languages;
|
|
|
|
|
|
|
|
codeCache = langs;
|
|
|
|
|
|
|
|
callback(null, langs);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var listCache = null;
|
|
|
|
|
|
|
|
Languages.list = function (callback) {
|
|
|
|
|
|
|
|
if (listCache && listCache.length) {
|
|
|
|
|
|
|
|
return callback(null, listCache);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Languages.listCodes(function (err, codes) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.map(codes, function (folder, next) {
|
|
|
|
|
|
|
|
var configPath = path.join(languagesPath, folder, 'language.json');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFile(configPath, function (err, buffer) {
|
|
|
|
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
|
|
|
|
return next();
|
|
|
|
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!stat.isDirectory()) {
|
|
|
|
var lang = JSON.parse(buffer.toString());
|
|
|
|
return next();
|
|
|
|
next(null, lang);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
next(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var configPath = path.join(languagesPath, folder, 'language.json');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFile(configPath, function (err, buffer) {
|
|
|
|
|
|
|
|
if (err && err.code !== 'ENOENT') {
|
|
|
|
|
|
|
|
return next(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer) {
|
|
|
|
|
|
|
|
var lang = JSON.parse(buffer.toString());
|
|
|
|
|
|
|
|
if (lang.name && lang.code && lang.dir) {
|
|
|
|
|
|
|
|
languages.push(lang);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
}, function (err, languages) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sort alphabetically
|
|
|
|
|
|
|
|
languages = languages.sort(function (a, b) {
|
|
|
|
// filter out invalid ones
|
|
|
|
return a.code > b.code ? 1 : -1;
|
|
|
|
languages = languages.filter(function (lang) {
|
|
|
|
|
|
|
|
return lang.code && lang.name && lang.dir;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
callback(err, languages);
|
|
|
|
listCache = languages;
|
|
|
|
|
|
|
|
callback(null, languages);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Languages;
|
|
|
|
|
|
|
|