Skip to content

Commit 702783a

Browse files
committed
update lazy example
1 parent 8918cbd commit 702783a

File tree

1 file changed

+24
-8
lines changed
  • examples/lazy-loading/vite/src

1 file changed

+24
-8
lines changed

examples/lazy-loading/vite/src/i18n.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
import { nextTick } from 'vue'
1+
import { nextTick, isRef } from 'vue'
22
import { createI18n } from 'vue-i18n'
33

4-
import type { I18n, I18nOptions, Locale, VueI18n, Composer } from 'vue-i18n'
4+
import type {
5+
I18n,
6+
I18nOptions,
7+
Locale,
8+
VueI18n,
9+
Composer,
10+
I18nMode
11+
} from 'vue-i18n'
512

613
export const SUPPORT_LOCALES = ['en', 'ja']
714

15+
function isComposer(
16+
instance: VueI18n | Composer,
17+
mode: I18nMode
18+
): instance is Composer {
19+
return mode === 'composition' && isRef(instance.locale)
20+
}
21+
822
export function getLocale(i18n: I18n): string {
9-
return i18n.mode === 'legacy'
10-
? (i18n.global as unknown as VueI18n).locale
11-
: (i18n.global as unknown as Composer).locale.value
23+
if (isComposer(i18n.global, i18n.mode)) {
24+
return i18n.global.locale.value
25+
} else {
26+
return i18n.global.locale
27+
}
1228
}
1329

1430
export function setLocale(i18n: I18n, locale: Locale): void {
15-
if (i18n.mode === 'legacy') {
16-
;(i18n.global as unknown as VueI18n).locale = locale
31+
if (isComposer(i18n.global, i18n.mode)) {
32+
i18n.global.locale.value = locale
1733
} else {
18-
;(i18n.global as unknown as Composer).locale.value = locale
34+
i18n.global.locale = locale
1935
}
2036
}
2137

0 commit comments

Comments
 (0)