UNPKG

macoolka-i18n

Version:

`macoolka-i18n` is a library for internationalization in TypeScript. It easily integrates some localization features to your Application.

95 lines (94 loc) 2.88 kB
/** * I18n type * @desczh * I18N类型 * @file */ import { MonadI18N } from './i18nFormat/stand'; import { Message } from './i18nFormat/types'; import * as E from 'fp-ts/Either'; export { Message, MonadI18N, }; /** * The define i18n locale and id and message * The Key is locale,Value is Record<ID,Message> * @desczh * i18n语言包 * * 定义了语言以及对应的消息id和消息格式。 * @example * import { I18NLanguagePackage } from 'macoolka-i18n' * const languagePackage={ * en: { * "macoolka.i18n.errors.LanguageNotFound": "Must provide a default locale({locale})", * "macoolka.i18n.errors.MessageNotFound": "Message ({id}) not found in locale({locale})" * }, * zh: { * "macoolka.i18n.errors.LanguageNotFound": "缺省语言({locale})没有发现", * "macoolka.i18n.errors.MessageNotFound": "消息({id})没有定义,请核对语言包({locale})" * } * } * * @since 0.2.0 */ export interface I18NLanguagePackage { [locale: string]: { [id: string]: string; }; } /** * Provide param when parse message * @desczh * 解析消息时用到的参数 * @since 0.2.0 */ export interface I18NOption { /** * default language * @desczh * 缺省语言 */ defaultLocale: string; /** * language Package * @desczh * 预定义的消息 */ data?: I18NLanguagePackage; } /** * Monoid for I18NValidation * @since 0.2.3 */ export declare const I18NValidationMonoid: <A>(empty: A) => import("fp-ts/lib/Monoid").Monoid<E.Either<string, A>>; /** * Build a MonadI18N from I18NOption * @desczh * 从I18NOption到MonidI18N * @example * import { buildi18n,setGlobalI18N} from 'macoolka-i18n' * const options = { * defaultLanguage: 'en', * data: { * en: { * 'macoolka.test.noparam': 'noparam', * 'macoolka.test.oneparam': 'one params {value}', * }, * zh: { * 'macoolka.test.noparam': '没有参数', * 'macoolka.test.oneparam': '一个参数 {value}' * } * } * } * const formatMessage = buildi18n<keyof typeof options.data.en>(options).formatMessage * * const formatNoParam = formatI18N({ id: 'macoolka.test.noparam', value: { value: 0 } }) * * expect(formatMessage({ id: 'macoolka.test.noparam', value: { value: 0 } })).toEqual('noparam') * expect(formatMessage({ id: 'macoolka.test.oneparam', value: { value: 111 } })).toEqual('one params 111') * setGlobalI18N({locale:'zh'}) * expect(formatMessage({ id: 'macoolka.test.noparam', value: { value: 0 } })).toEqual('没有参数') * expect(formatMessage({ id: 'macoolka.test.oneparam', value: { value: 111 } })).toEqual('一个参数 111') * @since 0.2.0 */ export declare const buildi18n: <K extends string>(a: I18NOption) => MonadI18N<K>; export default buildi18n;