babel-plugin-react-intl-auto

组件 i18n 新时代。 自动管理 react-intl ID。「i18n for the component age. Auto management react-intl ID.」

Github stars Tracking Chart

babel-plugin-react-intl-auto

Build Status
Build status
Coverage Status
styled with prettier
tested with jest
All Contributors
babel-plugin-react-intl-auto Dev Token

i18n for the component age. Auto management react-intl ID.

React Intl is awesome. But, Global ID management is difficult and confusing.

Many projects, like react-boilerplate, give the ID to the name of the component as a prefix.
But it is redundant and troublesome.

This babel-plugin releases you from cumbersome ID management.
Based on the file path, this automatically generates a prefixed id.

Also, we strongly encourage you to use extract-react-intl-messages.
You can generate json automatically.

Goodbye, global ID!!

Before

import { defineMessages, FormattedMessage } from 'react-intl'

export default defineMessages({
  hello: {
    id: 'App.Components.Greeting.hello',
    defaultMessage: 'hello {name}',
  },
  welcome: {
    id: 'App.Components.Greeting.welcome',
    defaultMessage: 'Welcome!',
  },
})

const MyComponent = () => (
  <FormattedMessage
    id="App.Components.Greeting.goodbye"
    defaultMessage="goodbye {name}"
  />
)

After

With babel-plugin-react-intl-auto.

import { defineMessages, FormattedMessage } from 'react-intl'

export default defineMessages({
  hello: 'hello {name}',
  welcome: 'Welcome!',
})

const MyComponent = () => <FormattedMessage defaultMessage="goodbye {name}" />

See examples.

With extract-react-intl-messages

Example usage with extract-react-intl-messages.

$ extract-messages -l=en -o translations 'src/**/*.js'

en.json

{
  "components.App.hello": "hello {name}",
  "components.App.welcome": "Welcome",
  "components.App.189751785": "goodbye {name}" // unique hash of defaultMessage
}

Install

npm

$ npm install --save-dev babel-plugin-react-intl-auto

# Optional: TypeScript support
$ npm install --save-dev @babel/plugin-transform-typescript

yarn

$ yarn add --dev babel-plugin-react-intl-auto

# Optional: TypeScript support
$ yarn add --dev @babel/plugin-transform-typescript

Usage

.babelrc

{
  "plugins": [
    [
      "react-intl-auto",
      {
        "removePrefix": "app/",
        "filebase": false
      }
    ]
  ]
}

with injectIntl

Input:

import { injectIntl } from 'react-intl'

const MyComponent = ({ intl }) => {
  const label = intl.formatMessage({ defaultMessage: 'Submit button' })
  return <button aria-label={label}>{label}</button>
}

injectIntl(MyComponent)

↓   ↓   ↓

Output:

import { injectIntl } from 'react-intl'

const MyComponent = ({ intl }) => {
  const label = intl.formatMessage({
    id: 'App.Components.Button.label',
    defaultMessage: 'Submit button',
  })
  return <button aria-label={label}>{label}</button>
}

injectIntl(MyComponent)

with useIntl

Input:

import { useIntl } from 'react-intl'

const MyComponent = () => {
  const intl = useIntl()
  const label = intl.formatMessage({ defaultMessage: 'Submit button' })
  return <button aria-label={label}>{label}</button>
}

↓   ↓   ↓

Output:

import { useIntl } from 'react-intl'

const MyComponent = () => {
  const intl = useIntl()
  const label = intl.formatMessage({
    id: 'App.Components.Button.label',
    defaultMessage: 'Submit button',
  })
  return <button aria-label={label}>{label}</button>
}

Options

removePrefix

remove prefix.

Type: `string

Main metrics

Overview
Name With Ownerakameco/babel-plugin-react-intl-auto
Primary LanguageTypeScript
Program languageJavaScript (Language Count: 2)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2017-04-22 13:56:28
Pushed At2023-01-05 16:17:20
Last Commit At2021-07-25 15:44:30
Release Count45
Last Release Namev3.3.0 (Posted on 2020-03-24 19:03:50)
First Release Namev0.1.0 (Posted on 2017-04-23 19:14:32)
用户参与
Stargazers Count210
Watchers Count3
Fork Count34
Commits Count425
Has Issues Enabled
Issues Count43
Issue Open Count5
Pull Requests Count145
Pull Requests Open Count50
Pull Requests Close Count27
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private