From 0d6e312a4af0a7b4d00ddeb0b9277d66f06bb984 Mon Sep 17 00:00:00 2001 From: Eyelly Wu Date: Tue, 30 Apr 2024 21:44:21 +0800 Subject: [PATCH] feat(lib): adjust t with a custom bind --- src/lib/index.ts | 27 ++++++--------------------- src/lib/utils.ts | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index f5ac529..ebe7dfe 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,13 +1,7 @@ -import { translateImpl, defineT } from './utils' +import { state, getCurrentState, translateImpl, defineT } from './utils' import { I18nState, SetI18n, Translate, WithI18n } from './type' export { Langs, I18nState, SetI18n, Translate, WithI18n, Config } from './type' -let state = {} as I18nState - -function getCurrentState(namespace: string) { - return state[namespace] || {} -} - /** * Sets or updates the internationalization state * @param namespace Current namespace @@ -53,10 +47,7 @@ function setI18n(namespace: string, stateProp: Parameters[0]) { ...newState, } - state = { - ...state, - [namespace]: newCurrentState, - } + state[namespace] = newCurrentState return newCurrentState } @@ -90,7 +81,7 @@ function withI18n(namespace: string, locale: string): { t: Translate } { locale, } return { - t: defineT(translateImpl.bind(null, state, null), state), + t: defineT(translateImpl.bind(null, state, null), false, namespace, locale), } } @@ -118,19 +109,13 @@ export function initI18n(stateProp: I18nState) { delete stateProp.beginIndex } - state = { - ...state, - [namespace]: { - ...(stateProp || {}), - }, + state[namespace] = { + ...(stateProp || {}), } return { setI18n: setI18n.bind(null, namespace) as SetI18n, - t: defineT( - translate.bind(null, namespace, null), - getCurrentState(namespace), - ), + t: defineT(translate.bind(null, namespace, null), true, namespace), withI18n: withI18n.bind(null, namespace) as WithI18n, } } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 5f0aec7..972a977 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -6,6 +6,12 @@ import { } from './constants' import { I18nState, Translate } from './type' +export const state = {} + +export function getCurrentState(namespace: string) { + return state[namespace] || {} +} + /** * 获取目标正则 * @param regExp 基础正则 @@ -19,13 +25,36 @@ export function getTargetRegExp(regExp: RegExp, index: number) { ) } -export function defineT(t: Translate, state: I18nState): Translate { +export function defineT( + t: Translate, + defineBind: boolean, + namespace: string, + locale?: string, +): Translate { Object.defineProperty(t, 't', { get() { - return translateImpl.bind(null, state) + const state = getCurrentState(namespace) + return translateImpl.bind( + null, + typeof locale === 'string' ? { ...state, locale } : state, + ) }, }) + if (defineBind) { + Object.defineProperty(t, 'bind', { + get() { + return () => { + const state = getCurrentState(namespace) + const newT = (...args) => + translateImpl.bind(null, state, null, ...args) + defineT(newT as Translate, false, namespace) + return newT + } + }, + }) + } + return t } @@ -68,7 +97,7 @@ export function getTextFromFormatter(props: { const content = formatter({ locale, payload: arg, - t: defineT(translateImpl.bind(null, state, null), state), + t: defineT(translateImpl.bind(null, state, null), false, state.namespace), ...(() => { let res = {} if (type === 'plural') {