Moving out the translate function to helpers folder (#10882)

* Moving out the translate function to helpers

GitOrigin-RevId: 876932308328761bf6b728b3d24a8867d950e9c0
This commit is contained in:
Davinder Singh 2022-12-21 11:34:42 +00:00 committed by Copybot
parent 55e05d2fb6
commit b7d8fa44b4
2 changed files with 37 additions and 31 deletions

View file

@ -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 }

View file

@ -6,13 +6,11 @@ const _ = require('lodash')
const { URL } = require('url') const { URL } = require('url')
const Path = require('path') const Path = require('path')
const moment = require('moment') const moment = require('moment')
const pug = require('pug-runtime')
const request = require('request') const request = require('request')
const Features = require('./Features') const Features = require('./Features')
const SessionManager = require('../Features/Authentication/SessionManager') const SessionManager = require('../Features/Authentication/SessionManager')
const PackageVersions = require('./PackageVersions') const PackageVersions = require('./PackageVersions')
const Modules = require('./Modules') const Modules = require('./Modules')
const SafeHTMLSubstitute = require('../Features/Helpers/SafeHTMLSubstitution')
const { const {
canRedirectToAdminDomain, canRedirectToAdminDomain,
hasAdminAccess, hasAdminAccess,
@ -20,6 +18,7 @@ const {
const { const {
addOptionalCleanupHandlerAfterDrainingConnections, addOptionalCleanupHandlerAfterDrainingConnections,
} = require('./GracefulShutdown') } = require('./GracefulShutdown')
const { translate } = require('../Features/Helpers/Translate')
const IEEE_BRAND_ID = Settings.ieeeBrandId const IEEE_BRAND_ID = Settings.ieeeBrandId
@ -77,8 +76,6 @@ function getWebpackAssets(entrypoint, section) {
return webpackManifest.entrypoints[entrypoint].assets[section] || [] return webpackManifest.entrypoints[entrypoint].assets[section] || []
} }
const I18N_HTML_INJECTIONS = new Set()
module.exports = function (webRouter, privateApiRouter, publicApiRouter) { module.exports = function (webRouter, privateApiRouter, publicApiRouter) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
// In the dev-env, delay requests until we fetched the manifest once. // 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) { webRouter.use(function (req, res, next) {
res.locals.translate = function (key, vars, components) { res.locals.translate = (key, vars, components) =>
vars = vars || {} translate(key, req, vars, components)
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
}
}
// Don't include the query string parameters, otherwise Google // Don't include the query string parameters, otherwise Google
// treats ?nocdn=true as the canonical version // treats ?nocdn=true as the canonical version
const parsedOriginalUrl = new URL(req.originalUrl, Settings.siteUrl) const parsedOriginalUrl = new URL(req.originalUrl, Settings.siteUrl)