Function API

September 6, 2025 · View on GitHub

Table of Contents

  Function List
    initI18n
      Type
      Parameter Description
    t
      Type
      Parameter Description
      Property
        t
          Type
          Parameter Description
        withLocale
          Type
          Parameter Description
    setI18n
      Type
      Parameter Description
  Other Types
    LangPack
    I18nState
    FormatFunc
    FormatDateFunc
    FormatPluralFunc

Function List

initI18n

Initialize configuration to get core API

Type

(
  props: {
    namespace: string,
    locale?: string,
    langs?: Record<string, (() => Promise<LangPack>) | LangPack>,
    beginIndex?: number,
    formatNumber?: FormatFunc,
    formatCurrency?: FormatFunc,
    formatDate?: FormatDateFunc,
    formatTime?: FormatDateFunc,
    formatPlural?: FormatPluralFunc,
  }
) => ({
  t,
  setI18n,
})

Parameter Description

Parameter Name Description
namespace Specify the namespace
locale Specify the current language

📢📢📢:The value of locale is the same as the language code by default. If you need to customize, please refer to the usage of codeLocaleMap
langs Set the current language pack
beginIndex Set the starting index for Interpolation Variable in the t function, default is 0
formatNumber Formatter for Number type Interpolation Variable , the corresponding type tag is n or N
formatCurrency Formatter for Currency type Interpolation Variable , the corresponding type tag is c or C
formatDate Formatter for Date type Interpolation Variable , the corresponding type tag is d or D
formatTime Formatter for Time type Interpolation Variable , the corresponding type tag is t or T
formatPlural Formatter for Plural type Interpolation Variable , the corresponding type tag is p or P

t

Get internationalized text
Internally, it obtains the text corresponding to the text from the language packs ( langs ) based on the current language locale . If no matching translation is found, the original content text will be displayed directly

Type

interface Translate {
  (text: string, ...args: Array<string | number | unknown>): string
  t: (
    key: string,
    text: string,
    ...args: Array<string | number | unknown>
  ) => string
  withLocale: (locale?: string) => Translate
}

Parameter Description

Parameter Name Description
text The text to be translated should meet specific Match Rules requirements
args Represents Interpolation Variable , with no limit on the number. text needs to be received in the form of {index} in the text, index represents the position of Interpolation Variable , starting from 0 (can be customized in initI18n ). The first parameter corresponds to 0, and 2 parameters correspond to 1, and so on

Property

t
Get internationalized text for custom-key
Internally, it obtains the text corresponding to the key from the language packs ( langs ) based on the current language locale . If no matching translation is found, the original content text will be displayed directly
Type
(
  key: string,
  text: string,
  ...args: Array<string | number | unknown>
) => string
Parameter Description
Parameter Name Description
key Custom key, need to meet specific Match Rules requirements
text The text to be translated should meet specific Match Rules requirements
args Represents Interpolation Variable , with no limit on the number. text needs to be received in the form of {index} in the text, index represents the position of Interpolation Variable , starting from 0 (can be customized in initI18n ). The first parameter corresponds to 0, and 2 parameters correspond to 1, and so on
withLocale

Generate a new t function
For server-side scenarios where each API response requires internationalization

Type
(
  locale?: string,
) => Translate
Parameter Description
Parameter Name Description
locale Specify the current language
If no language is specified, it will be consistent with the language before generation

setI18n

Set the language and language pack

Type

(
  props: {
    locale?: string,
    langs?: Record<string, LangPack>,
  }
) => Promise<I18nState>

Parameter Description

Parameter Name Description
locale Specify the current language
langs Set the current language pack to support incremental addition, and the new one will be merged and overwrite the original

Other Types

The following types are for convenience in document description, and there may be differences in the type definitions in the code. Please refer to the actual code for accuracy

LangPack

Language Pack

type LangPack = Record<string, string>

I18nState

Namespace state

type I18nState = {
  namespace: string
  locale?: string
  langs?: Record<string, (() => Promise<LangPack>) | LangPack>
  beginIndex?: number
  formatNumber?: FormatFunc,
  formatCurrency?: FormatFunc,
  formatDate?: FormatDateFunc,
  formatTime?: FormatDateFunc,
  formatPlural?: FormatPluralFunc,
}

FormatFunc

Common Formatter type

type FormatFunc = <T>(props: {
  /**
   * Current language
   */
  locale: string,
  /**
   * Interpolation Variable
   */
  payload: string | number | unknown | T,
  /**
   * t  function
   */
  t: t,
}) => number | string

FormatDateFunc

Formatter type of date (time)

type FormatDateFunc = <T>(props: {
  /**
   * Current language
   */
  locale: string,
  /**
   * Interpolation Variable
   */
  payload: string | number | Date | unknown | T,
  /**
   * t  function
   */
  t: t,
}) => string

FormatPluralFunc

Formatter type of plural

type FormatPluralFunc = <T>(props: {
  /**
   * Current language
   */
  locale: string,
  /**
   * Interpolation Variable
   */
  payload: string | number | unknown | T,
  /**
   * A string that combines quantifiers and nouns by default. Languages that do not require plural processing can return this property directly
   */
  text: string
  /**
   * Plural keyword
   */
  keyword: string
  /**
   * t  function
   */
  t: t,
 }) => string