diff --git a/packages/inula-intl/index.ts b/packages/inula-intl/index.ts index da43b69b..0d9c6267 100644 --- a/packages/inula-intl/index.ts +++ b/packages/inula-intl/index.ts @@ -36,7 +36,7 @@ export { // 组件 export { FormattedMessage, - I18nContext, + I18nContext as IntlContext, I18nProvider as IntlProvider, injectIntl as injectIntl, InjectProvider as RawIntlProvider, diff --git a/packages/inula-intl/src/format/Translation.ts b/packages/inula-intl/src/format/Translation.ts index b1f57f79..d627d35d 100644 --- a/packages/inula-intl/src/format/Translation.ts +++ b/packages/inula-intl/src/format/Translation.ts @@ -16,8 +16,8 @@ import { UNICODE_REG } from '../constants'; import { CompiledMessage, Locale, LocaleConfig, Locales } from '../types/types'; import generateFormatters from './generateFormatters'; -import {FormatOptions, I18nCache} from '../types/interfaces'; -import {createIntlCache} from "../../index"; +import { FormatOptions, I18nCache } from '../types/interfaces'; +import creatI18nCache from './cache/cache'; /** * 获取翻译结果 @@ -34,7 +34,7 @@ class Translation { this.locale = locale; this.locales = locales; this.localeConfig = localeConfig; - this.cache = cache ?? createIntlCache; + this.cache = cache ?? creatI18nCache(); } /** @@ -47,7 +47,7 @@ class Translation { locales: Locales, values: object, formatOptions: FormatOptions, - localeConfig: LocaleConfig, + localeConfig: LocaleConfig ) => { const textFormatter = (name: string, type: string, format: any) => { const formatters = generateFormatters(locale, locales, localeConfig, formatOptions, this.cache); @@ -67,13 +67,7 @@ class Translation { return textFormatter; }; - let textFormatter = createTextFormatter( - this.locale, - this.locales, - values, - formatOptions, - this.localeConfig, - ); + let textFormatter = createTextFormatter(this.locale, this.locales, values, formatOptions, this.localeConfig); // 通过递归方法formatCore进行格式化处理 const result = this.formatMessage(this.compiledMessage, textFormatter); return result; // 返回要格式化的结果 @@ -84,28 +78,29 @@ class Translation { return compiledMessage; } - return compiledMessage.map(token => { - if (typeof token === 'string') { - return token; - } + return compiledMessage + .map(token => { + if (typeof token === 'string') { + return token; + } - const [name, type, format] = token; + const [name, type, format] = token; + let replaceValueFormat = format; - let replaceValueFormat = format; - - // 如果 format 是对象,函数将递归地对它的每个值调用 formatMessage 后保存,否则直接保存 - if (format && typeof format !== 'string') { - replaceValueFormat = Object.keys(replaceValueFormat).reduce((text, key) => { - text[key] = this.formatMessage(format[key], textFormatter); - return text; - }, {}); - } - //调用 getContent 函数来获取给定 name、type 和 interpolateFormat 的值 - const value = textFormatter(name, type, replaceValueFormat); - return value ?? `{${name}}`; - }).join(''); - }; + // 如果 format 是对象,函数将递归地对它的每个值调用 formatMessage 后保存,否则直接保存 + if (format && typeof format !== 'string') { + replaceValueFormat = Object.keys(replaceValueFormat).reduce((text, key) => { + text[key] = this.formatMessage(format[key], textFormatter); + return text; + }, {}); + } + //调用 getContent 函数来获取给定 name、type 和 interpolateFormat 的值 + const value = textFormatter(name, type, replaceValueFormat); + return value ?? `{${name}}`; + }) + .join(''); + } } export default Translation; diff --git a/packages/inula-intl/src/format/fomatters/PluralFormatter.ts b/packages/inula-intl/src/format/fomatters/PluralFormatter.ts index e16a1091..a1336b00 100644 --- a/packages/inula-intl/src/format/fomatters/PluralFormatter.ts +++ b/packages/inula-intl/src/format/fomatters/PluralFormatter.ts @@ -16,8 +16,8 @@ import utils from '../../utils/utils'; import NumberFormatter from './NumberFormatter'; import { Locale, Locales } from '../../types/types'; -import {I18nCache} from "../../types/interfaces"; -import {createIntlCache} from "../../../index"; +import { I18nCache } from '../../types/interfaces'; +import creatI18nCache from '../cache/cache'; /** * 复数格式化 @@ -34,7 +34,7 @@ class PluralFormatter { this.locales = locales; this.value = value; this.message = message; - this.cache = cache ?? createIntlCache(); + this.cache = cache ?? creatI18nCache(); } // 将 message中的“#”替换为指定数字value,并返回新的字符串或者字符串数组