Skip to content

Commit

Permalink
Merge pull request #435 from matthub/master
Browse files Browse the repository at this point in the history
Supporting standalone pipe characters
  • Loading branch information
mashpie committed Aug 3, 2020
2 parents fb9aa22 + 06be228 commit dd2ee76
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
11 changes: 8 additions & 3 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,19 +800,24 @@ module.exports = (function() {
var parsePluralInterval = function(phrase, count) {
var returnPhrase = phrase;
var phrases = phrase.split(/\|/);
var intervalRuleExists = false;

// some() breaks on 1st true
phrases.some(function(p) {
var matches = p.match(/^\s*([\(\)\[\]\d,]+)?\s*(.*)$/);
var matches = p.match(/^\s*([\(\)\[\]]+[\d,]+[\(\)\[\]]+)?\s*(.*)$/);

// not the same as in combined condition
if (matches[1]) {
if (matches != null && matches[1]) {
intervalRuleExists = true;
if (matchInterval(count, matches[1]) === true) {
returnPhrase = matches[2];
return true;
}
} else {
returnPhrase = p;
// this is a other or catch all case, this only is taken into account if there is actually another rule
if (intervalRuleExists) {
returnPhrase = p;
}
}

});
Expand Down
4 changes: 3 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,7 @@
". is first character": "Dot is first character",
"last character is .": "last character is Dot",
"few sentences. with .": "few sentences with Dot",
"Hello {{{name}}}": "Hello {{{name}}}"
"Hello {{{name}}}": "Hello {{{name}}}",
"Standalone | 42 symbol somewhere | in the text | 1| 0": "Standalone | 42 symbol somewhere | in the text | 1| 0",
"should ignore \n standalone | mixed with \n new lines 42 | value - 42": "should ignore \n standalone | mixed with \n new lines 42 | value - 42"
}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "i18n",
"description": "lightweight translation module with dynamic json storage",
"version": "0.10.0",
"version": "0.10.1",
"homepage": "http://github.com/mashpie/i18n-node",
"repository": {
"type": "git",
Expand Down
19 changes: 18 additions & 1 deletion test/i18n.plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ i18n.setLocale(pluralTest, 'en');

describe('parsing plural intervals from strings', function() {

// ignoring pipe symbols without interval rules, see #274
it('should ignore standalone | symbols', function() {
const standalone = 'Standalone | 42 symbol somewhere | in the text | 1| 0';
should.equal(
pluralTest.__(standalone),
standalone
);
});

it('should ignore mixed pipe and newline symbols', function() {
const standalone = 'should ignore \n standalone | mixed with \n new lines 42 | value - 42';
should.equal(
pluralTest.__(standalone),
standalone
);
});

it('should work with classic format too', function() {
should.equal(
"There are 3 monkeys in the tree",
Expand Down Expand Up @@ -381,4 +398,4 @@ describe('parsing plural intervals from strings', function() {

});

});
});

0 comments on commit dd2ee76

Please sign in to comment.