简体   繁体   English

在项目上运行 formatjs extract 时没有返回消息。 关于为什么的任何想法?

[英]No messages returned when running formatjs extract on project. Any ideas of why?

So, I'm implementing a project in Electron and wanted to implement internationalization into it.因此,我正在 Electron 中实施一个项目,并希望在其中实施国际化。 I have a simple component that uses react-intl 's FormattedMessage and a messages.js file with a description of its message.我有一个简单的组件,它使用react-intl的 FormattedMessage 和一个带有消息描述的messages.js文件。 I tried to follow the tutorial in the docs for extracting internationalization messages using @formatjs/cli , however, even though it seems to run correctly the npm script seems to not return anything, like it didn't find any messages.我尝试按照文档中的教程使用@formatjs/cli提取国际化消息,但是,即使它似乎正确运行 npm 脚本似乎没有返回任何内容,就像它没有找到任何消息一样。 My configs and files are listed below:我的配置和文件如下所示:

.babelrc

{
  "presets":  ["react-app"],
  "plugins": [
    [
      "react-intl",
      {
        "idInterpolationPattern": "[sha512:contenthash:base64:6]",
        "extractFromFormatMessageCall": true,
        "ast": true
      }
    ]
  ]
}

messages.js

import { defineMessage, defineMessages } from 'react-intl';

const scope = 'src.components.Test';

export default defineMessages({
  info: {
    id: `${scope}.info`,
    defaultMessage: 'Info'
  }
});

package.json

...
"scripts": {
    "serve": "react-scripts start",
    "start": "SET DEBUG=true && electron .",
    "lint": "eslint .",
    "lint:fix": "eslint --fix .",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "messages:extract": "formatjs extract --ignore=src/translations/* --out-file=src/translations/pt.json 'src/**/messages.js'",
    "messages:compile": "formatjs compile-folder --ast  src/translations src/compiled-translations"
  },
...
"devDependencies": {
    "@babel/cli": "^7.12.8",
    "@formatjs/cli": "^2.13.15",
    "babel-plugin-react-intl": "^8.2.21",
    "babel-preset-react-app": "^10.0.0",
    "electron": "^10.1.1",
    "eslint": "^6.6.0",
    "eslint-config-airbnb": "^18.2.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "^4.2.0",
    "extract-react-intl-messages": "^4.1.1"
  }

Now, whenever I run the script for extraction ( messages:extract ) all I get is an empty json object.现在,每当我运行提取脚本( messages:extract )时,我得到的只是一个空的 json object。 Any ideas if I'm doing anything wrong?如果我做错了什么,有什么想法吗? Thanks!谢谢!

I had the same issue.我遇到过同样的问题。

Try to change 'src/**/messages.js' to \"src/**/messages.js\"尝试将'src/**/messages.js'更改为\"src/**/messages.js\"

For me, this change works.对我来说,这种改变是有效的。

I don't know if I was having the same issue, but what happened for me was that the CLI did see the messages, but did not write any JSON file.我不知道我是否有同样的问题,但发生在我身上的是 CLI 确实看到了这些消息,但没有写入任何 JSON 文件。 The only way I could get this to work is as follows (in package.json).我可以让它工作的唯一方法如下(在 package.json 中)。

package.json

...
scripts: {
  "translations:extract": "npm run translations:extract:internal -- \"src/**/*.ts*\" --out-file \"locales/en.json\" --id-interpolation-pattern '[sha512:contenthash:base64:6]'",
  "translations:extract:internal": "formatjs extract",
}
...

It feels a bit hacky, but it does the job.感觉有点hacky,但它确实完成了工作。 If you really want to get dirty, the following is also possible just using one line:如果你真的想弄脏,也可以只使用一行:

package.json

...
scripts: {
    "translations:extract": "node_modules/.bin/formatjs extract \"src/**/*.ts*\" --out-file \"locales/extracted/en.json\" --id-interpolation-pattern '[sha512:contenthash:base64:6]'"
}
...

The above needs formatjs to be installed as a project dependency以上需要将formatjs安装为项目依赖

暂无
暂无

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

相关问题 如何使用 FormatJS CLI 从多个特定文件夹中提取消息? - How do I extract messages from multiple specific folders with FormatJS CLI? 我设置了新的盖茨比项目。 它已成功安装,但是为什么在我启动开发服务器时它会失败? - I setup new Gatsby project. It got installed successfully but Why it is failing when I am starting the dev server? 为什么这会反应组件按钮onClick的任何想法都不会触发 - Any ideas why this react components buttons onClick would not be firing 寻找一个账单扫描OCR工具,我可以将其内置到一个学术项目的React Native App中。 有什么建议么? - Looking for a bill scanning OCR tool that I can build into a React Native App for an academic project. Any suggestions? 有什么想法为什么 useEffect 不能识别我的依赖关系吗? - Any ideas why useEffect isn't recognizing my dependency? 贬值警告.. 在客户端项目上运行 npm 后启动。 显示了以下错误。 在计算机关闭之前它正在工作 - Deprication Warning !! After running npm start on client side project. The following error has shown. It was working before the computer shut down 尝试编译我的反应项目时收到大量错误。 我完全不知道这意味着什么或如何开始修复它 - Recieving an influx of errors when trying to complie my react project. I quite literally have no idea what it means or how to begin to fix it 新手尝试 react.js 项目。 当我尝试导入图像时,我收到一条无法编译的错误消息 - Newbie trying a react.js project. When I try to import an image I get an error message that it failed to compile 当任何 function 正在运行时,useEffect 正在运行 - useEffect is running when any function is running 如何运行 formatjs 编译? - How to run formatjs compile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM