1- import { computed , isRef , ref , unref } from 'vue'
2- import { get } from 'lodash-unified'
1+ import { computed , isRef , ref , unref } from 'vue'
2+ import { get } from 'lodash-unified'
33import Chinese from '@/locale/lang/zh-cn'
4- import { useGlobalConfig } from '@/hooks'
5- import type { MaybeRef } from '@vueuse/core'
6- import type { Ref } from 'vue'
7- import type { Language } from '@/locale'
4+ import { useGlobalConfig } from '@/hooks'
5+ import type { MaybeRef } from '@vueuse/core'
6+ import type { Ref } from 'vue'
7+ import type { Language } from '@/locale'
88
99export type TranslatorOption = Record < string , string | number >
1010export type Translator = ( path : string , option ?: TranslatorOption ) => string
@@ -16,18 +16,22 @@ export type LocaleContext = {
1616
1717export const buildTranslator =
1818 ( locale : MaybeRef < Language > ) : Translator =>
19- ( path , option ) =>
20- translate ( path , option , unref ( locale ) )
19+ ( path , option ) =>
20+ translate ( path , option , unref ( locale ) )
2121
2222export const translate = (
2323 path : string ,
2424 option : undefined | TranslatorOption ,
2525 locale : Language
26- ) : string =>
27- ( get ( locale , path , path ) as string ) . replace (
28- / \{ ( \w + ) \} / g,
29- ( _ , key ) => `${ option ?. [ key ] ?? `{${ key } }` } `
30- )
26+ ) : string => {
27+ if ( ! i18nHandler ) {
28+ return ( get ( locale , path , path ) as string ) . replace (
29+ / \{ ( \w + ) \} / g,
30+ ( _ , key ) => `${ option ?. [ key ] ?? `{${ key } }` } `
31+ )
32+ }
33+ return i18nHandler ( path ) ;
34+ }
3135
3236export const buildLocaleContext = (
3337 locale : MaybeRef < Language >
@@ -45,3 +49,9 @@ export const useLocale = () => {
4549 const locale = useGlobalConfig ( 'locale' )
4650 return buildLocaleContext ( computed ( ( ) => locale . value || Chinese ) )
4751}
52+
53+ let i18nHandler : Function ;
54+
55+ export const i18n = ( fn : Function | undefined ) => {
56+ i18nHandler = fn || i18nHandler ;
57+ } ;
0 commit comments