Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

extract react intl messages

License

NotificationsYou must be signed in to change notification settings

akameco/extract-react-intl-messages

Repository files navigation

testtested with jeststyled with prettierMIT LicenseAll Contributors

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 thedefineMessages function ofreact-intl. The value of each of these keys will be an empty string, except for yourdefaultLocale which will be populated with thedefaultMessage.

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'exportdefaultdefineMessages({hello:{id:'a.hello',defaultMessage:'hello'},world:{id:'a.world',defaultMessage:'world'}})exportconstSubmitButton=()=>{constintl=useIntl()constlabel=intl.formatMessage({id:'a.submit',defaultMessage:'Submit Button'})return<buttonaria-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 withbabel-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: true  --flat                  json [default: true] | yaml [default: false]  --indent                default: 2  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 withNODE_ENV=development.

$ NODE_ENV=development extract-messages ...

TypeScript

babel required.

Seeexample/with-typescript

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
Default:en

format

Type:json |yaml
Default:json

Set extension to output.

overwriteDefault

Type:boolean
Default: true

If overwriteDefault isfalse, it will not overwrite messages in the default locale.

indent

Type:number
Default:2

flat

Type:boolean
Default:true

If format isyaml, set tofalse.

Be careful iffalse.Seethis issue.

babel-plugin-react-intl's Options

Seehttps://github.com/formatjs/formatjs/tree/master/packages/babel-plugin-react-intl#options

Contributors

Thanks goes to these wonderful people (emoji key):

akameco
akameco

💻⚠️📖🚇
Hoan Tran
Hoan Tran

💻⚠️
giantpinkwalrus
giantpinkwalrus

💻
enrique-ramirez
enrique-ramirez

📖
Stefan Gojan
Stefan Gojan

🐛💻⚠️
Solomon English
Solomon English

💻
Filip "Filson" Pasternak
Filip "Filson" Pasternak

💻
nodaguti
nodaguti

💻⚠️
fix-fix
fix-fix

💻
bradbarrow
bradbarrow

🐛💻⚠️
Gregor MacLennan
Gregor MacLennan

💻
Dmitry Zarva
Dmitry Zarva

💻
Michael Pan
Michael Pan

💡
Tom Erik Støwer
Tom Erik Støwer

💻
Bart Lens
Bart Lens

💻
Truong Hoang Dung
Truong Hoang Dung

💡
Nestoro
Nestoro

💻
Yutaro Kido
Yutaro Kido

💻

This project follows theall-contributors specification. Contributions of any kind welcome!

License

MIT ©akameco


[8]ページ先頭

©2009-2025 Movatter.jp