Skip to content

Node.js based forum software built for the modern web

feat: load timeago strings client-side

Julian Lam558a2d739c75
diff --git a/public/src/app.js b/public/src/app.js
index c06a8c9929..39f80815cc 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -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: {
diff --git a/src/middleware/header.js b/src/middleware/header.js
index 4873a210b2..fd736de06c 100644
--- a/src/middleware/header.js
+++ b/src/middleware/header.js
@@ -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 };
});