diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index 8c68b0c06a..465a240e4e 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -580,39 +580,13 @@ prepareDOM: function prepareDOM() { // Load the appropriate timeago locale file, // and correct NodeBB language codes to timeago codes, if necessary - var languageCode; - switch (config.userLang) { - case 'en-GB': - case 'en-US': - languageCode = 'en'; - break; - - case 'fa-IR': - languageCode = 'fa'; - break; - - case 'pt-BR': - languageCode = 'pt-br'; - break; - - case 'nb': - languageCode = 'no'; - break; - - default: - languageCode = config.userLang; - break; - } + var languageCode = utils.userLangToTimeagoCode(config.userLang); - jQuery.getScript(config.relative_path + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '.js').done(function () { - jQuery('.timeago').timeago(); - adaptor.timeagoShort = assign({}, jQuery.timeago.settings.strings); + adaptor.timeagoShort = assign({}, jQuery.timeago.settings.strings); - // Retrieve the shorthand timeago values as well - jQuery.getScript(config.relative_path + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '-short.js').done(function () { - // Switch back to long-form - adaptor.toggleTimeagoShorthand(); - }); + jQuery.getScript(config.relative_path + '/assets/vendor/jquery/timeago/locales/jquery.timeago.' + languageCode + '-short.js').done(function () { + // Switch back to long-form + adaptor.toggleTimeagoShorthand(); }); // Add directional code if necessary diff --git a/public/src/utils.js b/public/src/utils.js index fe655c2e3b..6e7f0400d1 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -103,7 +103,16 @@ hasLanguageKey: function (input) { return utils.languageKeyRegex.test(input); }, - + userLangToTimeagoCode: function (userLang) { + var mapping = { + 'en-GB': 'en', + 'en-US': 'en', + 'fa-IR': 'fa', + 'pt-BR': 'pt-br', + nb: 'no', + }; + return mapping[userLang] || userLang; + }, // shallow objects merge merge: function () { var result = {}; diff --git a/src/middleware/header.js b/src/middleware/header.js index d5c3d53e6f..fe95b11a27 100644 --- a/src/middleware/header.js +++ b/src/middleware/header.js @@ -9,6 +9,7 @@ var meta = require('../meta'); var plugins = require('../plugins'); var navigation = require('../navigation'); var translator = require('../translator'); +var utils = require('../utils'); var controllers = { api: require('../controllers/api'), @@ -152,6 +153,8 @@ module.exports = function (middleware) { return { src: script }; }); + addTimeagoLocaleScript(templateValues.scripts, res.locals.config.userLang); + if (req.route && req.route.path === '/') { modifyTitle(templateValues); } @@ -168,6 +171,11 @@ 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 (req, res, data, callback) { async.waterfall([ function (next) {