国际化

更新时间:
复制为 MD 格式

自定义可视化 @quickbi/bi-open-sdk@quickbi/bi-open-react-sdk@quickbi/bi-open-vue-sdk@quickbi/bi-open-menu-sdk等 SDK 提供了用于处理国际化的 I18n工具类,会自动根据当前宿主语言切换语言

import { I18n } from '@quickbi/bi-open-sdk';
import { I18n } from '@quickbi/bi-open-react-sdk';

I18n.init:创建国际化实例的静态方法,返回I18nInstance实例

static init(options: IOption, appendTarget?: object) => I18nInstance

options:配置项参数。

type Lng = "zh-CN" | "en-US" | "zh-TW"
interface IOption {
    lng?: Lng,
  resources: Partial<{
      [key in Lng]: {
        [key: string]: string
    }
  }>
}

lng:设置当前语言,如果没有传入,则当前语言默认为Quick BI当前语言。

resources:语言包配置。与词条的keyi18n.t函数的第一个参数一一对应。

import { I18n } form '@quickbi/bi-open-sdk'

const i18n = I18n.init({
  // lng: 'zh-CN', // 默认当前宿主页当前语言
  resources: {
    // 英文 key
    'en-US': {
      我的: 'my',
      苹果: 'apple',
      '{{who}}{{count}}个苹果': '{{who}} {{count}} apple',
    },
  },
})

I18nInstance.t:翻译函数。

(text: string, param?: object) => string;

text:需要翻译的文字。

param:动态文本参数。

示例如下:

const i18n = I18n.init({})

i18n.t('苹果');

i18n.t('{{who}}{{count}}个苹果', {
  who: t('我的'),
  count: 2
});