From 1bc459de66d71b410fafcfc1f0eec127b132d87e Mon Sep 17 00:00:00 2001 From: Glenn de Haan Date: Wed, 9 Oct 2024 13:48:26 +0200 Subject: [PATCH] Swapped mail function parameters. Replaced throw errors for log warnings. Implemented fallback language in translation.js. Added express-locale for future web i18n use --- modules/mail.js | 2 +- modules/translation.js | 13 +++++++++---- package-lock.json | 7 +++++++ package.json | 1 + server.js | 9 +++++++++ 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/modules/mail.js b/modules/mail.js index f65f00a..379a339 100644 --- a/modules/mail.js +++ b/modules/mail.js @@ -49,7 +49,7 @@ module.exports = { send: (to, voucher) => { return new Promise(async (resolve, reject) => { // Create new translator - const t = translation('en', 'email'); + const t = translation('email'); // Attempt to send mail via SMTP transport const result = await transport.sendMail({ diff --git a/modules/translation.js b/modules/translation.js index f70a4e3..c0bb1c5 100644 --- a/modules/translation.js +++ b/modules/translation.js @@ -6,19 +6,23 @@ const fs = require('fs'); /** * Import own modules */ +const log = require('./log'); const variables = require('./variables'); /** * Translation returns translator function * - * @param language * @param module + * @param language + * @param fallback * @return {(function(key: string): (string))} */ -module.exports = (language = 'en', module) => { +module.exports = (module, language = 'en', fallback = 'en') => { // Check if translation file exists if(!fs.existsSync(`${__dirname}/../locales/${language}/${module}.json`)) { - throw new Error(`[Translation] Missing translation file: ${__dirname}/../locales/${language}/${module}.json`); + log.warn(`[Translation] Missing translation file: ${__dirname}/../locales/${language}/${module}.json`); + language = fallback; + log.warn(`[Translation] Using fallback: ${__dirname}/../locales/${language}/${module}.json`); } // Get locales mapping @@ -34,7 +38,8 @@ module.exports = (language = 'en', module) => { // Check if key exists within translation file if(typeof translations[key] === 'undefined') { - throw new Error(`[Translation][${language}] Missing for key: ${key}`); + log.warn(`[Translation][${language}] Missing for key: ${key}`); + return `%${key}%`; } // Check if debugging is enabled. If enabled only return key diff --git a/package-lock.json b/package-lock.json index 14b5731..94fc3da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "cookie-parser": "^1.4.7", "ejs": "^3.1.10", "express": "^4.21.1", + "express-locale": "^2.0.2", "express-openid-connect": "^2.17.1", "js-logger": "^1.6.1", "jsonwebtoken": "^9.0.2", @@ -1343,6 +1344,12 @@ "node": ">= 0.10.0" } }, + "node_modules/express-locale": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/express-locale/-/express-locale-2.0.2.tgz", + "integrity": "sha512-z1hRa5iOwlgcM2iGpho3Mwq6DKv0534h1Ts2g2/Gct72g/YSrYSsSCHejLGjAT+hGoZOFcDUovmCkM+6YcQ4iQ==", + "license": "MIT" + }, "node_modules/express-openid-connect": { "version": "2.17.1", "resolved": "https://registry.npmjs.org/express-openid-connect/-/express-openid-connect-2.17.1.tgz", diff --git a/package.json b/package.json index d447f1e..e1207f7 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "cookie-parser": "^1.4.7", "ejs": "^3.1.10", "express": "^4.21.1", + "express-locale": "^2.0.2", "express-openid-connect": "^2.17.1", "js-logger": "^1.6.1", "jsonwebtoken": "^9.0.2", diff --git a/server.js b/server.js index a12bb44..476b7b5 100644 --- a/server.js +++ b/server.js @@ -6,6 +6,7 @@ const crypto = require('crypto'); const express = require('express'); const multer = require('multer'); const cookieParser = require('cookie-parser'); +const locale = require('express-locale'); /** * Import own modules @@ -96,6 +97,14 @@ if(!variables.authDisabled && variables.authOidcEnabled) { oidc.init(app); } +/** + * Enable locale + */ +app.use(locale({ + "priority": ["accept-language", "default"], + "default": "en-GB" +})); + /** * Enable multer */