Match-id-663822d634db9cf81c3a4a8b8a8126475b648d8e

This commit is contained in:
* 2023-09-25 16:59:17 +08:00
parent bbea645b73
commit a77442bd20
3 changed files with 29 additions and 34 deletions

View File

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

View File

@ -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;

View File

@ -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并返回新的字符串或者字符串数组