Skip to content

Commit

Permalink
chore(lib): adjust defineT and implementation of translate
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelly-wu committed May 4, 2024
1 parent aff7c84 commit e1a57dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 50 deletions.
31 changes: 11 additions & 20 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -52,21 +52,6 @@ function setI18n(namespace: string, stateProp: Parameters<SetI18n>[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 | number | unknown>
): string {
return translateImpl(getCurrentState(namespace), key, text, ...args)
}

/**
* Gets the i18n function independent of the main program
*
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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,
}
}
5 changes: 5 additions & 0 deletions src/lib/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ export interface Translate {
* @returns
*/
export type WithI18n = (locale: string) => { t: Translate }

export type Condition = {
namespace: string
locale: null | string
}
60 changes: 30 additions & 30 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
invalidPluralFormatterRegex,
tagFormatterNameMap,
} from './constants'
import { I18nState, Translate } from './type'
import { Condition, I18nState, Translate } from './type'

export const state = {}

Expand All @@ -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
}

Expand All @@ -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]
Expand All @@ -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') {
Expand Down Expand Up @@ -131,12 +128,14 @@ export function getTextFromFormatter(props: {
* @returns
*/
export function translateImpl(
i18nState: I18nState,
condition: Condition,
key: null | string = null,
text: string,
...args: Array<string | number | unknown>
) {
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

Expand Down Expand Up @@ -188,6 +187,7 @@ export function translateImpl(
arg,
text,
state: i18nState,
condition,
})
})

Expand Down

0 comments on commit e1a57dd

Please sign in to comment.