11import { nextTick } from 'vue'
22import { createI18n } from 'vue-i18n'
33
4- import type { I18n , I18nOptions , Composer } from 'vue-i18n'
4+ import type { I18n , I18nOptions , Locale , VueI18n , Composer } from 'vue-i18n'
55
66export const SUPPORT_LOCALES = [ 'en' , 'ja' ]
77
8+ 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
12+ }
13+
14+ export function setLocale ( i18n : I18n , locale : Locale ) : void {
15+ if ( i18n . mode === 'legacy' ) {
16+ ; ( i18n . global as unknown as VueI18n ) . locale = locale
17+ } else {
18+ ; ( i18n . global as unknown as Composer ) . locale . value = locale
19+ }
20+ }
21+
822export function setupI18n ( options : I18nOptions = { locale : 'en' } ) : I18n {
923 const i18n = createI18n ( options )
1024 setI18nLanguage ( i18n , options . locale ! )
1125 return i18n
1226}
1327
14- export function setI18nLanguage ( i18n : I18n , locale : string ) : void {
15- if ( i18n . mode === 'legacy' ) {
16- i18n . global . locale = locale
17- } else {
18- ; ( ( i18n . global as unknown ) as Composer ) . locale . value = locale
19- }
28+ export function setI18nLanguage ( i18n : I18n , locale : Locale ) : void {
29+ setLocale ( i18n , locale )
2030 /**
2131 * NOTE:
2232 * If you need to specify the language setting for headers, such as the `fetch` API, set it here.
@@ -27,12 +37,17 @@ export function setI18nLanguage(i18n: I18n, locale: string): void {
2737 document . querySelector ( 'html' ) ! . setAttribute ( 'lang' , locale )
2838}
2939
30- export async function loadLocaleMessages ( i18n : I18n , locale : string ) {
40+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41+ const getResourceMessages = ( r : any ) => r . default || r
42+
43+ export async function loadLocaleMessages ( i18n : I18n , locale : Locale ) {
3144 // load locale messages
32- const messages = await import ( /* @vite -ignore */ `./locales/${ locale } .yaml` )
45+ const messages = await import (
46+ /* @vite -ignore */ `./locales/${ locale } .json`
47+ ) . then ( getResourceMessages )
3348
3449 // set locale and locale message
35- i18n . global . setLocaleMessage ( locale , messages . default )
50+ i18n . global . setLocaleMessage ( locale , messages )
3651
3752 return nextTick ( )
3853}
0 commit comments