简体   繁体   English

FormatJS CLI 提取

[英]FormatJS CLI Extract

I'm trying to get formatjs setup for use with my app which uses react-intl, and i'm running into an issue when doing an extract using their CLI.我正在尝试设置 formatjs 以与使用 react-intl 的应用程序一起使用,并且在使用他们的 CLI 进行提取时遇到了问题。 I currently have custom components that embed the FormattedMessage component inside of them, example:我目前有在其中嵌入FormattedMessage组件的自定义组件,例如:

<AppLabel title="section_title_name"/>

Inside of <AppLabel/> it has <FormattedMessage id={this.props.title}.../> which would provide the translation for the label.<AppLabel/>内部,它有<FormattedMessage id={this.props.title}.../>它将为 label 提供翻译。

Is it still possible for me to setup extractions for these different component names, if they're not using the id attribute to hold the string to extract for translation?如果它们不使用id属性来保存要提取的字符串以进行翻译,我是否仍然可以为这些不同的组件名称设置提取?

Since manual IDs are discouraged, we've provided a babel plugin and a TypeScript AST transformer that will automatically insert message IDs in your transpiled code.由于不鼓励使用手动 ID,我们提供了一个 babel 插件和一个 TypeScript AST 转换器,它们会自动在您的转译代码中插入消息 ID。 For more details please visit Bundling with formatjs.有关更多详细信息,请访问使用 formatjs 捆绑。 Automatic ID Generation 自动生成 ID

From Message Extraction guide, we explicitly recommend against explicit ID due to potential collision in large application.在消息提取指南中,由于大型应用程序中的潜在冲突,我们明确建议不要使用显式 ID。 While our extractor can insert IDs in the extracted JSON file, you'd need to also insert those IDs into the compiled JS output.虽然我们的提取器可以在提取的 JSON 文件中插入 ID,但您还需要将这些 ID 插入到已编译的 JS output 中。 Bundling with formatjs与 formatjs 捆绑

You can do this.你可以这样做。

yarn add -D babel-plugin-formatjs

In .babelrc.babelrc

  "plugins": [
    [
      "formatjs",
      {
        "idInterpolationPattern": "[sha512:contenthash:base64:6]",
        "ast": true
      }
    ]
  ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM