Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make plural handling more loose #201

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions packages/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@
"node": ">=14.0.0"
},
"devDependencies": {
"@babel/core": "^7.17.10",
"@babel/plugin-transform-runtime": "^7.17.10",
"@babel/preset-env": "^7.17.10",
"@babel/runtime": "^7.17.9",
"@babel/core": "^7.23.5",
"@babel/plugin-transform-runtime": "^7.23.4",
"@babel/preset-env": "^7.23.5",
"@babel/runtime": "^7.23.5",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.5",
"chai": "^4.3.4",
"babel-loader": "^8.3.0",
"chai": "^4.3.10",
"eslint": "^7.28.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-import": "^2.26.0",
"glob": "^8.0.1",
"mocha": "^10.0.0",
"nock": "^13.1.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.29.0",
"glob": "^8.1.0",
"mocha": "^10.2.0",
"nock": "^13.4.0",
"nyc": "^15.1.0",
"source-map-support": "^0.5.19",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2"
"source-map-support": "^0.5.21",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@messageformat/core": "^3.0.0",
"axios": "^0.27.2",
"@messageformat/core": "^3.3.0",
"axios": "^1.6.2",
"md5": "^2.3.0"
}
}
8 changes: 6 additions & 2 deletions packages/native/src/renderers/MessageFormatRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ export default class MessageFormatRenderer {
const locale = ((localeCode || '').split('_'))[0];
if (!MF[locale]) {
try {
MF[locale] = new MessageFormat(locale);
MF[locale] = new MessageFormat(locale, {
strictPluralKeys: false,
});
} catch (err) {
MF[locale] = new MessageFormat();
MF[locale] = new MessageFormat('*', {
strictPluralKeys: false,
});
}
}
const msg = MF[locale].compile(sourceString);
Expand Down
11 changes: 11 additions & 0 deletions packages/native/tests/plurals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('explodePlurals', () => {
testExplode('{cnt, plural, =1 {hello world} other {hello worlds}}',
['cnt', { one: 'hello world', other: 'hello worlds' }]);
});

it('should not explode with leading/tailing spaces', () => {
testExplode(
' {cnt, plural, one {hello world} other {hello worlds}}',
Expand All @@ -42,6 +43,7 @@ describe('explodePlurals', () => {
[null, { other: '{cnt, plural, one {hello world} other {hello worlds}} ' }],
);
});

it('should fail when missing opening/closing brackets', () => {
testExplode(
'cnt, plural, one {hello world} other {hello worlds}}',
Expand All @@ -52,6 +54,7 @@ describe('explodePlurals', () => {
[null, { other: '{cnt, plural, one {hello world} other {hello worlds}' }],
);
});

it('should fail if message is incomplete', () => {
testExplode('{cnt}', [null, { other: '{cnt}' }]);
testExplode('{cnt, }', [null, { other: '{cnt, }' }]);
Expand All @@ -63,6 +66,7 @@ describe('explodePlurals', () => {
testExplode('{cnt, plural, one hello world}',
[null, { other: '{cnt, plural, one hello world}' }]);
});

it('should fail if variable, plural keyword or rules are wrong', () => {
testExplode(
'{cn t, plural, one {hello world} other {hello worlds}}',
Expand Down Expand Up @@ -97,14 +101,17 @@ describe('explodePlurals', () => {
[null, { other: '{cnt, plural, one {hello world} o ther {hello worlds}}' }],
);
});

it('should fail on missing plural strings', () => {
testExplode('{cnt, plural, one', [null, { other: '{cnt, plural, one' }]);
testExplode('{cnt, plural, one}', [null, { other: '{cnt, plural, one}' }]);
});

it('should handle nested brackets', () => {
testExplode('{cnt, plural, one {hello {world}} other {hello worlds}}',
['cnt', { one: 'hello {world}', other: 'hello worlds' }]);
});

it("should fail when minimum plural rules don't exist", () => {
testExplode('{cnt, plural, one {hello world}}',
[null, { other: '{cnt, plural, one {hello world}}' }]);
Expand All @@ -113,6 +120,7 @@ describe('explodePlurals', () => {
[null, { other: '{cnt, plural, few {hello world} other {hello worlds}}' }],
);
});

it('should propertly escape stuff with apostrophes', () => {
testExplode("{cnt, plural, one {hello '{'world'}'} other {hello worlds}}",
['cnt', { one: "hello '{'world'}'", other: 'hello worlds' }]);
Expand All @@ -139,16 +147,19 @@ describe('implodePlurals', () => {
expect(implodePlurals({ one: 'hello world', two: 'hello worlds' })).to
.equal('{???, plural, one {hello world} two {hello worlds}}');
});

it('should respect custom count variable', () => {
expect(implodePlurals({ zero: 'hello world' }, 'count')).to
.equal('{count, plural, zero {hello world}}');
});

it('should respect order', () => {
expect(implodePlurals({ few: 'hello world', many: 'hello worlds' })).to
.equal('{???, plural, few {hello world} many {hello worlds}}');
expect(implodePlurals({ many: 'hello worlds', few: 'hello world' })).to
.equal('{???, plural, few {hello world} many {hello worlds}}');
});

it('should ignore unknown rules', () => {
expect(implodePlurals({ other: 'hello world', six: 'hello worlds' })).to
.equal('{???, plural, other {hello world}}');
Expand Down
17 changes: 17 additions & 0 deletions packages/native/tests/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ describe('String renderer', () => {
t = tx.translate.bind(tx);
});

it('renders plurals', () => {
expect(tx.stringRenderer.render('{cnt, plural, one {# hello} other {# hellos}}', '', { cnt: 1 }))
.to.equal('1 hello');
expect(tx.stringRenderer.render('{cnt, plural, one {# hello} other {# hellos}}', '', { cnt: 0 }))
.to.equal('0 hellos');
expect(tx.stringRenderer.render('{cnt, plural, one {# one hello} two {# two hellos} many {# many hellos} other {# other hellos}}', '', { cnt: 2 }))
.to.equal('2 other hellos');
expect(tx.stringRenderer.render('{cnt, plural, one {# one hello} two {# two hellos} many {# many hellos} other {# other hellos}}', 'he', { cnt: 2 }))
.to.equal('2 two hellos');
expect(tx.stringRenderer.render('{cnt, plural, one {תחנה אחת} two {# תחנות} many {# תחנות} other {# תחנות}}', 'he', { cnt: 0 }))
.to.equal('0 תחנות');
expect(tx.stringRenderer.render('{cnt, plural, one {תחנה אחת} two {# תחנות} many {# תחנות} other {# תחנות}}', 'he', { cnt: 1 }))
.to.equal('תחנה אחת');
expect(tx.stringRenderer.render('{cnt, plural, one {תחנה אחת} two {# תחנות} many {# תחנות} other {# תחנות}}', 'he', { cnt: 2 }))
.to.equal('2 תחנות');
});

it('renders with localized dates', async () => {
const d = Date.parse('2020-02-01');

Expand Down
Loading