-
-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐ new(directives): support v-t custom directive (welcome back!)
- Loading branch information
Showing
4 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* @flow */ | ||
|
||
import { warn, isPlainObject, looseEqual } from './util' | ||
|
||
export function bind (el: any, binding: Object, vnode: any): void { | ||
t(el, binding, vnode) | ||
} | ||
|
||
export function update (el: any, binding: Object, vnode: any, oldVNode: any): void { | ||
if (looseEqual(binding.value, binding.oldValue)) { return } | ||
|
||
t(el, binding, vnode) | ||
} | ||
|
||
function t (el: any, binding: Object, vnode: any): void { | ||
const value: any = binding.value | ||
|
||
const { path, locale, args } = parseValue(value) | ||
if (!path && !locale && !args) { | ||
warn('not support value type') | ||
return | ||
} | ||
|
||
const vm: any = vnode.context | ||
if (!vm) { | ||
warn('not exist Vue instance in VNode context') | ||
return | ||
} | ||
|
||
if (!vm.$i18n) { | ||
warn('not exist VueI18n instance in Vue instance') | ||
return | ||
} | ||
|
||
if (!path) { | ||
warn('required `path` in v-t directive') | ||
return | ||
} | ||
|
||
el._vt = el.textContent = vm.$i18n.t(path, ...makeParams(locale, args)) | ||
} | ||
|
||
function parseValue (value: any): Object { | ||
let path: ?string | ||
let locale: ?Locale | ||
let args: any | ||
|
||
if (typeof value === 'string') { | ||
path = value | ||
} else if (isPlainObject(value)) { | ||
path = value.path | ||
locale = value.locale | ||
args = value.args | ||
} | ||
|
||
return { path, locale, args } | ||
} | ||
|
||
function makeParams (locale: Locale, args: any): Array<any> { | ||
const params: Array<any> = [] | ||
|
||
locale && params.push(locale) | ||
if (args && (Array.isArray(args) || isPlainObject(args))) { | ||
params.push(args) | ||
} | ||
|
||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import messages from './fixture/index' | ||
|
||
describe('custom directive', () => { | ||
let i18n | ||
beforeEach(() => { | ||
i18n = new VueI18n({ | ||
locale: 'en', | ||
messages | ||
}) | ||
}) | ||
|
||
function createVM (options) { | ||
const el = document.createElement('div') | ||
return new Vue(options).$mount(el) | ||
} | ||
|
||
describe('v-t', () => { | ||
describe('string literal', () => { | ||
it('should be translated', done => { | ||
const vm = createVM({ | ||
i18n, | ||
render (h) { | ||
// <p ref="text" v-t="'message.hello'"></p> | ||
return h('p', { ref: 'text', directives: [{ | ||
name: 't', rawName: 'v-t', value: ('message.hello'), expression: "'message.hello'" | ||
}] }) | ||
} | ||
}) | ||
nextTick(() => { | ||
assert.equal(vm.$refs.text.textContent, messages.en.message.hello) | ||
assert.equal(vm.$refs.text._vt, messages.en.message.hello) | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('object', () => { | ||
it('should be translated', done => { | ||
const vm = createVM({ | ||
i18n, | ||
data: { | ||
msgPath: 'message.format.named' | ||
}, | ||
render (h) { | ||
// <p ref="text" v-t="{ path: msgPath, locale: 'ja', args: { name: 'kazupon' } }"></p> | ||
return h('p', { ref: 'text', directives: [{ | ||
name: 't', rawName: 'v-t', | ||
value: ({ path: this.msgPath, locale: 'ja', args: { name: 'kazupon' } }), | ||
expression: "{ path: msgPath, locale: 'ja', args: { name: 'kazupon' } }" | ||
}] }) | ||
} | ||
}) | ||
nextTick(() => { | ||
const expected = 'こんにちは kazupon, ごきげんいかが?' | ||
assert.equal(vm.$refs.text.textContent, expected) | ||
assert.equal(vm.$refs.text._vt, expected) | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('not support warning', () => { | ||
it('should be warned', done => { | ||
const spy = sinon.spy(console, 'warn') | ||
createVM({ | ||
i18n, | ||
render (h) { | ||
// <p ref="text" v-t="[1]"></p> | ||
return h('p', { ref: 'text', directives: [{ | ||
name: 't', rawName: 'v-t', value: ([1]), expression: '[1]' | ||
}] }) | ||
} | ||
}) | ||
nextTick(() => { | ||
assert(spy.notCalled === false) | ||
assert(spy.callCount === 1) | ||
spy.restore() | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('path required warning', () => { | ||
it('should be warned', done => { | ||
const spy = sinon.spy(console, 'warn') | ||
createVM({ | ||
i18n, | ||
render (h) { | ||
// <p ref="text" v-t="{ locale: 'ja', args: { name: 'kazupon' } }"></p> | ||
return h('p', { ref: 'text', directives: [{ | ||
name: 't', rawName: 'v-t', | ||
value: ({ locale: 'ja', args: { name: 'kazupon' } }), | ||
expression: "{ locale: 'ja', args: { name: 'kazupon' } }" | ||
}] }) | ||
} | ||
}) | ||
nextTick(() => { | ||
assert(spy.notCalled === false) | ||
assert(spy.callCount === 1) | ||
spy.restore() | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('VueI18n instance warning', () => { | ||
it('should be warned', done => { | ||
const spy = sinon.spy(console, 'warn') | ||
createVM({ | ||
render (h) { | ||
return h('p', { ref: 'text', directives: [{ | ||
name: 't', rawName: 'v-t', value: ('message.hello'), expression: "'message.hello'" | ||
}] }) | ||
} | ||
}) | ||
nextTick(() => { | ||
assert(spy.notCalled === false) | ||
assert(spy.callCount === 1) | ||
spy.restore() | ||
}).then(done) | ||
}) | ||
}) | ||
}) | ||
}) |