首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react intl在编译消息后使用defaultRichTextElements时显示警告。

react intl在编译消息后使用defaultRichTextElements时显示警告。
EN

Stack Overflow用户
提问于 2022-02-28 09:45:35
回答 1查看 423关注 0票数 0

我有一个用create-react-app创建的带有类型记录的项目。我一直在努力寻找最好的方式来组织我的信息。这是我的方法:

  • 我已经用/messages/en.json编译了消息。
代码语言:javascript
复制
{
  "a.hello": "<bold>hello</bold>",
  "a.world": "world",
  "another.another": "another <bold>message</bold>"
}
  • 消息以这种方式使用defineMessages定义。

messages.ts

代码语言:javascript
复制
import { defineMessages } from "react-intl";

export default defineMessages({
  hello: {
    id: "a.hello",
    defaultMessage: "<bold>hello</bold>",
  },
  world: {
    id: "a.world",
    defaultMessage: "world",
  },
});

index.tsx

代码语言:javascript
复制
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { IntlProvider } from "react-intl";
import enMessages from "./messages/en.json";

interface Props {
  children?: React.ReactNode;
}

const Bold = (props: Props) => {
  return (
    <div>
      <strong>{props.children}</strong>
    </div>
  );
};

ReactDOM.render(
  <IntlProvider
    locale="en"
    defaultLocale="en"
    messages={enMessages}
    defaultRichTextElements={{
      bold: (chunks) => <Bold>{chunks}</Bold>,
    }}
  >
    <App />
  </IntlProvider>,
  document.getElementById("root")
);
  • 我在FormattedMessage组件中使用消息。

App.tsx

代码语言:javascript
复制
import "./App.css";
import messages from "./messages";
import { FormattedMessage } from "react-intl";

function App() {
  return (
    <div className="App">
      <FormattedMessage {...messages.hello} />{" "}
      <FormattedMessage {...messages.world} />
    </div>
  );
}

export default App;

我是拥有这个npm脚本的提取和编译消息

代码语言:javascript
复制
"extract-compile": "formatjs extract 'src/**/*.ts*' --ignore='**/*.d.ts' --out-file temp.json --flatten --id-interpolation-pattern '[sha512:contenthash:base64:6]' && formatjs compile 'temp.json' --out-file src/messages/en.json && rm temp.json"

问题是,我仍然在控制台中收到这个警告:

代码语言:javascript
复制
message.js:50 [@formatjs/intl] "defaultRichTextElements" was specified but "message" was not pre-compiled. 
Please consider using "@formatjs/cli" to pre-compile your messages for performance.
For more details see https://formatjs.io/docs/getting-started/message-distribution

我的方法有什么问题吗?我认为警告应该消失,但我显然遗漏了一些东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-07 17:13:04

在深入了解文档之后,标记--ast (在文档示例中不存在,但在CLI中描述)似乎是关键。

代码语言:javascript
复制
"extract-compile": "formatjs extract 'src/**/*.ts*' --ignore='**/*.d.ts' --out-file temp.json --flatten --id-interpolation-pattern '[sha512:contenthash:base64:6]' && formatjs compile 'temp.json'  --ast --out-file lang/en.json && rm temp.json",
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71293085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档