feat: load timeago strings client-side

9adaccd036 introduced the ability to
configure an assetBaseUrl, but the timeago strings were still
calling a hardcoded value as it was handled server-side. There's
no need for the strings to be loaded until timeago is initialised.
v1.18.x
Julian Lam 5 years ago
parent 79e847d424
commit 558a2d739c

@ -252,7 +252,11 @@ app.cacheBuster = null;
app.processPage = function () {
highlightNavigationLink();
$('.timeago').timeago();
app.initTimeago().then(function () {
$('.timeago').timeago();
}, function () {
console.warn('[timeago] Unable to retrieve ' + config.userLang + ' strings, falling back to English');
});
utils.makeNumbersHumanReadable($('.human-readable-number'));
@ -268,6 +272,21 @@ app.cacheBuster = null;
}
};
app.initTimeago = function () {
// Loads strings based on user language, if not already loaded
return new Promise(function (resolve, reject) {
if (app.flags._timeago) {
return resolve();
}
var pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + utils.userLangToTimeagoCode(config.userLang) + '.js';
$.getScript(config.assetBaseUrl + pathToLocaleFile).done(function () {
app.flags._timeago = true;
resolve();
}).fail(reject);
});
};
app.showMessages = function () {
var messages = {
login: {

@ -15,7 +15,6 @@ 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 helpers = require('./helpers');
@ -188,25 +187,8 @@ module.exports = function (middleware) {
templateValues: templateValues,
});
const results = await utils.promiseParallel({
scripts: plugins.fireHook('filter:scripts.get', []),
timeagoLocale: (async () => {
const languageCodes = await languages.listCodes();
const userLang = res.locals.config.userLang;
const timeagoCode = utils.userLangToTimeagoCode(userLang);
if (languageCodes.includes(userLang) && languages.timeagoCodes.includes(timeagoCode)) {
const pathToLocaleFile = '/vendor/jquery/timeago/locales/jquery.timeago.' + timeagoCode + '.js';
return nconf.get('relative_path') + '/assets' + pathToLocaleFile;
}
return false;
})(),
});
if (results.timeagoLocale) {
results.scripts.push(results.timeagoLocale);
}
data.templateValues.scripts = results.scripts.map(function (script) {
const scripts = await plugins.fireHook('filter:scripts.get', []);
data.templateValues.scripts = scripts.map(function (script) {
return { src: script };
});

Loading…
Cancel
Save