Match-id-b4b1f1fb248382d7292915c784e868a5f48dabec

This commit is contained in:
* 2023-09-25 17:43:12 +08:00
commit ead091a670
3 changed files with 29 additions and 34 deletions

View File

@ -36,7 +36,7 @@ export {
// 组件 // 组件
export { export {
FormattedMessage, FormattedMessage,
I18nContext, I18nContext as IntlContext,
I18nProvider as IntlProvider, I18nProvider as IntlProvider,
injectIntl as injectIntl, injectIntl as injectIntl,
InjectProvider as RawIntlProvider, InjectProvider as RawIntlProvider,

View File

@ -16,8 +16,8 @@
import { UNICODE_REG } from '../constants'; import { UNICODE_REG } from '../constants';
import { CompiledMessage, Locale, LocaleConfig, Locales } from '../types/types'; import { CompiledMessage, Locale, LocaleConfig, Locales } from '../types/types';
import generateFormatters from './generateFormatters'; import generateFormatters from './generateFormatters';
import {FormatOptions, I18nCache} from '../types/interfaces'; import { FormatOptions, I18nCache } from '../types/interfaces';
import {createIntlCache} from "../../index"; import creatI18nCache from './cache/cache';
/** /**
* *
@ -34,7 +34,7 @@ class Translation {
this.locale = locale; this.locale = locale;
this.locales = locales; this.locales = locales;
this.localeConfig = localeConfig; this.localeConfig = localeConfig;
this.cache = cache ?? createIntlCache; this.cache = cache ?? creatI18nCache();
} }
/** /**
@ -47,7 +47,7 @@ class Translation {
locales: Locales, locales: Locales,
values: object, values: object,
formatOptions: FormatOptions, formatOptions: FormatOptions,
localeConfig: LocaleConfig, localeConfig: LocaleConfig
) => { ) => {
const textFormatter = (name: string, type: string, format: any) => { const textFormatter = (name: string, type: string, format: any) => {
const formatters = generateFormatters(locale, locales, localeConfig, formatOptions, this.cache); const formatters = generateFormatters(locale, locales, localeConfig, formatOptions, this.cache);
@ -67,13 +67,7 @@ class Translation {
return textFormatter; return textFormatter;
}; };
let textFormatter = createTextFormatter( let textFormatter = createTextFormatter(this.locale, this.locales, values, formatOptions, this.localeConfig);
this.locale,
this.locales,
values,
formatOptions,
this.localeConfig,
);
// 通过递归方法formatCore进行格式化处理 // 通过递归方法formatCore进行格式化处理
const result = this.formatMessage(this.compiledMessage, textFormatter); const result = this.formatMessage(this.compiledMessage, textFormatter);
return result; // 返回要格式化的结果 return result; // 返回要格式化的结果
@ -84,28 +78,29 @@ class Translation {
return compiledMessage; return compiledMessage;
} }
return compiledMessage.map(token => { return compiledMessage
if (typeof token === 'string') { .map(token => {
return 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') {
// 如果 format 是对象,函数将递归地对它的每个值调用 formatMessage 后保存,否则直接保存 replaceValueFormat = Object.keys(replaceValueFormat).reduce((text, key) => {
if (format && typeof format !== 'string') { text[key] = this.formatMessage(format[key], textFormatter);
replaceValueFormat = Object.keys(replaceValueFormat).reduce((text, key) => { return text;
text[key] = this.formatMessage(format[key], textFormatter); }, {});
return text; }
}, {}); //调用 getContent 函数来获取给定 name、type 和 interpolateFormat 的值
} const value = textFormatter(name, type, replaceValueFormat);
//调用 getContent 函数来获取给定 name、type 和 interpolateFormat 的值 return value ?? `{${name}}`;
const value = textFormatter(name, type, replaceValueFormat); })
return value ?? `{${name}}`; .join('');
}).join(''); }
};
} }
export default Translation; export default Translation;

View File

@ -16,8 +16,8 @@
import utils from '../../utils/utils'; import utils from '../../utils/utils';
import NumberFormatter from './NumberFormatter'; import NumberFormatter from './NumberFormatter';
import { Locale, Locales } from '../../types/types'; import { Locale, Locales } from '../../types/types';
import {I18nCache} from "../../types/interfaces"; import { I18nCache } from '../../types/interfaces';
import {createIntlCache} from "../../../index"; import creatI18nCache from '../cache/cache';
/** /**
* *
@ -34,7 +34,7 @@ class PluralFormatter {
this.locales = locales; this.locales = locales;
this.value = value; this.value = value;
this.message = message; this.message = message;
this.cache = cache ?? createIntlCache(); this.cache = cache ?? creatI18nCache();
} }
// 将 message中的“#”替换为指定数字value并返回新的字符串或者字符串数组 // 将 message中的“#”替换为指定数字value并返回新的字符串或者字符串数组