Awesome
extract-react-intl-messages
This package will generate json or yaml files from a glob. It will generate one file per locale, with the ids of each message defined by the defineMessages
function of react-intl. The value of each of these keys will be an empty string, except for your defaultLocale
which will be populated with the defaultMessage
.
Dependencies
Babel
- 0.x works with Babel 6
Install
$ npm install --save-dev extract-react-intl-messages
Usage
app/components/App/messages.js
import { defineMessages, useIntl } from 'react-intl'
export default defineMessages({
hello: {
id: 'a.hello',
defaultMessage: 'hello'
},
world: {
id: 'a.world',
defaultMessage: 'world'
}
})
export const SubmitButton = () => {
const intl = useIntl()
const label = intl.formatMessage({
id: 'a.submit',
defaultMessage: 'Submit Button'
})
return <button aria-label={label}>{label}</button>
}
Run Script
$ extract-messages -l=en,ja -o app/translations -d en --flat false './app/**/!(*.test).js'
Output
app/translations/en.json
{
"a": {
"hello": "hello",
"world": "world",
"submit": "Submit Button"
}
}
app/translations/ja.json
{
"a": {
"hello": "",
"world": "",
"submit": ""
}
}
Recommend
Use with babel-plugin-react-intl-auto: i18n for the component age. Auto management react-intl ID.
CLI
$ extract-messages --help
Extract react-intl messages
Usage
$ extract-react-intl-messages <input>
$ extract-messages <input>
Options
-o, --output Output directory [require: true]
-l, --locales locales [require: true]
-f, --format json | yaml [default: json]
-d, --defaultLocale default locale
--overwriteDefault default: false
--flat json [default: true] | yaml [default: false]
Example
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
$ extract-messages -l=ja,en -o i18n 'src/**/*.js'
$ extract-messages -l=ja,en -o app/translations -f yaml 'app/**/messages.js'
create-react-app user
create .babelrc
like this.
{
"presets": ["react-app"]
}
Run with NODE_ENV=development
.
$ NODE_ENV=development extract-messages ...
TypeScript
babel required.
npm install --save-dev @babel/core @babel/preset-typescript @babel/preset-react
babel.config.js
module.exports = function (api) {
api.cache(true)
return {
presets: ['@babel/preset-react', '@babel/preset-typescript']
}
}
API
extractReactIntlMessages(locales, input, buildDir, [options])
locales
Type: Array<string>
Example: ['en', 'ja']
input
Type: Array<string>
Target files. glob.
buildDir
Type: string
Export directory.
options
defaultLocale
Type: string
<br>
Default: en
format
Type: json
| yaml
<br>
Default: json
Set extension to output.
overwriteDefault
Type: boolean
<br>
Default: true
If overwriteDefault is false
, it will not overwrite messages in the default locale.
flat
Type: boolean
<br>
Default: true
If format is yaml
, set to false
.
Be careful if false
.
See this issue.
babel-plugin-react-intl's Options
See https://github.com/formatjs/formatjs/tree/master/packages/babel-plugin-react-intl#options
Contributors
Thanks goes to these wonderful people (emoji key):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> <!-- prettier-ignore-start --> <!-- markdownlint-disable --> <table> <tr> <td align="center"><a href="http://akameco.github.io"><img src="https://avatars2.githubusercontent.com/u/4002137?v=4" width="100px;" alt=""/><br /><sub><b>akameco</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=akameco" title="Code">๐ป</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=akameco" title="Tests">โ ๏ธ</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=akameco" title="Documentation">๐</a> <a href="#infra-akameco" title="Infrastructure (Hosting, Build-Tools, etc)">๐</a></td> <td align="center"><a href="http://hoantran.info"><img src="https://avatars3.githubusercontent.com/u/13161875?v=4" width="100px;" alt=""/><br /><sub><b>Hoan Tran</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=hoantran-it" title="Code">๐ป</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=hoantran-it" title="Tests">โ ๏ธ</a></td> <td align="center"><a href="https://github.com/giantpinkwalrus"><img src="https://avatars1.githubusercontent.com/u/3383240?v=4" width="100px;" alt=""/><br /><sub><b>giantpinkwalrus</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=giantpinkwalrus" title="Code">๐ป</a></td> <td align="center"><a href="https://github.com/enrique-ramirez"><img src="https://avatars3.githubusercontent.com/u/1190640?v=4" width="100px;" alt=""/><br /><sub><b>enrique-ramirez</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=enrique-ramirez" title="Documentation">๐</a></td> <td align="center"><a href="http://stefan-gojan.de"><img src="https://avatars2.githubusercontent.com/u/163128?v=4" width="100px;" alt=""/><br /><sub><b>Stefan Gojan</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/issues?q=author%3Ahoschi" title="Bug reports">๐</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=hoschi" title="Code">๐ป</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=hoschi" title="Tests">โ ๏ธ</a></td> <td align="center"><a href="https://lithe.net"><img src="https://avatars1.githubusercontent.com/u/857744?v=4" width="100px;" alt=""/><br /><sub><b>Solomon English</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=solomon23" title="Code">๐ป</a></td> <td align="center"><a href="https://github.com/Filson14"><img src="https://avatars1.githubusercontent.com/u/4540538?v=4" width="100px;" alt=""/><br /><sub><b>Filip "Filson" Pasternak</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=Filson14" title="Code">๐ป</a></td> </tr> <tr> <td align="center"><a href="http://about.me/nodaguti"><img src="https://avatars0.githubusercontent.com/u/27622?v=4" width="100px;" alt=""/><br /><sub><b>nodaguti</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=nodaguti" title="Code">๐ป</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=nodaguti" title="Tests">โ ๏ธ</a></td> <td align="center"><a href="https://github.com/fix-fix"><img src="https://avatars1.githubusercontent.com/u/11943024?v=4" width="100px;" alt=""/><br /><sub><b>fix-fix</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=fix-fix" title="Code">๐ป</a></td> <td align="center"><a href="http://bradbarrow.com"><img src="https://avatars3.githubusercontent.com/u/1264276?v=4" width="100px;" alt=""/><br /><sub><b>bradbarrow</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/issues?q=author%3Abradbarrow" title="Bug reports">๐</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=bradbarrow" title="Code">๐ป</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=bradbarrow" title="Tests">โ ๏ธ</a></td> <td align="center"><a href="http://ddem.us/"><img src="https://avatars1.githubusercontent.com/u/290457?v=4" width="100px;" alt=""/><br /><sub><b>Gregor MacLennan</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=gmaclennan" title="Code">๐ป</a></td> <td align="center"><a href="https://github.com/zarv1k"><img src="https://avatars1.githubusercontent.com/u/6296643?v=4" width="100px;" alt=""/><br /><sub><b>Dmitry Zarva</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=zarv1k" title="Code">๐ป</a></td> <td align="center"><a href="https://github.com/panpanc"><img src="https://avatars2.githubusercontent.com/u/29132669?v=4" width="100px;" alt=""/><br /><sub><b>Michael Pan</b></sub></a><br /><a href="#example-panpanc" title="Examples">๐ก</a></td> <td align="center"><a href="https://github.com/testower"><img src="https://avatars2.githubusercontent.com/u/231492?v=4" width="100px;" alt=""/><br /><sub><b>Tom Erik Stรธwer</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=testower" title="Code">๐ป</a></td> </tr> <tr> <td align="center"><a href="https://nextbook.io"><img src="https://avatars0.githubusercontent.com/u/20876627?v=4" width="100px;" alt=""/><br /><sub><b>Bart Lens</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=lensbart" title="Code">๐ป</a></td> <td align="center"><a href="https://github.com/revskill10"><img src="https://avatars3.githubusercontent.com/u/1390196?v=4" width="100px;" alt=""/><br /><sub><b>Truong Hoang Dung</b></sub></a><br /><a href="#example-revskill10" title="Examples">๐ก</a></td> </tr> </table> <!-- markdownlint-enable --> <!-- prettier-ignore-end --> <!-- ALL-CONTRIBUTORS-LIST:END -->This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT ยฉ akameco