Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pluralization feature to directive #304

Merged
merged 3 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -51,24 +51,30 @@ 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
}

function parseValue (value: any): Object {
let path: ?string
let locale: ?Locale
let args: any
let choice: ?number

if (typeof value === 'string') {
path = value
} else if (isPlainObject(value)) {
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<any> {
Expand Down
42 changes: 42 additions & 0 deletions test/unit/directive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,47 @@ describe('custom directive', () => {
}).then(done)
})
})

describe('pluralize', () => {
it('should be singular', done => {
const vm = createVM({
i18n,
render (h) {
// <p ref="text" v-t="{path: 'plurals.car', choice: 1}"></p>
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) {
// <p ref="text" v-t="{path: 'plurals.car', choice: 2}"></p>
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)
})
})
})
})