Skip to content

Commit 96a36ee

Browse files
authored
support vue3 beta (#13)
1 parent 357390e commit 96a36ee

File tree

4 files changed

+120
-71
lines changed

4 files changed

+120
-71
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"dependencies": {
28-
"vue": "^3.0.0-alpha.10"
28+
"vue": "^3.0.0-beta.1"
2929
},
3030
"devDependencies": {
3131
"@microsoft/api-documenter": "^7.7.17",

src/legacy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { App, Plugin } from 'vue'
88
import { apply } from './plugin'
9-
import { getMixin } from './mixin'
9+
import { defineMixin } from './mixin'
1010
import { Path, resolveValue } from './path'
1111
import {
1212
PluralizationRule,
@@ -476,7 +476,7 @@ export function createVueI18n(options: VueI18nOptions = {}): VueI18n {
476476
// eslint-disable-next-line @typescript-eslint/no-explicit-any
477477
install(app: App, ...options: any[]): void {
478478
apply(app, composer, ...options)
479-
app.mixin(getMixin(vueI18n, composer))
479+
app.mixin(defineMixin(app, vueI18n, composer))
480480
}
481481
}
482482

src/mixin.ts

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ComponentPublicInstance, ComponentOptions } from 'vue'
1+
import {
2+
App,
3+
getCurrentInstance,
4+
ComponentInternalInstance,
5+
ComponentPublicInstance,
6+
ComponentOptions
7+
} from 'vue'
28
import { Path } from './path'
39
import { Locale } from './runtime/context'
410
import { Composer } from './composer'
@@ -41,11 +47,66 @@ type LegacyMixin = {
4147
$n(value: number, args: { [key: string]: string }): NumberFormatResult
4248
}
4349

50+
const legacyInstances = new Map<ComponentInternalInstance, VueI18n>()
51+
52+
function getLegacyInstance(
53+
key: ComponentInternalInstance | null,
54+
legacyDefault: VueI18n
55+
): VueI18n {
56+
return key ? legacyInstances.get(key) || legacyDefault : legacyDefault
57+
}
58+
4459
// supports compatibility for vue-i18n legacy mixin
45-
export function getMixin(
46-
vueI18n: VueI18n,
60+
export function defineMixin(
61+
app: App,
62+
legacyGlobal: VueI18n,
4763
composer: Composer
4864
): ComponentOptions {
65+
// inject Legacy APIs for globally
66+
Object.defineProperty(app.config.globalProperties, '$i18n', {
67+
get: () => {
68+
const instance = getCurrentInstance()
69+
return instance
70+
? legacyInstances.get(instance) || legacyGlobal
71+
: legacyGlobal
72+
}
73+
})
74+
75+
Object.defineProperty(app.config.globalProperties, '$t', {
76+
value: (...args: unknown[]): TranslateResult => {
77+
const vueI18n = getLegacyInstance(getCurrentInstance(), legacyGlobal)
78+
return vueI18n.t(...args)
79+
}
80+
})
81+
82+
Object.defineProperty(app.config.globalProperties, '$tc', {
83+
value: (...args: unknown[]): TranslateResult => {
84+
const vueI18n = getLegacyInstance(getCurrentInstance(), legacyGlobal)
85+
return vueI18n.tc(...args)
86+
}
87+
})
88+
89+
Object.defineProperty(app.config.globalProperties, '$te', {
90+
value: (key: Path, locale?: Locale): boolean => {
91+
const vueI18n = getLegacyInstance(getCurrentInstance(), legacyGlobal)
92+
return vueI18n.te(key, locale)
93+
}
94+
})
95+
96+
Object.defineProperty(app.config.globalProperties, '$d', {
97+
value: (...args: unknown[]): DateTimeFormatResult => {
98+
const vueI18n = getLegacyInstance(getCurrentInstance(), legacyGlobal)
99+
return vueI18n.d(...args)
100+
}
101+
})
102+
103+
Object.defineProperty(app.config.globalProperties, '$n', {
104+
value: (...args: unknown[]): NumberFormatResult => {
105+
const vueI18n = getLegacyInstance(getCurrentInstance(), legacyGlobal)
106+
return vueI18n.n(...args)
107+
}
108+
})
109+
49110
return {
50111
beforeCreate(this: ComponentPublicInstance<LegacyMixin>) {
51112
const options = this.$options
@@ -57,34 +118,22 @@ export function getMixin(
57118
optionsI18n.__i18n = options.__i18n
58119
}
59120
optionsI18n.__root = composer
60-
this.$i18n = createVueI18n(optionsI18n)
121+
const instance = getCurrentInstance()
122+
if (instance) {
123+
legacyInstances.set(instance, createVueI18n(optionsI18n))
124+
}
61125
} else if (options.__i18n) {
62-
this.$i18n = createVueI18n({ __i18n: options.__i18n, __root: composer })
63-
} else if (this.$root && this.$root.proxy) {
64-
// root i18n
65-
// TODO: should resolve type inference
66-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67-
const instance: any = this.$root.proxy
68-
this.$i18n = instance.$i18n || vueI18n
69-
} else if (this.$parent && this.$parent.proxy) {
70-
// parent i18n
71-
// TODO: should resolve type inference
72-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73-
const instance: any = this.$parent.proxy
74-
this.$i18n = instance.$i18n || vueI18n
75-
} else {
76-
this.$i18n = vueI18n
126+
const instance = getCurrentInstance()
127+
if (instance) {
128+
legacyInstances.set(
129+
instance,
130+
createVueI18n({
131+
__i18n: options.__i18n,
132+
__root: composer
133+
})
134+
)
135+
}
77136
}
78-
79-
// define vue-i18n legacy APIs
80-
this.$t = (...args: unknown[]): TranslateResult => this.$i18n.t(...args)
81-
this.$tc = (...args: unknown[]): TranslateResult => this.$i18n.tc(...args)
82-
this.$te = (key: Path, locale?: Locale): boolean =>
83-
this.$i18n.te(key, locale)
84-
this.$d = (...args: unknown[]): DateTimeFormatResult =>
85-
this.$i18n.d(...args)
86-
this.$n = (...args: unknown[]): NumberFormatResult =>
87-
this.$i18n.n(...args)
88137
}
89138
}
90139
}

yarn.lock

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -911,53 +911,53 @@
911911
semver "^6.3.0"
912912
tsutils "^3.17.1"
913913

914-
"@vue/[email protected]alpha.12":
915-
version "3.0.0-alpha.12"
916-
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0-alpha.12.tgz#f125bdfd637d6bcc95edb2c55483a242932c5d2e"
917-
integrity sha512-0aDhUP9SS+O1psH2xm08oxQQV+5p5ig/zhSNL8fOreSQabIzfSuNfZqrJ8e2Ffa+zoJkZ0Z0SKJ1+UuzHXm0zA==
914+
"@vue/[email protected]beta.1":
915+
version "3.0.0-beta.1"
916+
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.0-beta.1.tgz#77017e0a98b808da3834327bcf8857e4b89363ef"
917+
integrity sha512-mlHfX/+3qH+GAuMbGFvye0Jn2/H9IwbmX6oqbpLErM241xAVbpqniFDiJCywrfjeUDKMfDuNs1i6lGnDBH715g==
918918
dependencies:
919919
"@babel/parser" "^7.8.6"
920920
"@babel/types" "^7.8.6"
921-
"@vue/shared" "3.0.0-alpha.12"
921+
"@vue/shared" "3.0.0-beta.1"
922922
estree-walker "^0.8.1"
923923
source-map "^0.6.1"
924924

925-
"@vue/[email protected]alpha.12":
926-
version "3.0.0-alpha.12"
927-
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-alpha.12.tgz#3f5b856007d5201c477540299bd89e0989e83543"
928-
integrity sha512-MpdGAFmS8Pc945Kgo0FbAQVObi+aTBGpDCz4f1UwBBR8z/TVgENTd8DXzksOnu82RrW1hV5Lbn3uUW/RuHFJlQ==
925+
"@vue/[email protected]beta.1":
926+
version "3.0.0-beta.1"
927+
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.0-beta.1.tgz#eabd8279e5433214e613558561f59e05a01ef450"
928+
integrity sha512-Tp3Y2sT014B0Z3VpdWLwAv8o6kNPZOwFFMI2aAgIBmG3KWPGXUxm2LDRrNZWPoOPYPWYjRMBri1cNy3gA2MeCA==
929929
dependencies:
930-
"@vue/compiler-core" "3.0.0-alpha.12"
931-
"@vue/shared" "3.0.0-alpha.12"
930+
"@vue/compiler-core" "3.0.0-beta.1"
931+
"@vue/shared" "3.0.0-beta.1"
932932

933-
"@vue/[email protected]alpha.12":
934-
version "3.0.0-alpha.12"
935-
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-alpha.12.tgz#35cb74dd08ee8f8929166b54cbaa0c30c959a865"
936-
integrity sha512-9lSysYh00p1lkZehwjUf6r4aZIgp+rRKIWfxlffnnEwH1rloOV0RkYJrix4NTOcxHHqIYYcoGBVJ3AK64pCJoQ==
933+
"@vue/[email protected]beta.1":
934+
version "3.0.0-beta.1"
935+
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.0-beta.1.tgz#b4899fe95efbc8ed15343f9d67950f2a33e219dc"
936+
integrity sha512-XbitRfUKIAgPO0IXEbi9/mq93xOL5CGRjaTPH2DvvTU0hXlAeW8aMPQe3+R+vvXppBiFERY3yjZjQc5SYkZVsg==
937937
dependencies:
938-
"@vue/shared" "3.0.0-alpha.12"
938+
"@vue/shared" "3.0.0-beta.1"
939939

940-
"@vue/[email protected]alpha.12":
941-
version "3.0.0-alpha.12"
942-
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.0-alpha.12.tgz#790c4a9743d2999d1886b6da1a193f90aaf5fa9a"
943-
integrity sha512-eVhd4bbVNd25FulwR9B6QD5+TeADlwSUm1ktRA0zvO3Sy+QAABPXXI147R/i18tr2v51tVWsUTIWz5f7H7Uang==
940+
"@vue/[email protected]beta.1":
941+
version "3.0.0-beta.1"
942+
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.0-beta.1.tgz#a3d97183dd5bc022fc02b9a86180698dc11a2e65"
943+
integrity sha512-eNHHGT17i2dOtAQpmn8QRDAcVGS4TiCyjGW+v6GfSlHvip1fFUmawD9pLSPVdL3DngGm9SmioOVmBK2CB8JG0g==
944944
dependencies:
945-
"@vue/reactivity" "3.0.0-alpha.12"
946-
"@vue/shared" "3.0.0-alpha.12"
945+
"@vue/reactivity" "3.0.0-beta.1"
946+
"@vue/shared" "3.0.0-beta.1"
947947

948-
"@vue/[email protected]alpha.12":
949-
version "3.0.0-alpha.12"
950-
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.0-alpha.12.tgz#70044b72db2ec184b49baaa6e36fc7ac6f7b2fbd"
951-
integrity sha512-G5JHfJfm/NX0hsoI/x3PNYqzv99B4Dryz9WU++WAIwM+VZLyTat0H6Q3jWdVeGm5h9tY/zr6UD7NokGRKuwsUw==
948+
"@vue/[email protected]beta.1":
949+
version "3.0.0-beta.1"
950+
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.0-beta.1.tgz#f93565cccdc0a1d2f419836d5468989a7a868c66"
951+
integrity sha512-+dnJS7582V6iZLm8YqsXjabL1eXo3n8ltzxWZlI/nV1ZPSWKQJpvB6ICbdRjwOlka4lukYStca3ZsrYQI7SvVg==
952952
dependencies:
953-
"@vue/runtime-core" "3.0.0-alpha.12"
954-
"@vue/shared" "3.0.0-alpha.12"
953+
"@vue/runtime-core" "3.0.0-beta.1"
954+
"@vue/shared" "3.0.0-beta.1"
955955
csstype "^2.6.8"
956956

957-
"@vue/[email protected]alpha.12":
958-
version "3.0.0-alpha.12"
959-
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-alpha.12.tgz#36855f95e2855f0099980aebf63edf51e0bad471"
960-
integrity sha512-SVXQd0fJ7Fod7J8WBFDPLp0rNobnixo11E+OevC9cBs5Y0fZklAr+8i5XaJJ/fdPr9qDWFHweXDwIvVzpoZMOA==
957+
"@vue/[email protected]beta.1":
958+
version "3.0.0-beta.1"
959+
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.0-beta.1.tgz#234185d345cde7e05db569e653feae87912bf218"
960+
integrity sha512-LehYUPtykdL3ZEUto2W1n26k+opBT1gtFdESPtp4lGo9oIreLl3pJVoUPIkY/Kk/mOZGmBOsYXiLZGJvfmGEQw==
961961

962962
JSONStream@^1.0.4:
963963
version "1.3.5"
@@ -6765,14 +6765,14 @@ vue-eslint-parser@^7.0.0:
67656765
esquery "^1.0.1"
67666766
lodash "^4.17.15"
67676767

6768-
vue@^3.0.0-alpha.10:
6769-
version "3.0.0-alpha.12"
6770-
resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.0-alpha.12.tgz#b8eb8dd47822e8d4479bd2d4155a28f7b2c72689"
6771-
integrity sha512-EKMdt7MOSXLo6F6h8UHLI8GzW58qDyyKPLt4NV6GOzyjtSPdlx2j+ZsCe6C9d+NRXSvvf4RtUwpWqOZ/jGmZwg==
6768+
vue@^3.0.0-beta.1:
6769+
version "3.0.0-beta.1"
6770+
resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.0-beta.1.tgz#86a4b8431ab4b7523487cad4ec0d7235fead22dc"
6771+
integrity sha512-Z5SFtxSQNYwtyfMWxAiJYoumOwRnRdha6opDaRy8f4jhJAGn9tpi2muJLxE4m6QZ9UqjnHghUC3VjxzXfofQTQ==
67726772
dependencies:
6773-
"@vue/compiler-dom" "3.0.0-alpha.12"
6774-
"@vue/runtime-dom" "3.0.0-alpha.12"
6775-
"@vue/shared" "3.0.0-alpha.12"
6773+
"@vue/compiler-dom" "3.0.0-beta.1"
6774+
"@vue/runtime-dom" "3.0.0-beta.1"
6775+
"@vue/shared" "3.0.0-beta.1"
67766776

67776777
w3c-hr-time@^1.0.1:
67786778
version "1.0.2"

0 commit comments

Comments
 (0)