React-intl 在反应之外定义消息

React-intl define messages outside of react

我有 utils.js 个文件。

export function categoryIdToCategoryName(categoryId) {
let name;
switch (categoryId) {
    case constants.RISK_CATEGORY_LOW:
        name = 'low';
        break;
    case constants.RISK_CATEGORY_MEDIUM:
        name = 'medium';
        break;
    case constants.RISK_CATEGORY_HIGH:
        name = 'high';
        break;
    case constants.RISK_CATEGORY_CRITICAL:
        name = 'critical';
        break;
    default:
        console.warn('see: /utils/risk.js', 'categoryIdToCategoryName:', categoryId);
        name = 'unknown';
   }
    return name;
}

我想使用 https://github.com/yahoo/react-intl 翻译这些文本 - [低、中、高、关键]。所以我定义了 messages

const translations = defineMessages({
riskLow: {
    id: 'utils.risk.low',
    defaultMessage: 'low',
},
riskMedium: {
    id: 'utils.risk.medium',
    defaultMessage: 'medium',
},
riskHigh: {
    id: 'utils.risk.high',
    defaultMessage: 'high',
},
riskCritical: {
    id: 'utils.risk.critical',
    defaultMessage: 'critical',
}
});

现在最后一步是什么?

如何将消息传递回函数?应该有 formatMessage 功能,但它只在反应上下文中。

您可以直接使用 yahoo intl 库来完成此任务:https://github.com/yahoo/intl-messageformat

否则,您可以将 formatMessage 函数从您的 React 组件传递给它调用的函数。

我想在你的情况下你想访问 intl 对象,但这个文件不是反应组件,对吧? 如果是这种情况,您必须在该文件中创建一个提供程序,类似于您可能已经在 index.js 文件中拥有的内容。

在您的 utils.js 文件中添加:

import { IntlProvider, addLocaleData } from 'react-intl';
import localeDataEN from 'react-intl/locale-data/en';
import { translations } from '../point-to-your-translation-file';

addLocaleData(localeDataEN);
const locale = 'en'
const messages = //read it from your translated json file
const intlProvider = new IntlProvider({ locale, messages });
const { intl } = intlProvider.getChildContext(); // this is how you get access to the formatMessage function to use i18n for your messages

function categoryIdToCategoryName(categoryId) {
let name;
switch (categoryId) {
    case constants.RISK_CATEGORY_LOW:
        name = intl.formatMessage(formMessages.riskLow);
        break;
    case constants.RISK_CATEGORY_MEDIUM:
        name = intl.formatMessage(formMessages.riskMedium);
        break;
    case constants.RISK_CATEGORY_HIGH:
        name = intl.formatMessage(formMessages.riskHigh);
        break;
    case constants.RISK_CATEGORY_CRITICAL:
        name = intl.formatMessage(formMessages.riskCritical);
        break;
    default:
        console.warn('see: /utils/risk.js', 'categoryIdToCategoryName:', categoryId);
        name = 'unknown';
   }
    return name;
}

您也可以查看这个答案:

现在可以使用 CreateIntl API(版本 4.6.4 React-intl)

这是适合我的代码片段:

import { createIntl, createIntlCache } from 'react-intl';
import English from '../../translations/en-US.json'; //your messages translated with id

const cache = createIntlCache();
const intl = createIntl({ locale: 'en-US', messages: English, }, cache);//locale and message can come from Redux or regular import
const number = intl.formatNumber(2000); //just use imperative way like you would with useIntl() hook or InjectIntl HOC