Skip to content

Commit

Permalink
⚡ improvement(pluralization): zero choice (#70) by @sebwas
Browse files Browse the repository at this point in the history
* Write test for zero choice

* Add zero choice to translate choice, providing backwards compatibility
  • Loading branch information
sebwas authored and kazupon committed Oct 20, 2016
1 parent 328bffc commit 5f0004f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Path from './path'

/**
* extend
*
*
* @param {Vue} Vue
* @return {Vue}
*/
Expand Down Expand Up @@ -69,13 +69,13 @@ export default function (Vue) {

function translate (getter, lang, fallback, key, params) {
let res = null
res = interpolate(getter(lang), key, params)
if (res) { return res }
res = interpolate(getter(lang), key, params)
if (res) { return res }

res = interpolate(getter(fallback), key, params)
res = interpolate(getter(fallback), key, params)
if (res) {
if (process.env.NODE_ENV !== 'production') {
warn('Fall back to translate the keypath "' + key + '" with "'
warn('Fall back to translate the keypath "' + key + '" with "'
+ fallback + '" language.')
}
return res
Expand All @@ -102,10 +102,23 @@ export default function (Vue) {
return this.$options.locales[lang]
}

function getOldChoiceIndexFixed (choice) {
return choice ? choice > 1 ? 1 : 0 : 1
}

function getChoiceIndex (choice, choicesLength) {
choice = Math.abs(choice)

if (choicesLength === 2) return getOldChoiceIndexFixed(choice)

return choice ? Math.min(choice, 2) : 0
}

function fetchChoice (locale, choice) {
if (!locale && typeof locale !== 'string') { return null }
const choices = locale.split('|')
choice = choice - 1

choice = getChoiceIndex(choice, choices.length)
if (!choices[choice]) { return locale }
return choices[choice].trim()
}
Expand Down Expand Up @@ -135,7 +148,6 @@ export default function (Vue) {
*/

Vue.tc = (key, choice, ...args) => {
if (!choice) { choice = 1 }
return fetchChoice(Vue.t(key, ...args), choice)
}

Expand Down Expand Up @@ -173,7 +185,6 @@ export default function (Vue) {
Vue.prototype.$tc = function (key, choice, ...args) {
if (typeof choice !== 'number'
&& typeof choice !== 'undefined') { return key }
if (!choice) { choice = 1 }
return fetchChoice(this.$t(key, ...args), choice)
}

Expand Down
1 change: 1 addition & 0 deletions test/specs/fixture/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default {
underscore: '{hello_msg} world',
plurals: {
car: 'car | cars',
apple: 'no apples | one apple | {count} apples',
format: {
named: 'Hello {name}, how are you? | Hi {name}, you look fine',
list: 'Hello {0}, how are you? | Hi {0}, you look fine'
Expand Down
10 changes: 10 additions & 0 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ describe('i18n', () => {
})

describe('Vue.tc', () => {
describe('split plural with zero choice', () => {
it('should allow a zero choice, a one choice and a plural choice', () => {
const count = 10

assert.equal(Vue.tc('plurals.apple', 0), 'no apples')
assert.equal(Vue.tc('plurals.apple', 1), 'one apple')
assert.equal(Vue.tc('plurals.apple', count, { count }), '10 apples')
})
})

describe('en language locale', () => {
it('should translate an english', () => {
assert.equal(Vue.tc('plurals.car', 1), 'car')
Expand Down

0 comments on commit 5f0004f

Please sign in to comment.