From b7d8fa44b4b98820c2f0879dbfbdf3fc69387e4f Mon Sep 17 00:00:00 2001 From: Davinder Singh Date: Wed, 21 Dec 2022 11:34:42 +0000 Subject: [PATCH] Moving out the translate function to helpers folder (#10882) * Moving out the translate function to helpers GitOrigin-RevId: 876932308328761bf6b728b3d24a8867d950e9c0 --- .../web/app/src/Features/Helpers/Translate.js | 34 +++++++++++++++++++ .../app/src/infrastructure/ExpressLocals.js | 34 ++----------------- 2 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 services/web/app/src/Features/Helpers/Translate.js diff --git a/services/web/app/src/Features/Helpers/Translate.js b/services/web/app/src/Features/Helpers/Translate.js new file mode 100644 index 0000000000..03b6b78a56 --- /dev/null +++ b/services/web/app/src/Features/Helpers/Translate.js @@ -0,0 +1,34 @@ +const Settings = require('@overleaf/settings') +const pug = require('pug-runtime') +const logger = require('@overleaf/logger') +const SafeHTMLSubstitute = require('./SafeHTMLSubstitution') +const I18N_HTML_INJECTIONS = new Set() + +function translate(key, req, vars, components) { + vars = vars || {} + + if (Settings.i18n.checkForHTMLInVars) { + Object.entries(vars).forEach(([field, value]) => { + if (pug.escape(value) !== value) { + const violationsKey = key + field + // do not flood the logs, log one sample per pod + key + field + if (!I18N_HTML_INJECTIONS.has(violationsKey)) { + logger.warn( + { key, field, value }, + 'html content in translations context vars' + ) + I18N_HTML_INJECTIONS.add(violationsKey) + } + } + }) + } + + vars.appName = Settings.appName + const locale = req.i18n.translate(key, vars) + if (components) { + return SafeHTMLSubstitute.render(locale, components) + } else { + return locale + } +} +module.exports = { translate } diff --git a/services/web/app/src/infrastructure/ExpressLocals.js b/services/web/app/src/infrastructure/ExpressLocals.js index 3dd065e2d1..ede2532e25 100644 --- a/services/web/app/src/infrastructure/ExpressLocals.js +++ b/services/web/app/src/infrastructure/ExpressLocals.js @@ -6,13 +6,11 @@ const _ = require('lodash') const { URL } = require('url') const Path = require('path') const moment = require('moment') -const pug = require('pug-runtime') const request = require('request') const Features = require('./Features') const SessionManager = require('../Features/Authentication/SessionManager') const PackageVersions = require('./PackageVersions') const Modules = require('./Modules') -const SafeHTMLSubstitute = require('../Features/Helpers/SafeHTMLSubstitution') const { canRedirectToAdminDomain, hasAdminAccess, @@ -20,6 +18,7 @@ const { const { addOptionalCleanupHandlerAfterDrainingConnections, } = require('./GracefulShutdown') +const { translate } = require('../Features/Helpers/Translate') const IEEE_BRAND_ID = Settings.ieeeBrandId @@ -77,8 +76,6 @@ function getWebpackAssets(entrypoint, section) { return webpackManifest.entrypoints[entrypoint].assets[section] || [] } -const I18N_HTML_INJECTIONS = new Set() - module.exports = function (webRouter, privateApiRouter, publicApiRouter) { if (process.env.NODE_ENV === 'development') { // In the dev-env, delay requests until we fetched the manifest once. @@ -219,33 +216,8 @@ module.exports = function (webRouter, privateApiRouter, publicApiRouter) { }) webRouter.use(function (req, res, next) { - res.locals.translate = function (key, vars, components) { - vars = vars || {} - - if (Settings.i18n.checkForHTMLInVars) { - Object.entries(vars).forEach(([field, value]) => { - if (pug.escape(value) !== value) { - const violationsKey = key + field - // do not flood the logs, log one sample per pod + key + field - if (!I18N_HTML_INJECTIONS.has(violationsKey)) { - logger.warn( - { key, field, value }, - 'html content in translations context vars' - ) - I18N_HTML_INJECTIONS.add(violationsKey) - } - } - }) - } - - vars.appName = Settings.appName - const locale = req.i18n.translate(key, vars) - if (components) { - return SafeHTMLSubstitute.render(locale, components) - } else { - return locale - } - } + res.locals.translate = (key, vars, components) => + translate(key, req, vars, components) // Don't include the query string parameters, otherwise Google // treats ?nocdn=true as the canonical version const parsedOriginalUrl = new URL(req.originalUrl, Settings.siteUrl)