Skip to content

Commit d1b75f3

Browse files
authored
fix: cannot locale change for specified i18n custom blocks only (#192)
* fix: cannot locale change for specified i18n custom blocks only * add jest setup for e2e
1 parent bbf2486 commit d1b75f3

File tree

3 files changed

+58
-62
lines changed

3 files changed

+58
-62
lines changed

jest.e2e.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ module.exports = {
128128
// setupFiles: [],
129129

130130
// A list of paths to modules that run some code to configure or set up the testing framework before each test
131-
// setupFilesAfterEnv: [],
131+
setupFilesAfterEnv: ['./jest.e2e.setup.js'],
132132

133133
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
134134
// snapshotSerializers: [],

jest.e2e.setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.setTimeout(30000)

src/mixin.ts

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
2828
composer: Composer<Messages, DateTimeFormats, NumberFormats>,
2929
i18n: I18nInternal
3030
): ComponentOptions {
31-
const legacy = (vuei18n as unknown) as VueI18nInternal<
32-
Messages,
33-
DateTimeFormats,
34-
NumberFormats
35-
>
3631
return {
3732
beforeCreate(): void {
3833
const instance = getCurrentInstance()
@@ -50,70 +45,29 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
5045
}
5146
optionsI18n.__root = composer
5247
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)
10349
} else {
10450
this.$i18n = createVueI18n(optionsI18n)
10551
}
10652
} 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+
}
11161
} else {
11262
// set global
113-
this.$i18n = legacy
63+
this.$i18n = vuei18n
11464
}
11565

116-
legacy.__onComponentInstanceCreated(this.$i18n)
66+
;((vuei18n as unknown) as VueI18nInternal<
67+
Messages,
68+
DateTimeFormats,
69+
NumberFormats
70+
>).__onComponentInstanceCreated(this.$i18n)
11771
i18n.__setInstance<
11872
Messages,
11973
DateTimeFormats,
@@ -138,6 +92,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
13892
},
13993

14094
mounted(): void {
95+
/* istanbul ignore if */
14196
if ((__DEV__ || __FEATURE_PROD_DEVTOOLS__) && !__NODE_JS__) {
14297
this.$el.__INTLIFY__ = this.$i18n.__composer
14398
const emitter: DevToolsEmitter = (this.__emitter = createEmitter<
@@ -160,6 +115,7 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
160115
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
161116
}
162117

118+
/* istanbul ignore if */
163119
if ((__DEV__ || __FEATURE_PROD_DEVTOOLS__) && !__NODE_JS__) {
164120
if (this.__emitter) {
165121
this.__emitter.off('*', addTimelineEvent)
@@ -186,3 +142,42 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
186142
}
187143
}
188144
}
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

Comments
 (0)