From 49127d26c9a8621aedf2f8be1a6327e565bf8ca0 Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Tue, 24 Aug 2021 10:16:25 +0300 Subject: [PATCH 1/2] Replace {} with ### in string placeholders --- json-to-pot.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/json-to-pot.js b/json-to-pot.js index fc911949ad..041c0fc446 100644 --- a/json-to-pot.js +++ b/json-to-pot.js @@ -1,9 +1,49 @@ +// More about the structure of .po files: +// https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html#PO-Files + +/* + * white-space + # translator-comments + #. extracted-comments + #: referenceā€¦ + #, flagā€¦ + #| msgid previous-untranslated-string + msgid untranslated-string + msgstr translated-string + */ + const json = require('./src/locales/en.json') +const fs = require('fs') const curlyRegex = new RegExp('{[a-z]*}') const containsCurlyWord = (string) => curlyRegex.test(string) const checkStringForVars = (string) => - containsCurlyWord(string) ? '(Do not translate words in curly braces)' : '' + containsCurlyWord(string) ? '(Do not translate words between ###)' : '' + +/** + * For GlotPress to display warning when the translators try to + * replace placeholders with something else, we need to wrap the + * placeholders with `###word###` + * @param string + * @return {string} + */ +const replaceVarsPlaceholders = (string) => { + if (!containsCurlyWord(string)) { + return string + } + const variable = /{(?[a-z]*)}/g + return string.replace(variable, `###$###`) +} + +/** + * Replace placeholder format for variables, + * escape quotes (in a different PR) + * @param string + * @return {string} + */ +const processValue = (string) => { + return replaceVarsPlaceholders(string) +} const findPath = (ob, key) => { const path = [] @@ -56,7 +96,7 @@ function potTime(json, parent = json) { # ${findPath(parent, key)}.${key} ${checkStringForVars(value)} msgctxt "${findPath(parent, key)}.${key}" -msgid "${value}" +msgid "${processValue(value)}" msgstr ""` } if (typeof value === 'object') { @@ -67,4 +107,10 @@ msgstr ""` } const potFile = potTime(json) - +try { + const fileName = 'test.pot' + fs.writeFileSync(fileName, potFile) + console.log(`Successfully wrote pot file to ${fileName}`) +} catch (err) { + console.error(err) +} From f672b52ae4203d05d9f6b24297ec292d7224dc5f Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Tue, 24 Aug 2021 09:28:14 +0300 Subject: [PATCH 2/2] Escape double quotes in strings --- json-to-pot.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/json-to-pot.js b/json-to-pot.js index 041c0fc446..7011e953e0 100644 --- a/json-to-pot.js +++ b/json-to-pot.js @@ -42,7 +42,7 @@ const replaceVarsPlaceholders = (string) => { * @return {string} */ const processValue = (string) => { - return replaceVarsPlaceholders(string) + return escapeQuotes(replaceVarsPlaceholders(string)) } const findPath = (ob, key) => { @@ -81,6 +81,8 @@ const findPath = (ob, key) => { return path.join('.') } +const escapeQuotes = (str) => str.replace(/"/g, '\\"') + // POT Syntax // msgctxt context