Skip to content

Commit

Permalink
fix: prevent format specifiers from being translated
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Feb 5, 2020
1 parent a6b80c1 commit 92febfd
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 282 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const Redis = require('@ladjs/redis');
const _ = require('lodash');
const emoji = require('remark-emoji');
const formatSpecifiers = require('format-specifiers');
const globby = require('globby');
const languages = require('@cospired/i18n-iso-languages');
const modifyFilename = require('modify-filename');
Expand Down Expand Up @@ -192,10 +193,17 @@ class Mandarin {

// attempt to translate all of these in the given language
await pMapSeries(translationsRequired, async phrase => {
// TODO: prevent %s %d and %j from getting translated
// prevent %s %d and %j from getting translated
// <https://nodejs.org/api/util.html#util_util_format_format>
// <https://github.com/nodejs/node/issues/17601>
//
let safePhrase = phrase;
for (const element of formatSpecifiers) {
safePhrase = safePhrase.replace(
new RegExp(element, 'g'),
`<span class="notranslate">${element}</span>`
);
}

// TODO: also prevent {{...}} from getting translated
// by wrapping such with `<span class="notranslate">`?

Expand All @@ -205,7 +213,7 @@ class Mandarin {

// get the translation results from Google
if (!_.isString(translation)) {
[translation] = await this.client.translate(phrase, locale);
[translation] = await this.client.translate(safePhrase, locale);
await this.redisClient.set(key, translation);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@google-cloud/translate": "^5.1.4",
"@ladjs/redis": "^1.0.3",
"@ladjs/shared-config": "^1.0.1",
"format-specifiers": "^1.0.0",
"globby": "^11.0.0",
"lodash": "^4.17.15",
"modify-filename": "^1.1.0",
Expand Down
Loading

0 comments on commit 92febfd

Please sign in to comment.