Skip to content

Commit

Permalink
when compiling po to json ignore white space difference (#3597)
Browse files Browse the repository at this point in the history
* when compiling po to json ignore white space difference

so when checking placeholders `{{count}}` will match `{{ count }}`

* avoid matchAll which is not supported in older node
  • Loading branch information
petrjasek committed Sep 14, 2020
1 parent 57c915b commit d6a127a
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions tasks/compile-translations-po-to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ function isDirectory(path) {
}
}

function escapeRegExp(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function regexMatchAll(regex, string) {
const matches = [];
let match;

while ((match = regex.exec(string)) !== null) {
matches.push(match);
}

return matches;
}

/*
It iterates all user supplied translations and removes placeholders found in the original string.
Expand All @@ -28,17 +43,21 @@ the translation is considered invalid and will not be outputted to JSON.
function removeInvalidTranslations(grunt, jsonFilePath, filename) {
var translations = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));

const KEY_REGEX = /{{ ?(.+?) ?}}/g;

const validTranslations = Object.keys(translations).filter((key) => {
if (key === '') { // metadata object added by gettext.js
return true;
}

const placeHolders = key.match(/{{.+?}}/g);
const placeHolders = regexMatchAll(KEY_REGEX, key);

return (Array.isArray(translations[key]) ? translations[key] : [translations[key]]).every(
(translatedString) => {
const translatedStringWithoutPlaceholders = (placeHolders || []).reduce((acc, item) => {
return acc.replace(item, '');
const translatedStringWithoutPlaceholders = placeHolders.reduce((acc, item) => {
const regex = RegExp(`{{ ?${escapeRegExp(item[1])} ?}}`);

return acc.replace(regex, '');
}, translatedString);

const valid = translatedStringWithoutPlaceholders.match(/{{.+?}}/) == null;
Expand All @@ -47,6 +66,7 @@ function removeInvalidTranslations(grunt, jsonFilePath, filename) {
grunt.log.error(
`Invalid translation string encountered in "${filename}"`
+ ` and will be ommited from JSON: "${translatedString}"`
+ ` for key: "${key}"`
);
}

Expand Down

0 comments on commit d6a127a

Please sign in to comment.