From e84f0fbd0d66eed93b5f0cb07e34aec75e285706 Mon Sep 17 00:00:00 2001 From: TATSUNO Yasuhiro Date: Sun, 14 Oct 2018 03:15:25 +0900 Subject: [PATCH] :zap: improvement(index): Pre-defined named arguments for Pluraization (#440) by @exoego * Add implicit "choice" exposing * Add doc * Rename variables * Better title --- src/index.js | 5 ++++ test/unit/basic.test.js | 24 ++++++++++++++++++ test/unit/fixture/index.js | 1 + vuepress/guide/pluralization.md | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/src/index.js b/src/index.js index dcebb5ddc..7188029ec 100644 --- a/src/index.js +++ b/src/index.js @@ -400,6 +400,11 @@ export default class VueI18n { if (choice === undefined) { choice = 1 } + + const predefined = { 'count': choice, 'n': choice } + const parsedArgs = parseArgs(...values) + parsedArgs.params = Object.assign(predefined, parsedArgs.params) + values = parsedArgs.locale === null ? [parsedArgs.params] : [parsedArgs.locale, parsedArgs.params] return fetchChoice(this._t(key, _locale, messages, host, ...values), choice) } diff --git a/test/unit/basic.test.js b/test/unit/basic.test.js index 3527f6da8..7342d84fa 100644 --- a/test/unit/basic.test.js +++ b/test/unit/basic.test.js @@ -191,6 +191,30 @@ describe('basic', () => { }) }) + describe('implicit choice exposing', () => { + describe('en locale', () => { + it('should expose "count" implicitly to locale message', () => { + assert.strictEqual(i18n.tc('plurals.apple', 10), '10 apples') + }) + + it('should not expose if given explicitly', () => { + const explicitArgs = { 'count': 'Many' } + assert.strictEqual(i18n.tc('plurals.apple', 10, explicitArgs), 'Many apples') + }) + }) + + describe('ja locale', () => { + it('should expose "count" and "n" implicitly to locale message', () => { + assert.strictEqual(i18n.tc('plurals.implicitPluralCount', 10, 'ja'), 'count:10, n:10') + }) + + it('should not expose if given explicitly', () => { + const explicitArgs = { 'count': 'たくさん', 'n': '大量' } + assert.strictEqual(i18n.tc('plurals.implicitPluralCount', 10, 'ja', explicitArgs), 'count:たくさん, n:大量') + }) + }) + }) + describe('en locale', () => { it('should translate an english', () => { assert.equal(i18n.tc('plurals.car', 1), 'car') diff --git a/test/unit/fixture/index.js b/test/unit/fixture/index.js index d8cfc995a..1271cc96b 100644 --- a/test/unit/fixture/index.js +++ b/test/unit/fixture/index.js @@ -71,6 +71,7 @@ export default { }, plurals: { car: 'ザ・ワールド | これはフォールバック', + implicitPluralCount: 'こっちには来ない | count:{count}, n:{n}', format: { named: 'こんにちは {name}, ごきげんいかが? | こんにちは {name}, ごきげんいかが?', list: 'こんにちは {0}, ごきげんいかが?| こんにちは {0}, ごきげんいかが?' diff --git a/vuepress/guide/pluralization.md b/vuepress/guide/pluralization.md index 798df4629..ee015d591 100644 --- a/vuepress/guide/pluralization.md +++ b/vuepress/guide/pluralization.md @@ -34,3 +34,47 @@ Output the below:

one apple

10 apples

``` + +## Accessing the number via the pre-defined argument + +You don't need to explicitly give the number for pluralization. +The number can be accessed within locale messages via pre-defined named arguments `{count}` and/or `{n}`. +You can overwrite those pre-defined named arguments if necessary. + +Locale messages the below: + +```js +const messages = { + en: { + apple: 'no apples | one apple | {count} apples', + banana: 'no bananas | {n} banana | {n} bananas' + } +} +``` + +Template the below: + +```html +

{{ $tc('apple', 10, { count: 10 }) }}

+

{{ $tc('apple', 10) }}

+ +

{{ $tc('banana', 1, { n: 1 }) }}

+

{{ $tc('banana', 1) }}

+

{{ $tc('banana', 100, { n: 'too much' }) }}

+``` + +Output the below: + +```html +

10 apples

+

10 apples

+ +

1 banana

+

1 banana

+

too much bananas

+``` + + + + +