Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 5f0004f

Browse files
sebwaskazupon
authored andcommitted
⚡ improvement(pluralization): zero choice (#70) by @sebwas
* Write test for zero choice * Add zero choice to translate choice, providing backwards compatibility
1 parent 328bffc commit 5f0004f

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/extend.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Path from './path'
55

66
/**
77
* extend
8-
*
8+
*
99
* @param {Vue} Vue
1010
* @return {Vue}
1111
*/
@@ -69,13 +69,13 @@ export default function (Vue) {
6969

7070
function translate (getter, lang, fallback, key, params) {
7171
let res = null
72-
res = interpolate(getter(lang), key, params)
73-
if (res) { return res }
72+
res = interpolate(getter(lang), key, params)
73+
if (res) { return res }
7474

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

105+
function getOldChoiceIndexFixed (choice) {
106+
return choice ? choice > 1 ? 1 : 0 : 1
107+
}
108+
109+
function getChoiceIndex (choice, choicesLength) {
110+
choice = Math.abs(choice)
111+
112+
if (choicesLength === 2) return getOldChoiceIndexFixed(choice)
113+
114+
return choice ? Math.min(choice, 2) : 0
115+
}
116+
105117
function fetchChoice (locale, choice) {
106118
if (!locale && typeof locale !== 'string') { return null }
107119
const choices = locale.split('|')
108-
choice = choice - 1
120+
121+
choice = getChoiceIndex(choice, choices.length)
109122
if (!choices[choice]) { return locale }
110123
return choices[choice].trim()
111124
}
@@ -135,7 +148,6 @@ export default function (Vue) {
135148
*/
136149

137150
Vue.tc = (key, choice, ...args) => {
138-
if (!choice) { choice = 1 }
139151
return fetchChoice(Vue.t(key, ...args), choice)
140152
}
141153

@@ -173,7 +185,6 @@ export default function (Vue) {
173185
Vue.prototype.$tc = function (key, choice, ...args) {
174186
if (typeof choice !== 'number'
175187
&& typeof choice !== 'undefined') { return key }
176-
if (!choice) { choice = 1 }
177188
return fetchChoice(this.$t(key, ...args), choice)
178189
}
179190

test/specs/fixture/locales.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default {
2020
underscore: '{hello_msg} world',
2121
plurals: {
2222
car: 'car | cars',
23+
apple: 'no apples | one apple | {count} apples',
2324
format: {
2425
named: 'Hello {name}, how are you? | Hi {name}, you look fine',
2526
list: 'Hello {0}, how are you? | Hi {0}, you look fine'

test/specs/i18n.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ describe('i18n', () => {
154154
})
155155

156156
describe('Vue.tc', () => {
157+
describe('split plural with zero choice', () => {
158+
it('should allow a zero choice, a one choice and a plural choice', () => {
159+
const count = 10
160+
161+
assert.equal(Vue.tc('plurals.apple', 0), 'no apples')
162+
assert.equal(Vue.tc('plurals.apple', 1), 'one apple')
163+
assert.equal(Vue.tc('plurals.apple', count, { count }), '10 apples')
164+
})
165+
})
166+
157167
describe('en language locale', () => {
158168
it('should translate an english', () => {
159169
assert.equal(Vue.tc('plurals.car', 1), 'car')

0 commit comments

Comments
 (0)