From 7c88a55153ce8287472403c710704a4a149dcd44 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 19 Jan 2017 19:04:35 -0700 Subject: [PATCH] Fix empty lines in language selectors --- src/languages.js | 5 ++++- src/meta/languages.js | 40 ++++++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/languages.js b/src/languages.js index 5374b9d87f..0769656519 100644 --- a/src/languages.js +++ b/src/languages.js @@ -52,7 +52,10 @@ Languages.list = function (callback) { return next(err); } if (buffer) { - languages.push(JSON.parse(buffer.toString())); + var lang = JSON.parse(buffer.toString()); + if (lang.name && lang.code && lang.dir) { + languages.push(lang); + } } next(); }); diff --git a/src/meta/languages.js b/src/meta/languages.js index 563b34889c..5b4d1180e8 100644 --- a/src/meta/languages.js +++ b/src/meta/languages.js @@ -5,6 +5,7 @@ var path = require('path'); var async = require('async'); var fs = require('fs'); var mkdirp = require('mkdirp'); +var rimraf = require('rimraf'); var file = require('../file'); var utils = require('../../public/src/utils'); @@ -138,42 +139,42 @@ function getTranslationTree(callback) { // for each plugin, fallback in this order: // 1. correct language string (en-GB) // 2. old language string (en_GB) - // 3. plugin defaultLang (en-US) + // 3. corrected plugin defaultLang (en-US) // 4. old plugin defaultLang (en_US) async.eachLimit(plugins, 10, function (pluginData, call) { var pluginLanguages = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages); - function tryLang(lang, onEnoent) { - fs.readFile(path.join(pluginLanguages, lang, ns + '.json'), function (err, buffer) { + + async.some([ + lang, + lang.replace('-', '_').replace('-x-', '@'), + pluginData.defaultLang.replace('_', '-').replace('@', '-x-'), + pluginData.defaultLang.replace('-', '_').replace('-x-', '@'), + ], function (language, next) { + fs.readFile(path.join(pluginLanguages, language, ns + '.json'), function (err, buffer) { if (err) { if (err.code === 'ENOENT') { - return onEnoent(); + return next(null, false); } - return call(err); + return next(err); } try { Object.assign(translations, JSON.parse(buffer.toString())); - call(); + next(null, true); } catch (err) { - call(err); + next(err); } }); - } - - tryLang(lang, function () { - tryLang(lang.replace('-', '_').replace('-x-', '@'), function () { - tryLang(pluginData.defaultLang, function () { - tryLang(pluginData.defaultLang.replace('-', '_').replace('-x-', '@'), call); - }); - }); - }); + }, call); }, function (err) { if (err) { return n(err); } - tree[lang] = tree[lang] || {}; - tree[lang][ns] = translations; + if (Object.keys(translations).length) { + tree[lang] = tree[lang] || {}; + tree[lang][ns] = translations; + } n(); }); }, @@ -209,6 +210,9 @@ function writeLanguageFiles(tree, callback) { exports.build = function buildLanguages(callback) { async.waterfall([ + function (next) { + rimraf(buildLanguagesPath, next); + }, getTranslationTree, writeLanguageFiles, ], function (err) {