diff --git a/src/lib/index.ts b/src/lib/index.ts index ebe7dfe..6ef141f 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,5 +1,5 @@ import { state, getCurrentState, translateImpl, defineT } from './utils' -import { I18nState, SetI18n, Translate, WithI18n } from './type' +import { Condition, I18nState, SetI18n, Translate, WithI18n } from './type' export { Langs, I18nState, SetI18n, Translate, WithI18n, Config } from './type' /** @@ -52,21 +52,6 @@ function setI18n(namespace: string, stateProp: Parameters[0]) { return newCurrentState } -/** - * Get the internationalized text based on the Original text - * @param namespace Current namespace - * @param text Original text - * @param args Dynamic parameter - */ -function translate( - namespace: string, - key: null | string, - text: string, - ...args: Array -): string { - return translateImpl(getCurrentState(namespace), key, text, ...args) -} - /** * Gets the i18n function independent of the main program * @@ -76,12 +61,13 @@ function translate( * @returns */ function withI18n(namespace: string, locale: string): { t: Translate } { - const state = { - ...getCurrentState(namespace), + const condition = { + namespace, locale, } + return { - t: defineT(translateImpl.bind(null, state, null), false, namespace, locale), + t: defineT(translateImpl.bind(null, condition, null), condition), } } @@ -113,9 +99,14 @@ export function initI18n(stateProp: I18nState) { ...(stateProp || {}), } + const condition: Condition = { + namespace, + locale: null, + } + return { setI18n: setI18n.bind(null, namespace) as SetI18n, - t: defineT(translate.bind(null, namespace, null), true, namespace), + t: defineT(translateImpl.bind(null, condition, null), condition), withI18n: withI18n.bind(null, namespace) as WithI18n, } } diff --git a/src/lib/type.ts b/src/lib/type.ts index 54b56ad..7e35dfe 100644 --- a/src/lib/type.ts +++ b/src/lib/type.ts @@ -137,3 +137,8 @@ export interface Translate { * @returns */ export type WithI18n = (locale: string) => { t: Translate } + +export type Condition = { + namespace: string + locale: null | string +} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 972a977..320d63b 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,7 +4,7 @@ import { invalidPluralFormatterRegex, tagFormatterNameMap, } from './constants' -import { I18nState, Translate } from './type' +import { Condition, I18nState, Translate } from './type' export const state = {} @@ -25,36 +25,23 @@ export function getTargetRegExp(regExp: RegExp, index: number) { ) } -export function defineT( - t: Translate, - defineBind: boolean, - namespace: string, - locale?: string, -): Translate { - Object.defineProperty(t, 't', { - get() { - const state = getCurrentState(namespace) - return translateImpl.bind( - null, - typeof locale === 'string' ? { ...state, locale } : state, - ) +export function defineT(t: Translate, condition: Condition): Translate { + Object.defineProperties(t, { + t: { + get() { + return translateImpl.bind(null, condition) + }, }, - }) - - if (defineBind) { - Object.defineProperty(t, 'bind', { + bind: { get() { return () => { - const state = getCurrentState(namespace) - const newT = (...args) => - translateImpl.bind(null, state, null, ...args) - defineT(newT as Translate, false, namespace) + const newT = translateImpl.bind(null, condition, null) + defineT(newT as Translate, condition) return newT } }, - }) - } - + }, + }) return t } @@ -71,9 +58,19 @@ export function getTextFromFormatter(props: { arg: unknown // 动态参数值 text: string // 待处理的文案 state: I18nState // Internationalization state + condition: Condition }): string { - const { type, originText, matchTagRes, arg, text: textProp, state } = props - const { locale } = state + const { + type, + originText, + matchTagRes, + arg, + text: textProp, + state, + condition, + } = props + const { locale: locale_ } = state + const locale = condition.locale || locale_ const [, matchTemplate, f, keyword = ''] = matchTagRes const formatterName = tagFormatterNameMap[f?.toLocaleLowerCase()] const formatter = state[formatterName] @@ -97,7 +94,7 @@ export function getTextFromFormatter(props: { const content = formatter({ locale, payload: arg, - t: defineT(translateImpl.bind(null, state, null), false, state.namespace), + t: defineT(translateImpl.bind(null, condition, null), condition), ...(() => { let res = {} if (type === 'plural') { @@ -131,12 +128,14 @@ export function getTextFromFormatter(props: { * @returns */ export function translateImpl( - i18nState: I18nState, + condition: Condition, key: null | string = null, text: string, ...args: Array ) { - const { locale, langs, beginIndex = 0 } = i18nState + const i18nState = getCurrentState(condition.namespace) + const { locale: locale_, langs, beginIndex = 0 } = i18nState + const locale = condition.locale || locale_ const lang = langs?.[locale] let originText = text @@ -188,6 +187,7 @@ export function translateImpl( arg, text, state: i18nState, + condition, }) })