Skip to content
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
17 changes: 16 additions & 1 deletion examples/composable/scope/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ <h1>Root</h1>
const { createApp } = Vue
const { createI18n, useI18n } = VueI18n

const SlotChild = {
template: `
<p><slot/></p>
`
}

const SubChild = {
template: `
<div class="sub-child">
Expand All @@ -44,7 +50,8 @@ <h1>Sub Child</h1>

const Child = {
components: {
SubChild
SubChild,
SlotChild
},
template: `
<div class="child">
Expand All @@ -58,6 +65,14 @@ <h1>Child</h1>
</form>
<p>{{ t('message.hi') }}</p>
<SubChild />
t inside of slot
<SlotChild>
{{ t('message.hi') }}
</SlotChild>
i18n-t inside of slot
<SlotChild>
<i18n-t keypath='message.hi'/>
</SlotChild>
</div>
`,
setup() {
Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/fallback/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const i18n = createI18n({
legacy: true,
locale: 'ja',
fallbackLocale: 'en',
fallbackLocale: ['en'],
messages: {
en: {
message: {
Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/fallback/suppress.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const i18n = createI18n({
legacy: true,
locale: 'ja',
fallbackLocale: 'en',
fallbackLocale: ['en'],
silentFallbackWarn: true, // warning off
messages: {
en: {
Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/missing/handler.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const i18n = createI18n({
legacy: true,
locale: 'ja',
fallbackLocale: 'en',
fallbackLocale: ['en'],
missing: (locale, key, instance) => {
// something to do ...
console.warn(`detect '${key}' key missing in '${locale}'`)
Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/missing/suppress.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const i18n = createI18n({
legacy: true,
locale: 'ja',
fallbackLocale: 'en',
fallbackLocale: ['en'],
silentTranslationWarn: true, // warning off
messages: {
en: {
Expand Down
17 changes: 16 additions & 1 deletion examples/legacy/scope/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ <h1>Root</h1>
const { createApp } = Vue
const { createI18n } = VueI18n

const SlotChild = {
template: `
<p><slot/></p>
`
}

const SubChild = {
template: `
<div class="sub-child">
Expand All @@ -41,7 +47,8 @@ <h1>Sub Child</h1>

const Child = {
components: {
SubChild
SubChild,
SlotChild
},
template: `
<div class="child">
Expand All @@ -55,6 +62,14 @@ <h1>Child</h1>
</form>
<p>{{ $t('message.hi') }}</p>
<SubChild />
$t inside of slot
<SlotChild>
{{ $t('message.hi') }}
</SlotChild>
i18n-t inside of slot
<SlotChild>
<i18n-t keypath='message.hi'/>
</SlotChild>
</div>
`
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"ts-jest": "^26.0.0",
"typescript": "^3.8.3",
"typescript-eslint-language-service": "^3.0.0",
"vue": "^3.0.0-beta.14"
"vue": "^3.0.0-beta.15"
},
"engines": {
"node": ">= 10"
Expand All @@ -87,7 +87,7 @@
"main": "dist/vue-i18n.cjs.js",
"module": "dist/vue-i18n.esm-bundler.js",
"peerDependencies": {
"vue": "^3.0.0-beta.14"
"vue": "^3.0.0-beta.15"
},
"repository": {
"type": "git",
Expand Down
9 changes: 1 addition & 8 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,16 +467,9 @@ export function createComposer(
fallbackFail: (key: string) => T,
successCondition: (val: unknown) => boolean
): ComputedRef<T> {
// NOTE:
// if this composer is global (__root is `undefined`), add dependency trakcing!
// by containing this, we can reactively notify components that reference the global composer.
if (!_isGlobal) {
_locale.value
}

return computed<T>(
(): T => {
const ret = fn(_context)
const ret = fn(getRuntimeContext())
if (isNumber(ret) && ret === NOT_REOSLVED) {
const key = argumentParser()
if (__DEV__ && _fallbackRoot && __root) {
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/i18n.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`slot reactivity compsable: en 1`] = `"<h1>Root</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>hello!</p><div class=\\"child\\"><h1>Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>hello!</p><div class=\\"sub-child\\"><h1>Sub Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>hello!</p></div> t inside of slot <p>hello!</p> i18n-t inside of slot <p>hello!</p></div>"`;

exports[`slot reactivity compsable: ja 1`] = `"<h1>Root</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>こんにちは!</p><div class=\\"child\\"><h1>Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>こんにちは!</p><div class=\\"sub-child\\"><h1>Sub Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>こんにちは!</p></div> t inside of slot <p>こんにちは!</p> i18n-t inside of slot <p>こんにちは!</p></div>"`;

exports[`slot reactivity legacy: en 1`] = `"<h1>Root</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>hello!</p><div class=\\"child\\"><h1>Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>hello!</p><div class=\\"sub-child\\"><h1>Sub Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>hello!</p></div> $t inside of slot <p>hello!</p> i18n-t inside of slot <p>hello!</p></div>"`;

exports[`slot reactivity legacy: ja 1`] = `"<h1>Root</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>こんにちは!</p><div class=\\"child\\"><h1>Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>こんにちは!</p><div class=\\"sub-child\\"><h1>Sub Child</h1><form><select><option value=\\"en\\">en</option><option value=\\"ja\\">ja</option></select></form><p>こんにちは!</p></div> $t inside of slot <p>こんにちは!</p> i18n-t inside of slot <p>こんにちは!</p></div>"`;
192 changes: 191 additions & 1 deletion test/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment jsdom
*/

import { defineComponent } from 'vue'
import { defineComponent, nextTick } from 'vue'
import { mount } from './helper'
import { createI18n, useI18n } from '../src/i18n'
import { errorMessages, I18nErrorCodes } from '../src/errors'
Expand Down Expand Up @@ -233,3 +233,193 @@ describe('useI18n', () => {
)
})
})

describe('slot reactivity', () => {
let org, spy
beforeEach(() => {
org = console.warn
spy = jest.fn()
console.warn = spy
})
afterEach(() => {
console.warn = org
})

test('legacy', async () => {
const i18n = createI18n({
legacy: true,
locale: 'ja',
fallbackLocale: ['en'],
messages: {
en: {
hello: 'hello!'
},
ja: {
hello: 'こんにちは!'
}
}
})

const SlotChild = {
template: `<p><slot/></p>`
}

const SubChild = {
template: `
<div class="sub-child">
<h1>Sub Child</h1>
<form>
<select v-model="$i18n.locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ $t('hello') }}</p>
</div>
`
}

const Child = {
components: {
SubChild,
SlotChild
},
template: `
<div class="child">
<h1>Child</h1>
<form>
<select v-model="$i18n.locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ $t('hello') }}</p>
<SubChild />
$t inside of slot
<SlotChild>
{{ $t('hello') }}
</SlotChild>
i18n-t inside of slot
<SlotChild>
<i18n-t keypath='hello'/>
</SlotChild>
</div>
`
}

const App = defineComponent({
components: {
Child
},
template: `
<h1>Root</h1>
<form>
<select v-model="$i18n.locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ $t('hello') }}</p>
<Child />
`
})
const { html } = await mount(App, i18n)
expect(html()).toMatchSnapshot('ja')
i18n.global.locale.value = 'en'
await nextTick()
expect(html()).toMatchSnapshot('en')
})

test('compsable', async () => {
const i18n = createI18n({
locale: 'ja',
fallbackLocale: ['en'],
messages: {
en: {
hello: 'hello!'
},
ja: {
hello: 'こんにちは!'
}
}
})

const SlotChild = {
template: `<p><slot/></p>`
}

const SubChild = {
template: `
<div class="sub-child">
<h1>Sub Child</h1>
<form>
<select v-model="locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ t('hello') }}</p>
</div>
`,
setup() {
return useI18n()
}
}

const Child = {
components: {
SubChild,
SlotChild
},
template: `
<div class="child">
<h1>Child</h1>
<form>
<select v-model="locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ t('hello') }}</p>
<SubChild />
t inside of slot
<SlotChild>
{{ t('hello') }}
</SlotChild>
i18n-t inside of slot
<SlotChild>
<i18n-t keypath='hello'/>
</SlotChild>
</div>
`,
setup() {
return useI18n()
}
}

const App = defineComponent({
components: {
Child
},
template: `
<h1>Root</h1>
<form>
<select v-model="locale">
<option value="en">en</option>
<option value="ja">ja</option>
</select>
</form>
<p>{{ t('hello') }}</p>
<Child />
`,
setup() {
return useI18n()
}
})
const { html } = await mount(App, i18n)
expect(html()).toMatchSnapshot('ja')
i18n.global.locale.value = 'en'
await nextTick()
expect(html()).toMatchSnapshot('en')
})
})
Loading