-
-
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.
- Loading branch information
Showing
5 changed files
with
270 additions
and
16 deletions.
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
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,33 @@ | ||
/* @flow */ | ||
|
||
import { warn } from './util' | ||
|
||
export default { | ||
name: 'i18n', | ||
functional: true, | ||
props: { | ||
path: { | ||
type: String, | ||
required: true | ||
}, | ||
locale: { | ||
type: String | ||
} | ||
}, | ||
render (h: Function, { props, children, parent }: Object) { | ||
const i18n = parent.$i18n | ||
if (!i18n) { | ||
warn('Cannot find VueI18n instance!') | ||
return children | ||
} | ||
|
||
const path: Path = props.path | ||
const locale: ?Locale = props.locale | ||
|
||
const params: Array<any> = [] | ||
locale && params.push(locale) | ||
children.forEach(child => params.push(child)) | ||
|
||
return i18n.i(path, ...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,161 @@ | ||
const messages = { | ||
en: { | ||
text: 'one: {0}', | ||
premitive: 'one: {0}, two: {1}', | ||
component: 'root: {0}, component: {1}', | ||
link: '@:premitive' | ||
}, | ||
ja: { | ||
text: '一: {0}', | ||
} | ||
} | ||
const components = { | ||
comp: { | ||
props: { | ||
msg: { type: String, default: '' } | ||
}, | ||
render (h) { | ||
return h('p', [this.msg]) | ||
} | ||
} | ||
} | ||
|
||
describe('component interpolation', () => { | ||
let i18n | ||
beforeEach(() => { | ||
i18n = new VueI18n({ | ||
locale: 'en', | ||
messages | ||
}) | ||
}) | ||
|
||
describe('children', () => { | ||
describe('text nodes', () => { | ||
it('should be interpolated', done => { | ||
const el = document.createElement('div') | ||
const vm = new Vue({ | ||
i18n, | ||
render (h) { | ||
return h('p', {}, [ | ||
h('i18n', { props: { path: 'text' } }, [ | ||
this._v('1') | ||
]) | ||
]) | ||
} | ||
}).$mount(el) | ||
nextTick(() => { | ||
assert.equal(vm.$el.textContent, 'one: 1') | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('premitive nodes', () => { | ||
it('should be interpolated', done => { | ||
const el = document.createElement('div') | ||
const vm = new Vue({ | ||
i18n, | ||
render (h) { | ||
return h('div', {}, [ | ||
h('i18n', { props: { path: 'premitive' } }, [ | ||
h('p', ['1']), | ||
h('p', ['2']) | ||
]) | ||
]) | ||
} | ||
}).$mount(el) | ||
nextTick(() => { | ||
assert.equal(vm.$el.innerHTML, 'one: <p>1</p>, two: <p>2</p>') | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('components', () => { | ||
it('should be interpolated', done => { | ||
const el = document.createElement('div') | ||
const vm = new Vue({ | ||
i18n, | ||
components, | ||
render (h) { | ||
return h('div', {}, [ | ||
h('i18n', { props: { path: 'component' } }, [ | ||
h('p', ['1']), | ||
h('comp', { props: { msg: 'foo' } }) | ||
]) | ||
]) | ||
} | ||
}).$mount(el) | ||
nextTick(() => { | ||
assert.equal(vm.$el.innerHTML, 'root: <p>1</p>, component: <p>foo</p>') | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('nested components', () => { | ||
it('should be interpolated', done => { | ||
const el = document.createElement('div') | ||
const vm = new Vue({ | ||
i18n, | ||
components, | ||
render (h) { | ||
return h('div', {}, [ | ||
h('i18n', { props: { path: 'component' } }, [ | ||
h('p', ['1']), | ||
h('div', {}, [ | ||
h('i18n', { props: { path: 'component' } }, [ | ||
h('p', ['2']), | ||
h('comp', { props: { msg: 'nested' } }) | ||
]) | ||
]) | ||
]) | ||
]) | ||
} | ||
}).$mount(el) | ||
nextTick(() => { | ||
assert.equal( | ||
vm.$el.innerHTML, | ||
'root: <p>1</p>, component: <div>root: <p>2</p>, component: <p>nested</p></div>' | ||
) | ||
}).then(done) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('linked', () => { | ||
it('should be interpolated', done => { | ||
const el = document.createElement('div') | ||
const vm = new Vue({ | ||
i18n, | ||
render (h) { | ||
return h('p', {}, [ | ||
h('i18n', { props: { path: 'link' } }, [ | ||
h('p', ['1']), | ||
h('p', ['2']) | ||
]) | ||
]) | ||
} | ||
}).$mount(el) | ||
nextTick(() => { | ||
assert.equal(vm.$el.innerHTML, 'one: <p>1</p>, two: <p>2</p>') | ||
}).then(done) | ||
}) | ||
}) | ||
|
||
describe('locale', () => { | ||
it('should be interpolated', done => { | ||
const el = document.createElement('div') | ||
const vm = new Vue({ | ||
i18n, | ||
render (h) { | ||
return h('p', {}, [ | ||
h('i18n', { props: { path: 'text', locale: 'ja' } }, [ | ||
this._v('1') | ||
]) | ||
]) | ||
} | ||
}).$mount(el) | ||
nextTick(() => { | ||
assert.equal(vm.$el.textContent, '一: 1') | ||
}).then(done) | ||
}) | ||
}) | ||
}) |