Преглед изворни кода

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

* Moving out the translate function to helpers

GitOrigin-RevId: 876932308328761bf6b728b3d24a8867d950e9c0
Davinder Singh пре 3 година
родитељ
комит
b7d8fa44b4

+ 34 - 0
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 }

+ 3 - 31
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)