Skip to content

Commit

Permalink
⚡ improvement(index): Pre-defined named arguments for Pluraization (#440
Browse files Browse the repository at this point in the history
) by @exoego

* Add implicit "choice" exposing

* Add doc

* Rename variables

* Better title
  • Loading branch information
TATSUNO Yasuhiro authored and kazupon committed Oct 13, 2018
1 parent 7583485 commit e84f0fb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
24 changes: 24 additions & 0 deletions test/unit/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions test/unit/fixture/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default {
},
plurals: {
car: 'ザ・ワールド | これはフォールバック',
implicitPluralCount: 'こっちには来ない | count:{count}, n:{n}',
format: {
named: 'こんにちは {name}, ごきげんいかが? | こんにちは {name}, ごきげんいかが?',
list: 'こんにちは {0}, ごきげんいかが?| こんにちは {0}, ごきげんいかが?'
Expand Down
44 changes: 44 additions & 0 deletions vuepress/guide/pluralization.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,47 @@ Output the below:
<p>one apple</p>
<p>10 apples</p>
```

## 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
<p>{{ $tc('apple', 10, { count: 10 }) }}</p>
<p>{{ $tc('apple', 10) }}</p>

<p>{{ $tc('banana', 1, { n: 1 }) }}</p>
<p>{{ $tc('banana', 1) }}</p>
<p>{{ $tc('banana', 100, { n: 'too much' }) }}</p>
```

Output the below:

```html
<p>10 apples</p>
<p>10 apples</p>

<p>1 banana</p>
<p>1 banana</p>
<p>too much bananas</p>
```





0 comments on commit e84f0fb

Please sign in to comment.