Internationalization

Updated at:

The custom visualization SDKs—including @quickbi/bi-open-sdk, @quickbi/bi-open-react-sdk, @quickbi/bi-open-vue-sdk, and @quickbi/bi-open-menu-sdk—provide an I18n utility class that automatically switches languages to match the host environment.

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

I18n.init is a static method that creates an I18nInstance.

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

options: The configuration object.

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

lng: The display language. Defaults to the current Quick BI language if not specified.

resources: The language pack. Each key maps to the first parameter of i18n.t.

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

const i18n = I18n.init({
  // lng: 'en-US', // Defaults to the current language of the host page.
  resources: {
    // English keys
    'en-US': {
      'my': 'my',
      'apple': 'apple',
      'apple_count': '{{who}} has {{count}} apples',
    },
  },
})

I18nInstance.t: Translates a key into the active locale string.

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

text: The key to translate.

param: Dynamic interpolation values.

Example:

const i18n = I18n.init({})

i18n.t('apple');

i18n.t('{{who}}{{count}} apples', {
  who: t('my'),
  count: 2
});