Umi: [Bug] 在一个ts文件中的独立函数中如何使用umi的useIntl来实现国际化?

Created on 17 Oct 2020  ·  2Comments  ·  Source: umijs/umi

What happens?

在一个ts文件中的独立函数中如何使用umi的useIntl来实现国际化?使用 import { formatMessage } from 'umi'的话有报警信息。

Most helpful comment

import { getIntl, getLocale } from 'umi';

function test() {
  const intl = getIntl(getLocale());
  console.log(
           intl.formatMessage({
              id: 'system.hello',
              description: 'Hello',
            })
  );
}

All 2 comments

这不是bug。

这是因为 intl 依赖指定的react 上下文,否则就会无法生效或者告警。

然后说回问题,你这个独立的函数文件,将来不是要用在 react 组件里么?具体做什么用的

import { getIntl, getLocale } from 'umi';

function test() {
  const intl = getIntl(getLocale());
  console.log(
           intl.formatMessage({
              id: 'system.hello',
              description: 'Hello',
            })
  );
}
Was this page helpful?
0 / 5 - 0 ratings