From 3dc6095289935f98e571d3dce97ae6d99fc70f5e Mon Sep 17 00:00:00 2001 From: SirLamer Date: Sat, 10 Mar 2018 11:11:33 -0700 Subject: [PATCH] :star: new(directive): Add pluralization feature to directive (#304) by @SirLamer * Add "choice" quantity support to v-t directive * Add unit tests * Revert dist changes --- src/directive.js | 12 ++++++++--- test/unit/directive.test.js | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/directive.js b/src/directive.js index 8f04cf616..cb9b70c2e 100644 --- a/src/directive.js +++ b/src/directive.js @@ -39,7 +39,7 @@ function localeEqual (el: any, vnode: any): boolean { function t (el: any, binding: Object, vnode: any): void { const value: any = binding.value - const { path, locale, args } = parseValue(value) + const { path, locale, args, choice } = parseValue(value) if (!path && !locale && !args) { warn('not support value type') return @@ -51,7 +51,11 @@ function t (el: any, binding: Object, vnode: any): void { } const vm: any = vnode.context - el._vt = el.textContent = vm.$i18n.t(path, ...makeParams(locale, args)) + if (choice) { + el._vt = el.textContent = vm.$i18n.tc(path, choice, ...makeParams(locale, args)) + } else { + el._vt = el.textContent = vm.$i18n.t(path, ...makeParams(locale, args)) + } el._locale = vm.$i18n.locale } @@ -59,6 +63,7 @@ function parseValue (value: any): Object { let path: ?string let locale: ?Locale let args: any + let choice: ?number if (typeof value === 'string') { path = value @@ -66,9 +71,10 @@ function parseValue (value: any): Object { path = value.path locale = value.locale args = value.args + choice = value.choice } - return { path, locale, args } + return { path, locale, args, choice } } function makeParams (locale: Locale, args: any): Array { diff --git a/test/unit/directive.test.js b/test/unit/directive.test.js index 58654b0d4..0d7e9f174 100644 --- a/test/unit/directive.test.js +++ b/test/unit/directive.test.js @@ -157,5 +157,47 @@ describe('custom directive', () => { }).then(done) }) }) + + describe('pluralize', () => { + it('should be singular', done => { + const vm = createVM({ + i18n, + render (h) { + //

+ return h('p', { ref: 'text', directives: [{ + name: 't', rawName: 'v-t', value: ({ path: 'plurals.car', choice: 1 }), expression: { path: 'plurals.car', choice: 1 } + }] }) + } + }) + nextTick(() => { + assert.equal(vm.$refs.text.textContent, 'car') + assert.equal(vm.$refs.text._vt, 'car') + vm.$forceUpdate() + }).then(() => { + assert.equal(vm.$refs.text.textContent, 'car') + assert.equal(vm.$refs.text._vt, 'car') + }).then(done) + }) + + it('should be plural', done => { + const vm = createVM({ + i18n, + render (h) { + //

+ return h('p', { ref: 'text', directives: [{ + name: 't', rawName: 'v-t', value: ({ path: 'plurals.car', choice: 2 }), expression: { path: 'plurals.car', choice: 2 } + }] }) + } + }) + nextTick(() => { + assert.equal(vm.$refs.text.textContent, 'cars') + assert.equal(vm.$refs.text._vt, 'cars') + vm.$forceUpdate() + }).then(() => { + assert.equal(vm.$refs.text.textContent, 'cars') + assert.equal(vm.$refs.text._vt, 'cars') + }).then(done) + }) + }) }) })