Skip to content

Commit 12a7663

Browse files
committed
improve comsposable API setup process
1 parent 7047d27 commit 12a7663

File tree

6 files changed

+137
-124
lines changed

6 files changed

+137
-124
lines changed

src/composer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import {
99
ref,
1010
computed,
1111
getCurrentInstance,
12+
App,
13+
Plugin,
1214
ComponentInternalInstance
1315
} from 'vue'
1416
import { WritableComputedRef, ComputedRef } from '@vue/reactivity'
17+
import { apply } from './plugin'
1518
import { Path } from './path'
1619
import {
1720
DateTimeFormats,
@@ -151,6 +154,7 @@ export type I18nComposer = {
151154
setPostTranslationHandler(handler: PostTranslationHandler | null): void
152155
getMissingHandler(): MissingHandler | null
153156
setMissingHandler(handler: MissingHandler | null): void
157+
install: Plugin
154158
}
155159

156160
function defineRuntimeMissingHandler(
@@ -466,7 +470,7 @@ export function createI18nComposer(
466470
}
467471

468472
// export composable API!
469-
return {
473+
const composer = {
470474
/**
471475
* properties
472476
*/
@@ -530,6 +534,11 @@ export function createI18nComposer(
530534
getPostTranslationHandler,
531535
setPostTranslationHandler,
532536
getMissingHandler,
533-
setMissingHandler
537+
setMissingHandler,
538+
install(app: App): void {
539+
apply(app, composer)
540+
}
534541
}
542+
543+
return composer
535544
}

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export {
1414
I18nComposer,
1515
createI18nComposer
1616
} from './composer'
17-
export * from './use'
17+
export { useI18n } from './use'
1818
export {
1919
TranslateResult,
2020
Choice,
@@ -28,4 +28,3 @@ export {
2828
VueI18n,
2929
createI18n
3030
} from './legacy'
31-
export { LegacyVueI18n } from './plugin'

src/legacy.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* This module is offered legacy vue-i18n API compatibility
55
*/
66

7-
import { App } from 'vue'
8-
import { applyPlugin } from './plugin'
7+
import { App, Plugin } from 'vue'
8+
import { apply } from './plugin'
9+
import { getMixin } from './mixin'
910
import { Path, resolveValue } from './path'
1011
import {
1112
PluralizationRule,
@@ -152,7 +153,7 @@ export type VueI18n = {
152153
setNumberFormat(locale: Locale, format: NumberFormat): void
153154
mergeNumberFormat(locale: Locale, format: NumberFormat): void
154155
getChoiceIndex: (choice: Choice, choicesLength: number) => number
155-
install(app: App): void
156+
install: Plugin
156157
}
157158

158159
/**
@@ -471,7 +472,8 @@ export function createI18n(options: VueI18nOptions = {}): VueI18n {
471472

472473
// install
473474
install(app: App): void {
474-
applyPlugin(app, i18n as VueI18n, composer)
475+
apply(app, composer)
476+
app.mixin(getMixin(i18n, composer))
475477
}
476478
}
477479

src/mixin.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { ComponentPublicInstance, ComponentOptions } from 'vue'
2+
import { Path } from './path'
3+
import { Locale } from './runtime/context'
4+
import { I18nComposer } from './composer'
5+
import {
6+
VueI18n,
7+
createI18n,
8+
VueI18nOptions,
9+
TranslateResult,
10+
DateTimeFormatResult,
11+
NumberFormatResult
12+
} from './legacy'
13+
14+
type LegacyMixin = {
15+
$i18n: VueI18n
16+
$t(key: Path): TranslateResult
17+
$t(key: Path, locale: Locale): TranslateResult
18+
$t(key: Path, locale: Locale, list: unknown[]): TranslateResult
19+
$t(key: Path, locale: Locale, named: object): TranslateResult
20+
$t(key: Path, list: unknown[]): TranslateResult
21+
$t(key: Path, named: object): TranslateResult
22+
$tc(key: Path): TranslateResult
23+
$tc(key: Path, locale: Locale): TranslateResult
24+
$tc(key: Path, list: unknown[]): TranslateResult
25+
$tc(key: Path, named: object): TranslateResult
26+
$tc(key: Path, choice: number): TranslateResult
27+
$tc(key: Path, choice: number, locale: Locale): TranslateResult
28+
$tc(key: Path, choice: number, list: unknown[]): TranslateResult
29+
$tc(key: Path, choice: number, named: object): TranslateResult
30+
$te(key: Path, locale?: Locale): boolean
31+
$d(value: number | Date): DateTimeFormatResult
32+
$d(value: number | Date, key: string): DateTimeFormatResult
33+
$d(value: number | Date, key: string, locale: Locale): DateTimeFormatResult
34+
$d(
35+
value: number | Date,
36+
args: { [key: string]: string }
37+
): DateTimeFormatResult
38+
$n(value: number): NumberFormatResult
39+
$n(value: number, key: string): NumberFormatResult
40+
$n(value: number, key: string, locale: Locale): NumberFormatResult
41+
$n(value: number, args: { [key: string]: string }): NumberFormatResult
42+
}
43+
44+
// supports compatibility for vue-i18n legacy mixin
45+
export function getMixin(
46+
legacy: VueI18n,
47+
composer: I18nComposer
48+
): ComponentOptions {
49+
return {
50+
beforeCreate(this: ComponentPublicInstance<LegacyMixin>) {
51+
const options = this.$options
52+
53+
if (options.i18n) {
54+
// component local i18n
55+
const optionsI18n = options.i18n as VueI18nOptions
56+
if (options.__i18n) {
57+
optionsI18n.__i18n = options.__i18n
58+
}
59+
optionsI18n._root = composer
60+
this.$i18n = createI18n(optionsI18n)
61+
} else if (options.__i18n) {
62+
this.$i18n = createI18n({ __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 || legacy
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 || legacy
75+
} else {
76+
this.$i18n = legacy
77+
}
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)
88+
}
89+
}
90+
}

src/plugin.ts

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,10 @@
1-
import { App, ComponentPublicInstance, FunctionDirective } from 'vue'
2-
import { Path } from './path'
3-
import { Locale } from './runtime/context'
1+
import { App, FunctionDirective } from 'vue'
42
import { I18nComposer } from './composer'
53
import { GlobalI18nSymbol } from './use'
6-
import {
7-
VueI18n,
8-
createI18n,
9-
VueI18nOptions,
10-
TranslateResult,
11-
DateTimeFormatResult,
12-
NumberFormatResult
13-
} from './legacy'
144
import { Interpolate, Number } from './components'
155
import { hook as vT } from './directive'
166

17-
export type LegacyVueI18n = {
18-
$i18n: VueI18n
19-
$t(key: Path): TranslateResult
20-
$t(key: Path, locale: Locale): TranslateResult
21-
$t(key: Path, locale: Locale, list: unknown[]): TranslateResult
22-
$t(key: Path, locale: Locale, named: object): TranslateResult
23-
$t(key: Path, list: unknown[]): TranslateResult
24-
$t(key: Path, named: object): TranslateResult
25-
$tc(key: Path): TranslateResult
26-
$tc(key: Path, locale: Locale): TranslateResult
27-
$tc(key: Path, list: unknown[]): TranslateResult
28-
$tc(key: Path, named: object): TranslateResult
29-
$tc(key: Path, choice: number): TranslateResult
30-
$tc(key: Path, choice: number, locale: Locale): TranslateResult
31-
$tc(key: Path, choice: number, list: unknown[]): TranslateResult
32-
$tc(key: Path, choice: number, named: object): TranslateResult
33-
$te(key: Path, locale?: Locale): boolean
34-
$d(value: number | Date): DateTimeFormatResult
35-
$d(value: number | Date, key: string): DateTimeFormatResult
36-
$d(value: number | Date, key: string, locale: Locale): DateTimeFormatResult
37-
$d(
38-
value: number | Date,
39-
args: { [key: string]: string }
40-
): DateTimeFormatResult
41-
$n(value: number): NumberFormatResult
42-
$n(value: number, key: string): NumberFormatResult
43-
$n(value: number, key: string, locale: Locale): NumberFormatResult
44-
$n(value: number, args: { [key: string]: string }): NumberFormatResult
45-
}
46-
47-
export function applyPlugin(
48-
app: App,
49-
legacyI18n: VueI18n,
50-
composer: I18nComposer
51-
): void {
7+
export function apply(app: App, composer: I18nComposer): void {
528
// install components
539
app.component(Interpolate.name, Interpolate)
5410
app.component(Number.name, Number)
@@ -58,47 +14,4 @@ export function applyPlugin(
5814

5915
// setup global provider
6016
app.provide(GlobalI18nSymbol, composer)
61-
62-
// supports compatibility for vue-i18n legacy APIs
63-
app.mixin({
64-
beforeCreate(this: ComponentPublicInstance<LegacyVueI18n>) {
65-
const options = this.$options
66-
67-
if (options.i18n) {
68-
// component local i18n
69-
const optionsI18n = options.i18n as VueI18nOptions
70-
if (options.__i18n) {
71-
optionsI18n.__i18n = options.__i18n
72-
}
73-
optionsI18n._root = composer
74-
this.$i18n = createI18n(optionsI18n)
75-
} else if (options.__i18n) {
76-
this.$i18n = createI18n({ __i18n: options.__i18n, _root: composer })
77-
} else if (this.$root && this.$root.proxy) {
78-
// root i18n
79-
// TODO: should resolve type inference
80-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81-
const instance: any = this.$root.proxy
82-
this.$i18n = instance.$i18n || legacyI18n
83-
} else if (this.$parent && this.$parent.proxy) {
84-
// parent i18n
85-
// TODO: should resolve type inference
86-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87-
const instance: any = this.$parent.proxy
88-
this.$i18n = instance.$i18n || legacyI18n
89-
} else {
90-
this.$i18n = legacyI18n
91-
}
92-
93-
// define vue-i18n legacy APIs
94-
this.$t = (...args: unknown[]): TranslateResult => this.$i18n.t(...args)
95-
this.$tc = (...args: unknown[]): TranslateResult => this.$i18n.tc(...args)
96-
this.$te = (key: Path, locale?: Locale): boolean =>
97-
this.$i18n.te(key, locale)
98-
this.$d = (...args: unknown[]): DateTimeFormatResult =>
99-
this.$i18n.d(...args)
100-
this.$n = (...args: unknown[]): NumberFormatResult =>
101-
this.$i18n.n(...args)
102-
}
103-
})
10417
}

yarn.lock

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,9 @@
690690
"@babel/types" "^7.0.0"
691691

692692
"@types/babel__traverse@*", "@types/babel__traverse@^7.0.6":
693-
version "7.0.9"
694-
resolved "https://registry.yarnpkg.com/@types/babel__traverse/-/babel__traverse-7.0.9.tgz#be82fab304b141c3eee81a4ce3b034d0eba1590a"
695-
integrity sha512-jEFQ8L1tuvPjOI8lnpaf73oCJe+aoxL6ygqSy6c8LcW98zaC+4mzWuQIRCEvKeCOu+lbqdXcg4Uqmm1S8AP1tw==
693+
version "7.0.10"
694+
resolved "https://registry.yarnpkg.com/@types/babel__traverse/-/babel__traverse-7.0.10.tgz#d9a99f017317d9b3d1abc2ced45d3bca68df0daf"
695+
integrity sha512-74fNdUGrWsgIB/V9kTO5FGHPWYY6Eqn+3Z7L6Hc4e/BxjYV7puvBqp5HwsVYYfLm6iURYBNCx4Ut37OF9yitCw==
696696
dependencies:
697697
"@babel/types" "^7.3.0"
698698

@@ -813,39 +813,39 @@
813813
"@types/yargs-parser" "*"
814814

815815
"@typescript-eslint/eslint-plugin@^2.26.0":
816-
version "2.26.0"
817-
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.26.0.tgz#04c96560c8981421e5a9caad8394192363cc423f"
818-
integrity sha512-4yUnLv40bzfzsXcTAtZyTjbiGUXMrcIJcIMioI22tSOyAxpdXiZ4r7YQUU8Jj6XXrLz9d5aMHPQf5JFR7h27Nw==
816+
version "2.27.0"
817+
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.27.0.tgz#e479cdc4c9cf46f96b4c287755733311b0d0ba4b"
818+
integrity sha512-/my+vVHRN7zYgcp0n4z5A6HAK7bvKGBiswaM5zIlOQczsxj/aiD7RcgD+dvVFuwFaGh5+kM7XA6Q6PN0bvb1tw==
819819
dependencies:
820-
"@typescript-eslint/experimental-utils" "2.26.0"
820+
"@typescript-eslint/experimental-utils" "2.27.0"
821821
functional-red-black-tree "^1.0.1"
822822
regexpp "^3.0.0"
823823
tsutils "^3.17.1"
824824

825-
"@typescript-eslint/experimental-utils@2.26.0":
826-
version "2.26.0"
827-
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.26.0.tgz#063390c404d9980767d76274df386c0aa675d91d"
828-
integrity sha512-RELVoH5EYd+JlGprEyojUv9HeKcZqF7nZUGSblyAw1FwOGNnmQIU8kxJ69fttQvEwCsX5D6ECJT8GTozxrDKVQ==
825+
"@typescript-eslint/experimental-utils@2.27.0":
826+
version "2.27.0"
827+
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.27.0.tgz#801a952c10b58e486c9a0b36cf21e2aab1e9e01a"
828+
integrity sha512-vOsYzjwJlY6E0NJRXPTeCGqjv5OHgRU1kzxHKWJVPjDYGbPgLudBXjIlc+OD1hDBZ4l1DLbOc5VjofKahsu9Jw==
829829
dependencies:
830830
"@types/json-schema" "^7.0.3"
831-
"@typescript-eslint/typescript-estree" "2.26.0"
831+
"@typescript-eslint/typescript-estree" "2.27.0"
832832
eslint-scope "^5.0.0"
833833
eslint-utils "^2.0.0"
834834

835835
"@typescript-eslint/parser@^2.26.0":
836-
version "2.26.0"
837-
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.26.0.tgz#385463615818b33acb72a25b39c03579df93d76f"
838-
integrity sha512-+Xj5fucDtdKEVGSh9353wcnseMRkPpEAOY96EEenN7kJVrLqy/EVwtIh3mxcUz8lsFXW1mT5nN5vvEam/a5HiQ==
836+
version "2.27.0"
837+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-2.27.0.tgz#d91664335b2c46584294e42eb4ff35838c427287"
838+
integrity sha512-HFUXZY+EdwrJXZo31DW4IS1ujQW3krzlRjBrFRrJcMDh0zCu107/nRfhk/uBasO8m0NVDbBF5WZKcIUMRO7vPg==
839839
dependencies:
840840
"@types/eslint-visitor-keys" "^1.0.0"
841-
"@typescript-eslint/experimental-utils" "2.26.0"
842-
"@typescript-eslint/typescript-estree" "2.26.0"
841+
"@typescript-eslint/experimental-utils" "2.27.0"
842+
"@typescript-eslint/typescript-estree" "2.27.0"
843843
eslint-visitor-keys "^1.1.0"
844844

845-
"@typescript-eslint/typescript-estree@2.26.0", "@typescript-eslint/typescript-estree@^2.26.0":
846-
version "2.26.0"
847-
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.26.0.tgz#d8132cf1ee8a72234f996519a47d8a9118b57d56"
848-
integrity sha512-3x4SyZCLB4zsKsjuhxDLeVJN6W29VwBnYpCsZ7vIdPel9ZqLfIZJgJXO47MNUkurGpQuIBALdPQKtsSnWpE1Yg==
845+
"@typescript-eslint/typescript-estree@2.27.0", "@typescript-eslint/typescript-estree@^2.26.0":
846+
version "2.27.0"
847+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.27.0.tgz#a288e54605412da8b81f1660b56c8b2e42966ce8"
848+
integrity sha512-t2miCCJIb/FU8yArjAvxllxbTiyNqaXJag7UOpB5DVoM3+xnjeOngtqlJkLRnMtzaRcJhe3CIR9RmL40omubhg==
849849
dependencies:
850850
debug "^4.1.1"
851851
eslint-visitor-keys "^1.1.0"
@@ -5158,9 +5158,9 @@ prettier-linter-helpers@^1.0.0:
51585158
fast-diff "^1.1.2"
51595159

51605160
prettier@^2.0.0:
5161-
version "2.0.3"
5162-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.3.tgz#9a06f0e94a51420e78b6925568b5bec72afe41ea"
5163-
integrity sha512-5qpBDBHO9fpE0zruKiTZm8Gxmz7kknO+WlQR/ivV+RMwgDw/WjOgmxLDn66MPrxq/WZPx/EgEZzh87xJO5E6Fw==
5161+
version "2.0.4"
5162+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.4.tgz#2d1bae173e355996ee355ec9830a7a1ee05457ef"
5163+
integrity sha512-SVJIQ51spzFDvh4fIbCLvciiDMCrRhlN3mbZvv/+ycjvmF5E73bKdGfU8QDLNmjYJf+lsGnDBC4UUnvTe5OO0w==
51645164

51655165
pretty-format@^25.2.1, pretty-format@^25.2.6:
51665166
version "25.2.6"
@@ -6785,9 +6785,9 @@ which@^2.0.1, which@^2.0.2:
67856785
isexe "^2.0.0"
67866786

67876787
windows-release@^3.1.0:
6788-
version "3.2.0"
6789-
resolved "https://registry.yarnpkg.com/windows-release/-/windows-release-3.2.0.tgz#8122dad5afc303d833422380680a79cdfa91785f"
6790-
integrity sha512-QTlz2hKLrdqukrsapKsINzqMgOUpQW268eJ0OaOpJN32h272waxR9fkB9VoWRtK7uKHG5EHJcTXQBD8XZVJkFA==
6788+
version "3.3.0"
6789+
resolved "https://registry.yarnpkg.com/windows-release/-/windows-release-3.3.0.tgz#dce167e9f8be733f21c849ebd4d03fe66b29b9f0"
6790+
integrity sha512-2HetyTg1Y+R+rUgrKeUEhAG/ZuOmTrI1NBb3ZyAGQMYmOJjBBPe4MTodghRkmLJZHwkuPi02anbeGP+Zf401LQ==
67916791
dependencies:
67926792
execa "^1.0.0"
67936793

0 commit comments

Comments
 (0)