From 1b23011c632b361e1d99b88e3e0db944f75903a2 Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:41:53 +0100 Subject: [PATCH 1/8] Add interpolation prefix and suffix options --- lib/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/index.js b/lib/index.js index 118b420..9ada6aa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -38,6 +38,8 @@ const defaults = { nsSeparator: ":", encoding: 'utf8', translateConditionalComments: false, + interpolationPrefix: '{{', + interpolationSuffix: '}}', i18n: { resGetPath: 'locales/__lng__.json', setJqueryExt: false @@ -105,6 +107,14 @@ function getOptions(baseOptions) { if (_.isUndefined(baseOptions.outputDir)) { options.outputDir = path.join(process.cwd(), 'i18n'); } + + if (_.isUndefined(options.i18n.interpolation)){ + options.i18n.interpolation = {} + } + + options.i18n.interpolation.prefix = options.interpolationPrefix = _.escapeRegExp(options.interpolationPrefix); + options.i18n.interpolation.suffix = options.interpolationSuffix = _.escapeRegExp(options.interpolationSuffix); + return options; } From 0099f0d8315aac07dafa879ee9eea3f1465b4d4e Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:42:52 +0100 Subject: [PATCH 2/8] Use escaped suffix and prefix options instead of hardcoding --- lib/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9ada6aa..1133a5d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -165,7 +165,8 @@ function translateAttributes($elem, options, t) { const attr = isData ? k : k.substring(0, k.length - options.attrSuffix.length); let trans = t(v); if (interpolate) { - trans = v.replace(/{{([^{}]*)}}/g, function(aa, bb) { + const replaceRegex = new RegExp(`${options.interpolationPrefix}([^{}]*)${options.interpolationSuffix}`, 'g') + trans = v.replace(replaceRegex, function(aa, bb) { return t(bb); }); } @@ -185,6 +186,8 @@ function translateAttributes($elem, options, t) { function translateElem($, elem, options, t) { let key, attr; + const replaceRegex = new RegExp(`${options.interpolationPrefix}([^{}]*)${options.interpolationSuffix}`, 'g') + const $elem = $(elem); if (options.useAttr && (attr = /^\[(.*?)\]$/.exec(options.selector))) { key = $elem.attr(attr[1]); @@ -202,7 +205,7 @@ function translateElem($, elem, options, t) { const interpolateAttr = getAttrFromSelector(options.interpolateSelector); const interpolate = $elem.filter(options.interpolateSelector).length; if (interpolate) { - trans = trans.replace(/{{([^{}]*)}}/g, function(aa, bb) { + trans = trans.replace(replaceRegex, function(aa, bb) { return t(bb); }); } @@ -211,7 +214,7 @@ function translateElem($, elem, options, t) { } if (options.allowHtml) { if (interpolate) { - $elem.html($elem.html().replace(/{{([^{}]*)}}/g, function(aa, bb) { + $elem.html($elem.html().replace(replaceRegex, function(aa, bb) { return t(bb); })); } else { From c3c4dbced2f573b409415730d1eaadd92019d64b Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:44:27 +0100 Subject: [PATCH 3/8] Add test to check for custom prefix and suffix --- test/index-test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/index-test.js b/test/index-test.js index 3923ffb..fe7154e 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -75,6 +75,22 @@ describe('processor', function () { expect($('input').attr('data-attr-t-interpolate')).to.be(undefined); }); + it('should support custom prefix and suffix when translating attributes with interpolation', async function () { + options = _.merge({}, options, { locales: ['en', 'ja'], interpolationPrefix: '_{', interpolationSuffix: '}' }); + const input = + ''; + const results = await staticI18n.process(input, options); + let $ = cheerio.load(results.en); + expect($('input').attr('href')).to.be( + 'http://www.example.com/filename.html' + ); + expect($('input').attr('data-attr-t-interpolate')).to.be(undefined); + $ = cheerio.load(results.ja); + expect($('input').attr('href')).to.be( + 'http://www.example.com/ja/filename.htm' + ); + expect($('input').attr('data-attr-t-interpolate')).to.be(undefined); + }); it('should remove interpolation related attributes', async function () { options = _.merge({}, options, { locales: ['en'] }); From b09aa467882f7881fca287cffe904ca28da90fb3 Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:45:01 +0100 Subject: [PATCH 4/8] Add test to check if prefix and suffix are escaped --- test/index-test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/index-test.js b/test/index-test.js index fe7154e..d2ff915 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -91,6 +91,24 @@ describe('processor', function () { ); expect($('input').attr('data-attr-t-interpolate')).to.be(undefined); }); + + it('should escape regex characters in prefix and suffix', async function () { + options = _.merge({}, options, { locales: ['en', 'ja'], interpolationPrefix: '${', interpolationSuffix: '}$' }); + const input = + ''; + const results = await staticI18n.process(input, options); + let $ = cheerio.load(results.en); + expect($('input').attr('href')).to.be( + 'http://www.example.com/filename.html' + ); + expect($('input').attr('data-attr-t-interpolate')).to.be(undefined); + $ = cheerio.load(results.ja); + expect($('input').attr('href')).to.be( + 'http://www.example.com/ja/filename.htm' + ); + expect($('input').attr('data-attr-t-interpolate')).to.be(undefined); + }); + it('should remove interpolation related attributes', async function () { options = _.merge({}, options, { locales: ['en'] }); From 1aa6ffa665eeab8a1eaf5a86ab7ae2913ce47c3f Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:45:34 +0100 Subject: [PATCH 5/8] Fix issue with paths on windows --- test/index-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index-test.js b/test/index-test.js index d2ff915..88bb262 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -179,12 +179,12 @@ describe('processor', function () { describe('#processDir', function () { it('should process all files', async function () { - _.merge(options, { locales: ['en', 'ja'], exclude: ['ignored/'] }); + _.merge(options, { locales: ['en', 'ja'], exclude: [`ignored${path.sep}`] }); const results = await staticI18n.processDir(basepath, options); expect(results).to.only.have.keys([ 'index.html', 'other.html', - 'sub/index.html', + path.join('sub', 'index.html'), ]); expect(results['index.html']).to.only.have.keys(['en', 'ja']); expect(results['other.html']).to.only.have.keys(['en', 'ja']); From 459234cbb2001067c1a4e25e86bedf1cad0ea2a1 Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:46:10 +0100 Subject: [PATCH 6/8] Add new options to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f0ef401..5fe104f 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,8 @@ static-i18n --attr-selector my-attr-t ... } } ``` +* `interpolationPrefix` (default: `{{`): Override the default interpolation prefix +* `interpolationSuffix` (default: `}}`): Override the default interpolation suffix * `i18n`: Object passed directly to [`i18next.init`](http://i18next.com/pages/doc_init.html). This allows you to override pretty much anything. Read [i18next](http://i18next.com/) doc for more info. When using the CLI, `outputOverride` and `i18n` options are parsed as JSON. From 1583c9678cb59b65063293d75020da61a18c9f06 Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Sun, 15 Aug 2021 18:46:51 +0100 Subject: [PATCH 7/8] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9b9a377..5d1a14a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "static-i18n", - "version": "0.2.10", + "version": "0.2.11", "description": "Utility to translate static HTML files.", "main": "lib/index.js", "bin": { From 63ffbf2b389f87bcfbb227b7365cb7ee846a047b Mon Sep 17 00:00:00 2001 From: Ahmed Tounsi Date: Mon, 16 Aug 2021 10:11:22 +0100 Subject: [PATCH 8/8] Fix an issue with interpolation being escaped multiple times --- lib/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1133a5d..2962e23 100644 --- a/lib/index.js +++ b/lib/index.js @@ -110,11 +110,10 @@ function getOptions(baseOptions) { if (_.isUndefined(options.i18n.interpolation)){ options.i18n.interpolation = {} + options.i18n.interpolation.prefix = options.interpolationPrefix = _.escapeRegExp(options.interpolationPrefix); + options.i18n.interpolation.suffix = options.interpolationSuffix = _.escapeRegExp(options.interpolationSuffix); } - options.i18n.interpolation.prefix = options.interpolationPrefix = _.escapeRegExp(options.interpolationPrefix); - options.i18n.interpolation.suffix = options.interpolationSuffix = _.escapeRegExp(options.interpolationSuffix); - return options; }