mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 16:30:34 +01:00
All hail the glorious translation prebuilding
This commit is contained in:
@@ -3,53 +3,27 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var async = require('async');
|
||||
var LRU = require('lru-cache');
|
||||
|
||||
var plugins = require('./plugins');
|
||||
|
||||
var Languages = {};
|
||||
var languagesPath = path.join(__dirname, '../public/language');
|
||||
var languagesPath = path.join(__dirname, '../build/public/language');
|
||||
|
||||
Languages.init = function (next) {
|
||||
if (Languages.hasOwnProperty('_cache')) {
|
||||
Languages._cache.reset();
|
||||
} else {
|
||||
Languages._cache = LRU(100);
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
Languages.get = function (language, namespace, callback) {
|
||||
var langNamespace = language + '/' + namespace;
|
||||
|
||||
if (Languages._cache && Languages._cache.has(langNamespace)) {
|
||||
return callback(null, Languages._cache.get(langNamespace));
|
||||
}
|
||||
|
||||
var languageData;
|
||||
|
||||
fs.readFile(path.join(languagesPath, language, namespace + '.json'), { encoding: 'utf-8' }, function (err, data) {
|
||||
if (err && err.code !== 'ENOENT') {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
// If language file in core cannot be read, then no language file present
|
||||
try {
|
||||
languageData = JSON.parse(data) || {};
|
||||
data = JSON.parse(data) || {};
|
||||
} catch (e) {
|
||||
languageData = {};
|
||||
return callback(e);
|
||||
}
|
||||
|
||||
if (plugins.customLanguages.hasOwnProperty(langNamespace)) {
|
||||
Object.assign(languageData, plugins.customLanguages[langNamespace]);
|
||||
}
|
||||
|
||||
if (Languages._cache) {
|
||||
Languages._cache.set(langNamespace, languageData);
|
||||
}
|
||||
|
||||
callback(null, languageData);
|
||||
callback(null, data);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -73,11 +47,13 @@ Languages.list = function (callback) {
|
||||
|
||||
var configPath = path.join(languagesPath, folder, 'language.json');
|
||||
|
||||
fs.readFile(configPath, function (err, stream) {
|
||||
if (err) {
|
||||
fs.readFile(configPath, function (err, buffer) {
|
||||
if (err && err.code !== 'ENOENT') {
|
||||
return next(err);
|
||||
}
|
||||
languages.push(JSON.parse(stream.toString()));
|
||||
if (buffer) {
|
||||
languages.push(JSON.parse(buffer.toString()));
|
||||
}
|
||||
next();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user