Skip to content

Commit

Permalink
added mustacheConfig options (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Aug 3, 2020
1 parent 9c697b7 commit 2082e3b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 5 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ i18n.configure({
indent: "\t",

// setting extension of json files - defaults to '.json' (you might want to set this to '.js' according to webtranslateit)
extension: '.js',
extension: '.json',

// setting prefix of json files name - default to none '' (in case you use different locale files naming scheme (webapp-en.json), rather then just en.json)
prefix: 'webapp-',
Expand Down Expand Up @@ -208,15 +208,21 @@ i18n.configure({
},

// Downcase locale when passed on queryParam; e.g. lang=en-US becomes
// en-us. When set to false, the queryParam value will be used as passed;
// en-us. When set to false, the queryParam value will be used as passed;
// e.g. lang=en-US remains en-US.
preserveLegacyCase: true,

// set the language catalog statically
// also overrides locales
staticCatalog: {
de: { /* require('de.json') */ },
}
},

// use mustache with customTags (https://www.npmjs.com/package/mustache#custom-delimiters) or disable mustache entirely
mustacheConfig: {
tags: [ '{{', '}}' ],
disable: false
},
});
```

Expand Down
27 changes: 25 additions & 2 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var vsprintf = require('sprintf-js').vsprintf,
MakePlural = require('make-plural'),
parseInterval = require('math-interval-parser').default;

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

// exports an instance
module.exports = (function() {

Expand All @@ -39,6 +44,11 @@ module.exports = (function() {
'addLocale': 'addLocale',
'removeLocale': 'removeLocale'
},
mustacheConfig = {
tags: [ '{{', '}}' ],
disable: false
},
mustacheRegex,
pathsep = path.sep, // ---> means win support will be available in node 0.8.x and above
autoReload,
cookiename,
Expand Down Expand Up @@ -160,6 +170,19 @@ module.exports = (function() {
syncFiles = false;
}

// customize mustache parsing
if(opt.mustacheConfig) {
if(Array.isArray(opt.mustacheConfig.tags)){
mustacheConfig.tags = opt.mustacheConfig.tags;
}
if(opt.mustacheConfig.disable === true){
mustacheConfig.disable = true;
}
}

const [ start, end ] = mustacheConfig.tags;
mustacheRegex = new RegExp(escapeRegExp(start) + '.*' + escapeRegExp(end));

// implicitly read all locales
if (Array.isArray(opt.locales)) {

Expand Down Expand Up @@ -554,8 +577,8 @@ module.exports = (function() {
}

// if the msg string contains {{Mustache}} patterns we render it as a mini tempalate
if ((/{{.*}}/).test(msg)) {
msg = Mustache.render(msg, namedValues);
if (!mustacheConfig.disable && mustacheRegex.test(msg)) {
msg = Mustache.render(msg, namedValues, {}, mustacheConfig.tags);
}

// if we have extra arguments with values to get replaced,
Expand Down
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"async": "^3.2.0",
"cookie-parser": "^1.4.5",
"express": "^4.17.1",
"import-fresh": "^3.2.1",
"jshint": "^2.11.0",
"mocha": "^7.1.2",
"should": "*",
Expand Down
71 changes: 71 additions & 0 deletions test/i18n.configureMustache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var importFresh = require('import-fresh'),
i18n1 = importFresh('../i18n'),
i18n2 = importFresh('../i18n'),
i18n3 = importFresh('../i18n'),
i18n4 = importFresh('../i18n'),
path = require('path'),
should = require("should");

// cleanup for other tests
delete require.cache[path.resolve('i18n.js')];

describe('mustache configuration', function() {
const e = should.equal
const staticCatalog = {
'en': {
"Hello {{{name}}}": "Hello {{{name}}}",
"Hello {{name}}": "Hello {{name}}",
"Hello <%{name}%>": "Hello <%{name}%>",
"Hello <%name%>": "Hello <%name%>",
"Hello **{name}**": "Hello **{name}**",
"Hello **name**": "Hello **name**",
}
}
const name = 'Pudo & Moka'

i18n1.configure({ staticCatalog });
i18n2.configure({ staticCatalog, mustacheConfig: { tags: [ '<%', '%>' ] } });
i18n3.configure({ staticCatalog, mustacheConfig: { tags: [ '**', '**' ] } });
i18n4.configure({ staticCatalog, mustacheConfig: { disable: true } });

it('should parse with defaults', function(done) {
e(i18n1.__('Hello {{{name}}}', { name }), 'Hello Pudo & Moka')
e(i18n1.__('Hello {{name}}', { name }), 'Hello Pudo &amp; Moka')
e(i18n1.__('Hello <%{name}%>', { name }), 'Hello <%{name}%>')
e(i18n1.__('Hello <%name%>', { name }), 'Hello <%name%>')
e(i18n1.__('Hello **{name}**', { name }), 'Hello **{name}**')
e(i18n1.__('Hello **name**', { name }), 'Hello **name**')
done();
});

it('should parse with customTags', function(done) {
e(i18n2.__('Hello {{{name}}}', { name }), 'Hello {{{name}}}')
e(i18n2.__('Hello {{name}}', { name }), 'Hello {{name}}')
e(i18n2.__('Hello <%{name}%>', { name }), 'Hello Pudo & Moka')
e(i18n2.__('Hello <%name%>', { name }), 'Hello Pudo &amp; Moka')
e(i18n2.__('Hello **{name}**', { name }), 'Hello **{name}**')
e(i18n2.__('Hello **name**', { name }), 'Hello **name**')
done();
});

it('should parse with customTags (regex safe)', function(done) {
e(i18n3.__('Hello {{{name}}}', { name }), 'Hello {{{name}}}')
e(i18n3.__('Hello {{name}}', { name }), 'Hello {{name}}')
e(i18n3.__('Hello <%{name}%>', { name }), 'Hello <%{name}%>')
e(i18n3.__('Hello <%name%>', { name }), 'Hello <%name%>')
e(i18n3.__('Hello **{name}**', { name }), 'Hello Pudo & Moka')
e(i18n3.__('Hello **name**', { name }), 'Hello Pudo &amp; Moka')
done();
});

it('should ignore mustache tags when opted-out', function(done) {
e(i18n4.__('Hello {{{name}}}', { name }), 'Hello {{{name}}}')
e(i18n4.__('Hello {{name}}', { name }), 'Hello {{name}}')
e(i18n4.__('Hello <%{name}%>', { name }), 'Hello <%{name}%>')
e(i18n4.__('Hello <%name%>', { name }), 'Hello <%name%>')
e(i18n4.__('Hello **{name}**', { name }), 'Hello **{name}**')
e(i18n4.__('Hello **name**', { name }), 'Hello **name**')
done();
});

});

0 comments on commit 2082e3b

Please sign in to comment.