fix: removal of timeago fallback middleware (#7259)

* fix: removal of timeago fallback middleware

Instead of loading English fallback on missing language, we opt
to not send a script tag for a missing language to begin with.

Timeago already loads with English as default, so it will just
continue to use English.

* fix: check userLang against supported language codes

* fix: cleaned up code as per @pitaj

* fix: added comments

* fix: more fixes as per @pitaj

* feat: added addl. test for timeago locales, fixed broken test
This commit is contained in:
Julian Lam
2019-01-17 12:25:53 -05:00
committed by GitHub
parent 0bb5681471
commit c831ff0de3
4 changed files with 35 additions and 35 deletions

View File

@@ -1,5 +1,6 @@
'use strict';
var path = require('path');
var async = require('async');
var nconf = require('nconf');
var jsesc = require('jsesc');
@@ -14,7 +15,9 @@ var plugins = require('../plugins');
var navigation = require('../navigation');
var translator = require('../translator');
var privileges = require('../privileges');
var languages = require('../languages');
var utils = require('../utils');
var file = require('../file');
var controllers = {
api: require('../controllers/api'),
@@ -223,11 +226,6 @@ module.exports = function (middleware) {
], callback);
};
function addTimeagoLocaleScript(scripts, userLang) {
var languageCode = utils.userLangToTimeagoCode(userLang);
scripts.push({ src: nconf.get('relative_path') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '.js' });
}
middleware.renderFooter = function renderFooter(req, res, data, callback) {
async.waterfall([
function (next) {
@@ -240,15 +238,35 @@ module.exports = function (middleware) {
function (data, next) {
async.parallel({
scripts: async.apply(plugins.fireHook, 'filter:scripts.get', []),
timeagoLocale: (next) => {
const userLang = res.locals.config.userLang;
const pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + utils.userLangToTimeagoCode(userLang) + '.js';
async.waterfall([
async.apply(languages.list),
(languages, next) => {
if (!languages.some(obj => obj.code === userLang)) {
return next(null, false);
}
file.exists(path.join(__dirname, '../../public', pathToLocaleFile), next);
},
(exists, next) => {
next(null, exists ? (nconf.get('relative_path') + '/assets' + pathToLocaleFile) : null);
},
], next);
},
}, function (err, results) {
next(err, data, results);
});
},
function (data, results, next) {
if (results.timeagoLocale) {
results.scripts.push(results.timeagoLocale);
}
data.templateValues.scripts = results.scripts.map(function (script) {
return { src: script };
});
addTimeagoLocaleScript(data.templateValues.scripts, res.locals.config.userLang);
data.templateValues.useCustomJS = meta.config.useCustomJS;
data.templateValues.customJS = data.templateValues.useCustomJS ? meta.config.customJS : '';