@@ -28,11 +28,6 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
28
28
composer : Composer < Messages , DateTimeFormats , NumberFormats > ,
29
29
i18n : I18nInternal
30
30
) : ComponentOptions {
31
- const legacy = ( vuei18n as unknown ) as VueI18nInternal <
32
- Messages ,
33
- DateTimeFormats ,
34
- NumberFormats
35
- >
36
31
return {
37
32
beforeCreate ( ) : void {
38
33
const instance = getCurrentInstance ( )
@@ -50,70 +45,29 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
50
45
}
51
46
optionsI18n . __root = composer
52
47
if ( this === this . $root ) {
53
- const rootLegacy = ( legacy as unknown ) as VueI18n <
54
- Messages ,
55
- DateTimeFormats ,
56
- NumberFormats
57
- >
58
- rootLegacy . locale = optionsI18n . locale || rootLegacy . locale
59
- rootLegacy . fallbackLocale =
60
- optionsI18n . fallbackLocale || rootLegacy . fallbackLocale
61
- rootLegacy . missing = optionsI18n . missing || rootLegacy . missing
62
- rootLegacy . silentTranslationWarn =
63
- optionsI18n . silentTranslationWarn || rootLegacy . silentFallbackWarn
64
- rootLegacy . silentFallbackWarn =
65
- optionsI18n . silentFallbackWarn || rootLegacy . silentFallbackWarn
66
- rootLegacy . formatFallbackMessages =
67
- optionsI18n . formatFallbackMessages ||
68
- rootLegacy . formatFallbackMessages
69
- rootLegacy . postTranslation =
70
- optionsI18n . postTranslation || rootLegacy . postTranslation
71
- rootLegacy . warnHtmlInMessage =
72
- optionsI18n . warnHtmlInMessage || rootLegacy . warnHtmlInMessage
73
- rootLegacy . escapeParameterHtml =
74
- optionsI18n . escapeParameterHtml || rootLegacy . escapeParameterHtml
75
- rootLegacy . sync = optionsI18n . sync || rootLegacy . sync
76
- const messages = getLocaleMessages < VueMessageType > (
77
- rootLegacy . locale ,
78
- {
79
- messages : optionsI18n . messages ,
80
- __i18n : optionsI18n . __i18n
81
- }
82
- )
83
- Object . keys ( messages ) . forEach ( locale =>
84
- rootLegacy . mergeLocaleMessage ( locale , messages [ locale ] )
85
- )
86
- if ( optionsI18n . datetimeFormats ) {
87
- Object . keys ( optionsI18n . datetimeFormats ) . forEach ( locale =>
88
- rootLegacy . mergeDateTimeFormat (
89
- locale ,
90
- optionsI18n . datetimeFormats ! [ locale ]
91
- )
92
- )
93
- }
94
- if ( optionsI18n . numberFormats ) {
95
- Object . keys ( optionsI18n . numberFormats ) . forEach ( locale =>
96
- rootLegacy . mergeNumberFormat (
97
- locale ,
98
- optionsI18n . numberFormats ! [ locale ]
99
- )
100
- )
101
- }
102
- this . $i18n = legacy
48
+ this . $i18n = mergeToRoot ( vuei18n , optionsI18n )
103
49
} else {
104
50
this . $i18n = createVueI18n ( optionsI18n )
105
51
}
106
52
} else if ( options . __i18n ) {
107
- this . $i18n = createVueI18n ( {
108
- __i18n : ( options as ComposerInternalOptions < Messages > ) . __i18n ,
109
- __root : composer
110
- } as VueI18nOptions )
53
+ if ( this === this . $root ) {
54
+ this . $i18n = mergeToRoot ( vuei18n , options )
55
+ } else {
56
+ this . $i18n = createVueI18n ( {
57
+ __i18n : ( options as ComposerInternalOptions < Messages > ) . __i18n ,
58
+ __root : composer
59
+ } as VueI18nOptions )
60
+ }
111
61
} else {
112
62
// set global
113
- this . $i18n = legacy
63
+ this . $i18n = vuei18n
114
64
}
115
65
116
- legacy . __onComponentInstanceCreated ( this . $i18n )
66
+ ; ( ( vuei18n as unknown ) as VueI18nInternal <
67
+ Messages ,
68
+ DateTimeFormats ,
69
+ NumberFormats
70
+ > ) . __onComponentInstanceCreated ( this . $i18n )
117
71
i18n . __setInstance <
118
72
Messages ,
119
73
DateTimeFormats ,
@@ -138,6 +92,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
138
92
} ,
139
93
140
94
mounted ( ) : void {
95
+ /* istanbul ignore if */
141
96
if ( ( __DEV__ || __FEATURE_PROD_DEVTOOLS__ ) && ! __NODE_JS__ ) {
142
97
this . $el . __INTLIFY__ = this . $i18n . __composer
143
98
const emitter : DevToolsEmitter = ( this . __emitter = createEmitter <
@@ -160,6 +115,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
160
115
throw createI18nError ( I18nErrorCodes . UNEXPECTED_ERROR )
161
116
}
162
117
118
+ /* istanbul ignore if */
163
119
if ( ( __DEV__ || __FEATURE_PROD_DEVTOOLS__ ) && ! __NODE_JS__ ) {
164
120
if ( this . __emitter ) {
165
121
this . __emitter . off ( '*' , addTimelineEvent )
@@ -186,3 +142,42 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
186
142
}
187
143
}
188
144
}
145
+
146
+ function mergeToRoot < Messages , DateTimeFormats , NumberFormats > (
147
+ root : VueI18n < Messages , DateTimeFormats , NumberFormats > ,
148
+ optoins : VueI18nOptions &
149
+ ComposerInternalOptions < Messages , DateTimeFormats , NumberFormats >
150
+ ) : VueI18n < Messages , DateTimeFormats , NumberFormats > {
151
+ root . locale = optoins . locale || root . locale
152
+ root . fallbackLocale = optoins . fallbackLocale || root . fallbackLocale
153
+ root . missing = optoins . missing || root . missing
154
+ root . silentTranslationWarn =
155
+ optoins . silentTranslationWarn || root . silentFallbackWarn
156
+ root . silentFallbackWarn =
157
+ optoins . silentFallbackWarn || root . silentFallbackWarn
158
+ root . formatFallbackMessages =
159
+ optoins . formatFallbackMessages || root . formatFallbackMessages
160
+ root . postTranslation = optoins . postTranslation || root . postTranslation
161
+ root . warnHtmlInMessage = optoins . warnHtmlInMessage || root . warnHtmlInMessage
162
+ root . escapeParameterHtml =
163
+ optoins . escapeParameterHtml || root . escapeParameterHtml
164
+ root . sync = optoins . sync || root . sync
165
+ const messages = getLocaleMessages < VueMessageType > ( root . locale , {
166
+ messages : optoins . messages ,
167
+ __i18n : optoins . __i18n
168
+ } )
169
+ Object . keys ( messages ) . forEach ( locale =>
170
+ root . mergeLocaleMessage ( locale , messages [ locale ] )
171
+ )
172
+ if ( optoins . datetimeFormats ) {
173
+ Object . keys ( optoins . datetimeFormats ) . forEach ( locale =>
174
+ root . mergeDateTimeFormat ( locale , optoins . datetimeFormats ! [ locale ] )
175
+ )
176
+ }
177
+ if ( optoins . numberFormats ) {
178
+ Object . keys ( optoins . numberFormats ) . forEach ( locale =>
179
+ root . mergeNumberFormat ( locale , optoins . numberFormats ! [ locale ] )
180
+ )
181
+ }
182
+ return root
183
+ }
0 commit comments