extract-react-intl-messages

extract react intl messages

Github星跟蹤圖

extract-react-intl-messages

Build Status
tested with jest
styled with prettier
MIT License
All 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 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]
  --flat             json [default: true], yaml [default: false]
  --default-locale   default locale [default: en]
  --moduleSourceName module source name from where components are imported [default: react-intl]

  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'
  $ extract-messages -l=ja,en -o i18n 'src/**/*.js' --extractFromFormatMessageCall

create-react-app user

create .babelrc like this.

{
  "presets": ["react-app"]
}

Run with NODE_ENV=development.

$ NODE_ENV=development extract-messages ...

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.

flat

Type: boolean
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):

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

License

MIT © akameco

主要指標

概覽
名稱與所有者akameco/extract-react-intl-messages
主編程語言TypeScript
編程語言JavaScript (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2017-04-23 07:03:41
推送於2023-01-06 02:47:20
最后一次提交2020-05-09 23:44:08
發布數43
最新版本名稱v4.1.1 (發布於 2020-03-24 17:05:14)
第一版名稱v0.1.1 (發布於 2017-04-22 23:08:43)
用户参与
星數182
關注者數4
派生數42
提交數308
已啟用問題?
問題數48
打開的問題數8
拉請求數32
打開的拉請求數36
關閉的拉請求數9
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?