[英]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.