Skip to content

Commit 4a56ffe

Browse files
lcjnilkazupon
authored andcommitted
Fix: Synchronous Watcher Issue Caused by Locale Setting Order (#2104)
1 parent 0f9171e commit 4a56ffe

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

packages/vue-i18n-core/src/composer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,17 +2110,17 @@ export function createComposer(options: any = {}): any {
21102110
const locale = computed({
21112111
get: () => _locale.value,
21122112
set: val => {
2113+
_context.locale = val
21132114
_locale.value = val
2114-
_context.locale = _locale.value
21152115
}
21162116
})
21172117

21182118
// fallbackLocale
21192119
const fallbackLocale = computed({
21202120
get: () => _fallbackLocale.value,
21212121
set: val => {
2122+
_context.fallbackLocale = val
21222123
_fallbackLocale.value = val
2123-
_context.fallbackLocale = _fallbackLocale.value
21242124
updateFallbackLocale(_context, _locale.value, val)
21252125
}
21262126
})

packages/vue-i18n-core/test/composer.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,35 @@ describe('locale', () => {
6161
test('reactivity', async () => {
6262
const fn = vi.fn()
6363

64-
const { locale } = createComposer({})
64+
const { locale, t } = createComposer({
65+
messages: {
66+
en: {
67+
test: 'en'
68+
},
69+
'en-US': {
70+
test: 'en-US'
71+
}
72+
}
73+
})
6574
watch(locale, fn)
6675
locale.value = 'en'
6776
await nextTick()
6877

6978
expect(fn).toBeCalled()
7079
expect(fn.mock.calls[0][0]).toEqual('en')
7180
expect(fn.mock.calls[0][1]).toEqual('en-US')
81+
82+
let result = ''
83+
locale.value = 'en'
84+
85+
watch(locale, () => (result = t('test')), {
86+
immediate: true,
87+
flush: 'sync'
88+
})
89+
expect(result).toEqual('en')
90+
locale.value = 'en-US'
91+
await nextTick()
92+
expect(result).toEqual('en-US')
7293
})
7394
})
7495

0 commit comments

Comments
 (0)