Skip to content

Latest commit

 

History

History
249 lines (231 loc) · 7.62 KB

API.md

File metadata and controls

249 lines (231 loc) · 7.62 KB

Function API

Table of Contents

  Function List
    initI18n
      Type
      Parameter Description
    t
      Type
      Parameter Description
    setI18n
      Type
      Parameter Description
    withI18n
      Type
      Parameter Description
  Other Types
    I18nState
    FormatFunc
    FormatDateFunc
    FormatPluralFunc

Function List

initI18n

Initialize a fixed configuration to get the core API

Type

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

Parameter Description

Parameter name Description
namespace Specify the naming space
locale Specify the current language

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

t

Get Internationalization Text
The internal will obtain Translation Text corresponding to text from the current language locale langs , and the content of the corresponding translation will directly display text

Type

(
  text: string,
  ...args: Array<string|number|unknown>
) => string

Parameter Description

Parameter name Description
text The text to be translated should meet specific Matching 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

setI18n

Set language and language package

Type

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

Parameter Description

Parameter name Description
locale Specify the current language
langs Set the current language package to support incremental addition, and the new one will cover the merger to the original

withI18n

Get the t function independent of the main program order
It is applicable to the server. Each interface response needs to be internationalized

Type

(
  props:{
    locale: string
  }
) => ({ t })

Parameter Description

Parameter name Description
locale Specify the current language

Other Types

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

I18nState

State under the namespace

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

FormatFunc

Common format callback type

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

FormatDateFunc

Format callback function type of Date(Time)

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

FormatPluralFunc

Format callback function type of Plural

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