import { ref , reactive , inject , computed } from 'vue' ; import { isString } from '../_utils/is' ; import { configProviderInjectionKey } from '../config-provider/context' ; import type { ArcoI18nMessages , ArcoLang } from './interface' ; import zhCN from './lang/zh-cn' ; const LOCALE = ref ( 'zh-CN' ) ; const I18N_MESSAGES = reactive < ArcoI18nMessages > ( { 'zh-CN' : zhCN } ) ; param messages param options export const addI18nMessages = ( messages , options ? ) => { for ( const key of Object . keys ( messages ) ) { if ( ! I18N_MESSAGES [ key ] || options ?. overwrite ) { I18N_MESSAGES [ key ] = messages [ key ] ; } } } ; param locale export const useLocale = ( locale ) => { if ( ! I18N_MESSAGES [ locale ] ) { console . warn ( `use ${ locale } failed! Please add ${ locale } first` ) ; return ; } LOCALE . value = locale ; } ; /**
 * 获取当前的地区语言
 */ export const getLocale = ( ) => { return LOCALE . value ; } ; export const useI18n = ( ) => { const configProvider = inject ( configProviderInjectionKey , undefined ) ; const i18nMessage = computed < ArcoLang > ( ( ) => configProvider ?. locale ?? I18N_MESSAGES [ LOCALE . value ] ) ; const locale = computed ( ( ) => i18nMessage . value . locale ) ; const transform = ( key , ... args ) : string => { const keyArray = key . split ( '.' ) ; let temp = i18nMessage . value ; for ( const keyItem of keyArray ) { if ( ! temp [ keyItem ] ) { return key ; } temp = temp [ keyItem ] ; } if ( isString ( temp ) ) { if ( args . length > 0 ) { return temp . replace ( /{(\d+)}/g , ( sub , index ) => args [ index ] ?? sub ) ; } return temp ; } return temp ; } ; return { locale , t : transform } ; } ;