From cd2ad192f5116f55eb3e248bc8ba7060ab5ebc74 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sun, 14 Jun 2020 23:39:37 +0900 Subject: [PATCH 1/2] fix: message update in slot closes #60 --- examples/composable/scope/global.html | 17 ++- examples/legacy/fallback/basic.html | 2 +- examples/legacy/fallback/suppress.html | 2 +- examples/legacy/missing/handler.html | 2 +- examples/legacy/missing/suppress.html | 2 +- examples/legacy/scope/global.html | 17 ++- package.json | 4 +- src/composer.ts | 8 +- test/__snapshots__/i18n.test.ts.snap | 9 ++ test/i18n.test.ts | 192 ++++++++++++++++++++++++- yarn.lock | 78 +++++----- 11 files changed, 281 insertions(+), 52 deletions(-) create mode 100644 test/__snapshots__/i18n.test.ts.snap diff --git a/examples/composable/scope/global.html b/examples/composable/scope/global.html index 4f4c19da2..a1968a128 100644 --- a/examples/composable/scope/global.html +++ b/examples/composable/scope/global.html @@ -23,6 +23,12 @@

Root

const { createApp } = Vue const { createI18n, useI18n } = VueI18n + const SlotChild = { + template: ` +

+ ` + } + const SubChild = { template: `
@@ -44,7 +50,8 @@

Sub Child

const Child = { components: { - SubChild + SubChild, + SlotChild }, template: `
@@ -58,6 +65,14 @@

Child

{{ t('message.hi') }}

+ t inside of slot + + {{ t('message.hi') }} + + i18n-t inside of slot + + +
`, setup() { diff --git a/examples/legacy/fallback/basic.html b/examples/legacy/fallback/basic.html index 36aa97f56..c858edad1 100644 --- a/examples/legacy/fallback/basic.html +++ b/examples/legacy/fallback/basic.html @@ -17,7 +17,7 @@ const i18n = createI18n({ legacy: true, locale: 'ja', - fallbackLocale: 'en', + fallbackLocale: ['en'], messages: { en: { message: { diff --git a/examples/legacy/fallback/suppress.html b/examples/legacy/fallback/suppress.html index 1bc701289..2541cea1e 100644 --- a/examples/legacy/fallback/suppress.html +++ b/examples/legacy/fallback/suppress.html @@ -17,7 +17,7 @@ const i18n = createI18n({ legacy: true, locale: 'ja', - fallbackLocale: 'en', + fallbackLocale: ['en'], silentFallbackWarn: true, // warning off messages: { en: { diff --git a/examples/legacy/missing/handler.html b/examples/legacy/missing/handler.html index e6f0d3dbb..c40e7ed10 100644 --- a/examples/legacy/missing/handler.html +++ b/examples/legacy/missing/handler.html @@ -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}'`) diff --git a/examples/legacy/missing/suppress.html b/examples/legacy/missing/suppress.html index 2babd1761..b981a48ef 100644 --- a/examples/legacy/missing/suppress.html +++ b/examples/legacy/missing/suppress.html @@ -17,7 +17,7 @@ const i18n = createI18n({ legacy: true, locale: 'ja', - fallbackLocale: 'en', + fallbackLocale: ['en'], silentTranslationWarn: true, // warning off messages: { en: { diff --git a/examples/legacy/scope/global.html b/examples/legacy/scope/global.html index 3d6093220..00987c5d7 100644 --- a/examples/legacy/scope/global.html +++ b/examples/legacy/scope/global.html @@ -23,6 +23,12 @@

Root

const { createApp } = Vue const { createI18n } = VueI18n + const SlotChild = { + template: ` +

+ ` + } + const SubChild = { template: `
@@ -41,7 +47,8 @@

Sub Child

const Child = { components: { - SubChild + SubChild, + SlotChild }, template: `
@@ -55,6 +62,14 @@

Child

{{ $t('message.hi') }}

+ $t inside of slot + + {{ $t('message.hi') }} + + i18n-t inside of slot + + +
` } diff --git a/package.json b/package.json index 1797e7824..7a82d233d 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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", diff --git a/src/composer.ts b/src/composer.ts index 04fd39fcc..d75fa7a5c 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -470,13 +470,13 @@ export function createComposer( // 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 - } + // if (!_isGlobal) { + // _locale.value + // } return computed( (): T => { - const ret = fn(_context) + const ret = fn(getRuntimeContext()) if (isNumber(ret) && ret === NOT_REOSLVED) { const key = argumentParser() if (__DEV__ && _fallbackRoot && __root) { diff --git a/test/__snapshots__/i18n.test.ts.snap b/test/__snapshots__/i18n.test.ts.snap new file mode 100644 index 000000000..7d290ac22 --- /dev/null +++ b/test/__snapshots__/i18n.test.ts.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`slot reactivity compsable: en 1`] = `"

Root

hello!

Child

hello!

Sub Child

hello!

t inside of slot

hello!

i18n-t inside of slot

hello!

"`; + +exports[`slot reactivity compsable: ja 1`] = `"

Root

こんにちは!

Child

こんにちは!

Sub Child

こんにちは!

t inside of slot

こんにちは!

i18n-t inside of slot

こんにちは!

"`; + +exports[`slot reactivity legacy: en 1`] = `"

Root

hello!

Child

hello!

Sub Child

hello!

$t inside of slot

hello!

i18n-t inside of slot

hello!

"`; + +exports[`slot reactivity legacy: ja 1`] = `"

Root

こんにちは!

Child

こんにちは!

Sub Child

こんにちは!

$t inside of slot

こんにちは!

i18n-t inside of slot

こんにちは!

"`; diff --git a/test/i18n.test.ts b/test/i18n.test.ts index 4630c510f..782a83da0 100644 --- a/test/i18n.test.ts +++ b/test/i18n.test.ts @@ -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' @@ -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: `

` + } + + const SubChild = { + template: ` +
+

Sub Child

+
+ +
+

{{ $t('hello') }}

+
+ ` + } + + const Child = { + components: { + SubChild, + SlotChild + }, + template: ` +
+

Child

+
+ +
+

{{ $t('hello') }}

+ + $t inside of slot + + {{ $t('hello') }} + + i18n-t inside of slot + + + +
+ ` + } + + const App = defineComponent({ + components: { + Child + }, + template: ` +

Root

+
+ +
+

{{ $t('hello') }}

+ + ` + }) + 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: `

` + } + + const SubChild = { + template: ` +
+

Sub Child

+
+ +
+

{{ t('hello') }}

+
+ `, + setup() { + return useI18n() + } + } + + const Child = { + components: { + SubChild, + SlotChild + }, + template: ` +
+

Child

+
+ +
+

{{ t('hello') }}

+ + t inside of slot + + {{ t('hello') }} + + i18n-t inside of slot + + + +
+ `, + setup() { + return useI18n() + } + } + + const App = defineComponent({ + components: { + Child + }, + template: ` +

Root

+
+ +
+

{{ t('hello') }}

+ + `, + setup() { + return useI18n() + } + }) + const { html } = await mount(App, i18n) + expect(html()).toMatchSnapshot('ja') + i18n.global.locale.value = 'en' + await nextTick() + expect(html()).toMatchSnapshot('en') + }) +}) diff --git a/yarn.lock b/yarn.lock index 8aade0079..992c78861 100644 --- a/yarn.lock +++ b/yarn.lock @@ -952,53 +952,53 @@ semver "^7.3.2" tsutils "^3.17.1" -"@vue/compiler-core@3.0.0-beta.14": - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0-beta.14.tgz#69019b5c3da8335e6d83f81b37648caf120dbacd" - integrity sha512-VZarslk2r0E8V9Iuu24LPOWuomWV8KgTp3Pmie6Ys+LnIk+G/hme9BwC2jZgmqgF+adwcfmUC5BTi/KbhRVeIw== +"@vue/compiler-core@3.0.0-beta.15": + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0-beta.15.tgz#8710a8e3ba15ba1a8b62bd17609d26bd27fdcc45" + integrity sha512-NLNW7tAMHl8ybRgTPTIWLsi8aXHbFngY2x95eEHAdxhNasTY5NsgmQBBH9TBAUQEn6Wo8ybmuvQoNzgcw979Zg== dependencies: "@babel/parser" "^7.8.6" "@babel/types" "^7.8.6" - "@vue/shared" "3.0.0-beta.14" + "@vue/shared" "3.0.0-beta.15" estree-walker "^0.8.1" source-map "^0.6.1" -"@vue/compiler-dom@3.0.0-beta.14": - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-beta.14.tgz#2ea1c165e06e9630e687a7a5cbde4e8b20b064ac" - integrity sha512-wZ2uWo4jvAGD5FPNZYMOxpKEDigLcoPvOGhIAv8H4B6ltDyW54Zfc4RrW5MopJqEcHDDZMpcgGcFN5Qa09sLOg== +"@vue/compiler-dom@3.0.0-beta.15": + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-beta.15.tgz#ee6dc9ae1dabb5c5c257d7cc20c5f3e95d5e5f4f" + integrity sha512-0qVaCosZ6XrkmlSOndGlNh33JQ2oao82uWxC/qw4QWBGm6a1DcKkZFIZFYLQWg5ZIcSrEQrR1VzUidBaZw9AIg== dependencies: - "@vue/compiler-core" "3.0.0-beta.14" - "@vue/shared" "3.0.0-beta.14" + "@vue/compiler-core" "3.0.0-beta.15" + "@vue/shared" "3.0.0-beta.15" -"@vue/reactivity@3.0.0-beta.14": - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-beta.14.tgz#a041ec24ce2e545583a6a1a42774311c16870a91" - integrity sha512-csqLljnM+8OBBAyzeUXGIYJhhph0DLOsHQwJGmz7uc342taW6XSi4MXaLk5MRiigunfmXxEswJGziwsh+4YP3g== +"@vue/reactivity@3.0.0-beta.15": + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-beta.15.tgz#4ee0942783d7ff09acab5b8755706597ba6234f6" + integrity sha512-Xa0LG8RTNlPYsuqOBhhV03xKhMmuSU0vtKXoIi1yxp9gGU7ga/TMmnhELb66AiupiXdLJwRcdv00KhPF/2y0dA== dependencies: - "@vue/shared" "3.0.0-beta.14" + "@vue/shared" "3.0.0-beta.15" -"@vue/runtime-core@3.0.0-beta.14": - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.0-beta.14.tgz#4f8162befd6ad1ac55cc6c142edc8301b090658a" - integrity sha512-5WKNMd7lX0vdSMeNd1cF0VhM+N+kXicSXKKZtTfQLUfZt1gLuE3nlBhv1PqjGf79zXw5lQLzz6XoUY1i52rEkA== +"@vue/runtime-core@3.0.0-beta.15": + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.0-beta.15.tgz#35c2ef61dadf07dfe9129dbea6fb8c1b88cc5bd6" + integrity sha512-jDkqSs1hsS9fRCgzah7VINafxWj7bYoDyweVuBqm6KPcHRfGkRZZxl2NltbbVaLH76Qvm4PPSnqGgEx7QtFSgg== dependencies: - "@vue/reactivity" "3.0.0-beta.14" - "@vue/shared" "3.0.0-beta.14" + "@vue/reactivity" "3.0.0-beta.15" + "@vue/shared" "3.0.0-beta.15" -"@vue/runtime-dom@3.0.0-beta.14": - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.0-beta.14.tgz#080e9dd48a95da639f9fcc6d70a2d9620aec6ab8" - integrity sha512-nwHvG+IsO0Ttl39NPvQKX2vv5H4XWZVzZCX1rqEIBP3llHyyB9dMrNSPcw54YlPGrEuCwBxVDokHG4CSeVEdtg== +"@vue/runtime-dom@3.0.0-beta.15": + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.0-beta.15.tgz#e3ce740c5e3766020719b174686bd65c75975c7e" + integrity sha512-161rUw1sWfbv51Ua8gKXaPc+seRJQcV+MLokTJtqYtNCajya0Mx6vdXJajBWqjDT8/Udx0sb7Wm/K/0DfGBUTw== dependencies: - "@vue/runtime-core" "3.0.0-beta.14" - "@vue/shared" "3.0.0-beta.14" + "@vue/runtime-core" "3.0.0-beta.15" + "@vue/shared" "3.0.0-beta.15" csstype "^2.6.8" -"@vue/shared@3.0.0-beta.14": - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-beta.14.tgz#886afe5c233a5b255c186142324c40f114958af5" - integrity sha512-mnK5teJMLzsBE56Kys+uiyR/jAl1kbokHZ++MnlP7ls9icPqZ/QQE/VTDl3QJ7IHteS2VR6ytAz/Aa/4Dpv/ew== +"@vue/shared@3.0.0-beta.15": + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-beta.15.tgz#77444ab234e229c3fdcc9f394678e167a55757af" + integrity sha512-wViILT5GgxMtnXVQ1xupj43wvnZ41g3NLWaBObs7l+eTxz5vq5yx72qH6HRpsfhR2Mg39jE0cfNLFEpC4lJIUQ== JSONStream@^1.0.4: version "1.3.5" @@ -7005,14 +7005,14 @@ vue-eslint-parser@^7.0.0: esquery "^1.0.1" lodash "^4.17.15" -vue@^3.0.0-beta.14: - version "3.0.0-beta.14" - resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.0-beta.14.tgz#d2c8739e00c4a4a06b519c14c57d204c350c980c" - integrity sha512-0MH1g5O3zX8ijvZuiQTYFq3UwHxtj512I/wrMPQLVXwjqb+ILA+fooSpdz4xgUBBl5zN/K9xJIwbl23sv+Sn7A== +vue@^3.0.0-beta.15: + version "3.0.0-beta.15" + resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.0-beta.15.tgz#99f107534755d3a237b51fc1279c43af2fefde6e" + integrity sha512-KTmvfNpkvD6mao8vloqjUMjrHEivS1HZvHmYeHPRHqU2HRvNcrZuwXYvETt3dGOTu0Oj7zAWQXP+uZ34CW75sw== dependencies: - "@vue/compiler-dom" "3.0.0-beta.14" - "@vue/runtime-dom" "3.0.0-beta.14" - "@vue/shared" "3.0.0-beta.14" + "@vue/compiler-dom" "3.0.0-beta.15" + "@vue/runtime-dom" "3.0.0-beta.15" + "@vue/shared" "3.0.0-beta.15" w3c-hr-time@^1.0.2: version "1.0.2" From 875bd6280811e14b1a5d10a47b237319e97ea3ac Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sun, 14 Jun 2020 23:41:02 +0900 Subject: [PATCH 2/2] fix: remove --- src/composer.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/composer.ts b/src/composer.ts index d75fa7a5c..cf6567fd0 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -467,13 +467,6 @@ export function createComposer( fallbackFail: (key: string) => T, successCondition: (val: unknown) => boolean ): ComputedRef { - // 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 => { const ret = fn(getRuntimeContext())