feat: #7743, meta/languages and languages
parent
66aa443b7a
commit
c02686bfc8
@ -1,99 +1,73 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
var async = require('async');
|
|
||||||
|
|
||||||
var Languages = module.exports;
|
const util = require('util');
|
||||||
var languagesPath = path.join(__dirname, '../build/public/language');
|
const readFileAsync = util.promisify(fs.readFile);
|
||||||
|
|
||||||
|
const Languages = module.exports;
|
||||||
|
const languagesPath = path.join(__dirname, '../build/public/language');
|
||||||
|
|
||||||
const files = fs.readdirSync(path.join(__dirname, '../public/vendor/jquery/timeago/locales'));
|
const files = fs.readdirSync(path.join(__dirname, '../public/vendor/jquery/timeago/locales'));
|
||||||
Languages.timeagoCodes = files.filter(f => f.startsWith('jquery.timeago')).map(f => f.split('.')[2]);
|
Languages.timeagoCodes = files.filter(f => f.startsWith('jquery.timeago')).map(f => f.split('.')[2]);
|
||||||
|
|
||||||
Languages.get = function (language, namespace, callback) {
|
Languages.get = async function (language, namespace) {
|
||||||
fs.readFile(path.join(languagesPath, language, namespace + '.json'), { encoding: 'utf-8' }, function (err, data) {
|
let data = await readFileAsync(path.join(languagesPath, language, namespace + '.json'), 'utf8');
|
||||||
if (err) {
|
try {
|
||||||
return callback(err);
|
data = JSON.parse(data) || {};
|
||||||
}
|
} catch (err) {
|
||||||
|
throw err;
|
||||||
try {
|
}
|
||||||
data = JSON.parse(data) || {};
|
return data;
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, data);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var codeCache = null;
|
let codeCache = null;
|
||||||
Languages.listCodes = function (callback) {
|
Languages.listCodes = async function () {
|
||||||
if (codeCache && codeCache.length) {
|
if (codeCache && codeCache.length) {
|
||||||
return callback(null, codeCache);
|
return codeCache;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
fs.readFile(path.join(languagesPath, 'metadata.json'), 'utf8', function (err, file) {
|
const file = await readFileAsync(path.join(languagesPath, 'metadata.json'), 'utf8');
|
||||||
if (err && err.code === 'ENOENT') {
|
const parsed = JSON.parse(file);
|
||||||
return callback(null, []);
|
|
||||||
|
codeCache = parsed.languages;
|
||||||
|
return parsed.languages;
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === 'ENOENT') {
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
if (err) {
|
throw err;
|
||||||
return callback(err);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var parsed;
|
|
||||||
try {
|
|
||||||
parsed = JSON.parse(file);
|
|
||||||
} catch (e) {
|
|
||||||
return callback(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
var langs = parsed.languages;
|
|
||||||
codeCache = langs;
|
|
||||||
callback(null, langs);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var listCache = null;
|
let listCache = null;
|
||||||
Languages.list = function (callback) {
|
Languages.list = async function () {
|
||||||
if (listCache && listCache.length) {
|
if (listCache && listCache.length) {
|
||||||
return callback(null, listCache);
|
return listCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
Languages.listCodes(function (err, codes) {
|
const codes = await Languages.listCodes();
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
async.map(codes, function (folder, next) {
|
let languages = await Promise.all(codes.map(async function (folder) {
|
||||||
var configPath = path.join(languagesPath, folder, 'language.json');
|
try {
|
||||||
|
const configPath = path.join(languagesPath, folder, 'language.json');
|
||||||
fs.readFile(configPath, 'utf8', function (err, file) {
|
const file = await readFileAsync(configPath, 'utf8');
|
||||||
if (err && err.code === 'ENOENT') {
|
const lang = JSON.parse(file);
|
||||||
return next();
|
return lang;
|
||||||
}
|
} catch (err) {
|
||||||
if (err) {
|
if (err.code === 'ENOENT') {
|
||||||
return next(err);
|
return;
|
||||||
}
|
|
||||||
var lang;
|
|
||||||
try {
|
|
||||||
lang = JSON.parse(file);
|
|
||||||
} catch (e) {
|
|
||||||
return next(e);
|
|
||||||
}
|
|
||||||
next(null, lang);
|
|
||||||
});
|
|
||||||
}, function (err, languages) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
// filter out invalid ones
|
// filter out invalid ones
|
||||||
languages = languages.filter(function (lang) {
|
languages = languages.filter(lang => lang && lang.code && lang.name && lang.dir);
|
||||||
return lang && lang.code && lang.name && lang.dir;
|
|
||||||
});
|
|
||||||
|
|
||||||
listCache = languages;
|
listCache = languages;
|
||||||
callback(null, languages);
|
return languages;
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
require('./promisify')(Languages);
|
||||||
|
@ -1,169 +1,136 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
var async = require('async');
|
const fs = require('fs');
|
||||||
var fs = require('fs');
|
const mkdirp = require('mkdirp');
|
||||||
var mkdirp = require('mkdirp');
|
const rimraf = require('rimraf');
|
||||||
var rimraf = require('rimraf');
|
const _ = require('lodash');
|
||||||
var _ = require('lodash');
|
|
||||||
|
const util = require('util');
|
||||||
var file = require('../file');
|
const mkdirpAsync = util.promisify(mkdirp);
|
||||||
var Plugins = require('../plugins');
|
const rimrafAsync = util.promisify(rimraf);
|
||||||
|
const writeFileAsync = util.promisify(fs.writeFile);
|
||||||
var buildLanguagesPath = path.join(__dirname, '../../build/public/language');
|
const readFileAsync = util.promisify(fs.readFile);
|
||||||
var coreLanguagesPath = path.join(__dirname, '../../public/language');
|
|
||||||
|
const file = require('../file');
|
||||||
function getTranslationMetadata(callback) {
|
const Plugins = require('../plugins');
|
||||||
async.waterfall([
|
|
||||||
// generate list of languages and namespaces
|
const buildLanguagesPath = path.join(__dirname, '../../build/public/language');
|
||||||
function (next) {
|
const coreLanguagesPath = path.join(__dirname, '../../public/language');
|
||||||
file.walk(coreLanguagesPath, next);
|
|
||||||
},
|
async function getTranslationMetadata() {
|
||||||
function (paths, next) {
|
const paths = await file.walk(coreLanguagesPath);
|
||||||
var languages = [];
|
let languages = [];
|
||||||
var namespaces = [];
|
let namespaces = [];
|
||||||
|
|
||||||
paths.forEach(function (p) {
|
paths.forEach(function (p) {
|
||||||
if (!p.endsWith('.json')) {
|
if (!p.endsWith('.json')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var rel = path.relative(coreLanguagesPath, p).split(/[/\\]/);
|
var rel = path.relative(coreLanguagesPath, p).split(/[/\\]/);
|
||||||
var language = rel.shift().replace('_', '-').replace('@', '-x-');
|
var language = rel.shift().replace('_', '-').replace('@', '-x-');
|
||||||
var namespace = rel.join('/').replace(/\.json$/, '');
|
var namespace = rel.join('/').replace(/\.json$/, '');
|
||||||
|
|
||||||
if (!language || !namespace) {
|
if (!language || !namespace) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
languages.push(language);
|
languages.push(language);
|
||||||
namespaces.push(namespace);
|
namespaces.push(namespace);
|
||||||
});
|
});
|
||||||
|
|
||||||
next(null, {
|
|
||||||
languages: _.union(languages, Plugins.languageData.languages).sort().filter(Boolean),
|
languages = _.union(languages, Plugins.languageData.languages).sort().filter(Boolean);
|
||||||
namespaces: _.union(namespaces, Plugins.languageData.namespaces).sort().filter(Boolean),
|
namespaces = _.union(namespaces, Plugins.languageData.namespaces).sort().filter(Boolean);
|
||||||
});
|
|
||||||
},
|
// save a list of languages to `${buildLanguagesPath}/metadata.json`
|
||||||
|
// avoids readdirs later on
|
||||||
// save a list of languages to `${buildLanguagesPath}/metadata.json`
|
await mkdirpAsync(buildLanguagesPath);
|
||||||
// avoids readdirs later on
|
const result = {
|
||||||
function (ref, next) {
|
languages: languages,
|
||||||
async.series([
|
namespaces: namespaces,
|
||||||
function (next) {
|
};
|
||||||
mkdirp(buildLanguagesPath, next);
|
await writeFileAsync(path.join(buildLanguagesPath, 'metadata.json'), JSON.stringify(result));
|
||||||
},
|
return result;
|
||||||
function (next) {
|
|
||||||
fs.writeFile(path.join(buildLanguagesPath, 'metadata.json'), JSON.stringify({
|
|
||||||
languages: ref.languages,
|
|
||||||
namespaces: ref.namespaces,
|
|
||||||
}), next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
next(err, ref);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeLanguageFile(language, namespace, translations, callback) {
|
async function writeLanguageFile(language, namespace, translations) {
|
||||||
var dev = global.env === 'development';
|
const dev = global.env === 'development';
|
||||||
var filePath = path.join(buildLanguagesPath, language, namespace + '.json');
|
const filePath = path.join(buildLanguagesPath, language, namespace + '.json');
|
||||||
|
|
||||||
async.series([
|
await mkdirpAsync(path.dirname(filePath));
|
||||||
async.apply(mkdirp, path.dirname(filePath)),
|
await writeFileAsync(filePath, JSON.stringify(translations, null, dev ? 2 : 0));
|
||||||
async.apply(fs.writeFile, filePath, JSON.stringify(translations, null, dev ? 2 : 0)),
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for each language and namespace combination,
|
// for each language and namespace combination,
|
||||||
// run through core and all plugins to generate
|
// run through core and all plugins to generate
|
||||||
// a full translation hash
|
// a full translation hash
|
||||||
function buildTranslations(ref, next) {
|
async function buildTranslations(ref) {
|
||||||
var namespaces = ref.namespaces;
|
const namespaces = ref.namespaces;
|
||||||
var languages = ref.languages;
|
const languages = ref.languages;
|
||||||
var plugins = _.values(Plugins.pluginsData).filter(function (plugin) {
|
const plugins = _.values(Plugins.pluginsData).filter(function (plugin) {
|
||||||
return typeof plugin.languages === 'string';
|
return typeof plugin.languages === 'string';
|
||||||
});
|
});
|
||||||
|
|
||||||
async.each(namespaces, function (namespace, next) {
|
const promises = [];
|
||||||
async.each(languages, function (lang, next) {
|
|
||||||
var translations = {};
|
namespaces.forEach(function (namespace) {
|
||||||
|
languages.forEach(function (language) {
|
||||||
async.series([
|
promises.push(buildNamespaceLanguage(language, namespace, plugins));
|
||||||
// core first
|
});
|
||||||
function (cb) {
|
});
|
||||||
fs.readFile(path.join(coreLanguagesPath, lang, namespace + '.json'), 'utf8', function (err, file) {
|
|
||||||
if (err) {
|
await Promise.all(promises);
|
||||||
if (err.code === 'ENOENT') {
|
}
|
||||||
return cb();
|
|
||||||
}
|
async function buildNamespaceLanguage(lang, namespace, plugins) {
|
||||||
return cb(err);
|
const translations = {};
|
||||||
}
|
// core first
|
||||||
|
await assignFileToTranslations(translations, path.join(coreLanguagesPath, lang, namespace + '.json'));
|
||||||
try {
|
|
||||||
Object.assign(translations, JSON.parse(file));
|
await Promise.all(plugins.map(pluginData => addPlugin(translations, pluginData, lang, namespace)));
|
||||||
cb();
|
|
||||||
} catch (err) {
|
if (Object.keys(translations).length) {
|
||||||
cb(err);
|
await writeLanguageFile(lang, namespace, translations);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
},
|
|
||||||
function (cb) {
|
async function addPlugin(translations, pluginData, lang, namespace) {
|
||||||
// for each plugin, fallback in this order:
|
const pluginLanguages = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
|
||||||
// 1. correct language string (en-GB)
|
const defaultLang = pluginData.defaultLang || 'en-GB';
|
||||||
// 2. old language string (en_GB)
|
|
||||||
// 3. corrected plugin defaultLang (en-US)
|
// for each plugin, fallback in this order:
|
||||||
// 4. old plugin defaultLang (en_US)
|
// 1. correct language string (en-GB)
|
||||||
async.each(plugins, function (pluginData, done) {
|
// 2. old language string (en_GB)
|
||||||
var pluginLanguages = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
|
// 3. corrected plugin defaultLang (en-US)
|
||||||
var defaultLang = pluginData.defaultLang || 'en-GB';
|
// 4. old plugin defaultLang (en_US)
|
||||||
|
const langs = [
|
||||||
async.eachSeries([
|
defaultLang.replace('-', '_').replace('-x-', '@'),
|
||||||
defaultLang.replace('-', '_').replace('-x-', '@'),
|
defaultLang.replace('_', '-').replace('@', '-x-'),
|
||||||
defaultLang.replace('_', '-').replace('@', '-x-'),
|
lang.replace('-', '_').replace('-x-', '@'),
|
||||||
lang.replace('-', '_').replace('-x-', '@'),
|
lang,
|
||||||
lang,
|
];
|
||||||
], function (language, next) {
|
|
||||||
fs.readFile(path.join(pluginLanguages, language, namespace + '.json'), 'utf8', function (err, file) {
|
for (const language of langs) {
|
||||||
if (err) {
|
/* eslint-disable no-await-in-loop */
|
||||||
if (err.code === 'ENOENT') {
|
await assignFileToTranslations(translations, path.join(pluginLanguages, language, namespace + '.json'));
|
||||||
return next(null, false);
|
}
|
||||||
}
|
}
|
||||||
return next(err);
|
|
||||||
}
|
async function assignFileToTranslations(translations, path) {
|
||||||
|
try {
|
||||||
try {
|
const fileData = await readFileAsync(path, 'utf8');
|
||||||
Object.assign(translations, JSON.parse(file));
|
Object.assign(translations, JSON.parse(fileData));
|
||||||
next(null, true);
|
} catch (err) {
|
||||||
} catch (err) {
|
if (err.code !== 'ENOENT') {
|
||||||
next(err);
|
throw err;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}, done);
|
|
||||||
}, function (err) {
|
|
||||||
if (err) {
|
|
||||||
return cb(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.keys(translations).length) {
|
|
||||||
writeLanguageFile(lang, namespace, translations, cb);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, next);
|
|
||||||
}, next);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.build = function buildLanguages(callback) {
|
exports.build = async function buildLanguages() {
|
||||||
async.waterfall([
|
await rimrafAsync(buildLanguagesPath);
|
||||||
function (next) {
|
const data = await getTranslationMetadata();
|
||||||
rimraf(buildLanguagesPath, next);
|
await buildTranslations(data);
|
||||||
},
|
|
||||||
getTranslationMetadata,
|
|
||||||
buildTranslations,
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue