Skip to content

Commit

Permalink
feat(lib): adjust t with a custom bind
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelly-wu committed Apr 30, 2024
1 parent 963c5a1 commit 0d6e312
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
27 changes: 6 additions & 21 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -53,10 +47,7 @@ function setI18n(namespace: string, stateProp: Parameters<SetI18n>[0]) {
...newState,
}

state = {
...state,
[namespace]: newCurrentState,
}
state[namespace] = newCurrentState

return newCurrentState
}
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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,
}
}
35 changes: 32 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 基础正则
Expand All @@ -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
}

Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 0d6e312

Please sign in to comment.