All Projects → akameco → Babel Plugin React Intl Auto

akameco / Babel Plugin React Intl Auto

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

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Babel Plugin React Intl Auto

I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (+91.63%)
Mutual labels:  babel, babel-plugin, i18n
Babel Plugin I18next Extract
Babel plugin that statically extracts i18next and react-i18next translation keys.
Stars: ✭ 93 (-54.19%)
Mutual labels:  babel-plugin, i18n
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (-55.17%)
Mutual labels:  babel, babel-plugin
Babel Plugin Prismjs
A babel plugin to use PrismJS with standard bundlers.
Stars: ✭ 114 (-43.84%)
Mutual labels:  babel, babel-plugin
Generator Babel Plugin
Babel Plugin generator for Yeoman
Stars: ✭ 88 (-56.65%)
Mutual labels:  babel, babel-plugin
Idx.macro
a 'babel-macros' version of 'babel-plugin-idx'
Stars: ✭ 90 (-55.67%)
Mutual labels:  babel, babel-plugin
Js Proposal Algebraic Effects
📐Let there be algebraic effects in JS
Stars: ✭ 110 (-45.81%)
Mutual labels:  babel, babel-plugin
Babel Plugin Optimize Clsx
Babel plugin to optimize the use of clsx, classnames, and other libraries with a compatible API
Stars: ✭ 80 (-60.59%)
Mutual labels:  babel, babel-plugin
Babel Plugin Polished
Compile polished helper functions at build time
Stars: ✭ 133 (-34.48%)
Mutual labels:  babel, babel-plugin
Babel Plugin Transform Typescript Metadata
Babel plugin to emit decorator metadata like typescript compiler
Stars: ✭ 142 (-30.05%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Html Attrs
Babel plugin which transforms HTML and SVG attributes on JSX host elements into React-compatible attributes
Stars: ✭ 170 (-16.26%)
Mutual labels:  babel, babel-plugin
Modify Babel Preset
💫 Create a modified babel preset based on an an existing preset.
Stars: ✭ 85 (-58.13%)
Mutual labels:  babel, babel-plugin
Compiled
A familiar and performant compile time CSS-in-JS library for React.
Stars: ✭ 1,235 (+508.37%)
Mutual labels:  babel, babel-plugin
Jsx Control Statements
Neater If and For for React JSX
Stars: ✭ 1,305 (+542.86%)
Mutual labels:  babel, babel-plugin
Catom
A 0 runtime CSS in JS library
Stars: ✭ 84 (-58.62%)
Mutual labels:  babel, babel-plugin
Babel Plugin Jsx Adopt
Stars: ✭ 94 (-53.69%)
Mutual labels:  babel, babel-plugin
Vue Infinite Slide Bar
∞ Infinite slide bar component (no dependency and light weight 1.48 KB)
Stars: ✭ 190 (-6.4%)
Mutual labels:  auto, component
Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (-67.98%)
Mutual labels:  babel, babel-plugin
Babel Plugin Captains Log
Babel plugin that injects helpful details into console statements
Stars: ✭ 80 (-60.59%)
Mutual labels:  babel, babel-plugin
Babel Plugin Runtyper
⚡️ Runtime type-checker for JavaScript
Stars: ✭ 117 (-42.36%)
Mutual labels:  babel, babel-plugin

babel-plugin-react-intl-auto

test 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 | boolean | regexp
Default: ''

if removePrefix is true, no file path prefix is included in the id.

Example (src/components/App/messages.js)

when removePrefix is "src"

import { defineMessages } from 'react-intl';

export default defineMessages({
  hello: 'hello world'
});

           

import { defineMessages } from 'react-intl';

export default defineMessages({
  hello: {
    id: 'components.App.hello',
    defaultMessage: 'hello world'
  }
});

when removePrefix is "src.components"

import { defineMessages } from 'react-intl';

export default defineMessages({
  hello: 'hello world'
});

           

import { defineMessages } from 'react-intl';

export default defineMessages({
  hello: {
    id: 'App.hello',
    defaultMessage: 'hello world'
  }
});

when removePrefix is true

import { defineMessages } from 'react-intl';

export default defineMessages({
  hello: 'hello world'
});

           

import { defineMessages } from 'react-intl';

export default defineMessages({
  hello: {
    id: 'hello',
    defaultMessage: 'hello world'
  }
});

filebase

Type: boolean
Default: false

if filebase is true, generate id with filename.

moduleSourceName

Type: string
Default: react-intl

if set, enables to use custom module as a source for defineMessages etc.

https://github.com/akameco/babel-plugin-react-intl-auto/issues/74#issuecomment-528562743

includeExportName

Type: boolean | 'all'
Default: false

if includeExportName is true, adds named exports as part of the id.

Only works with defineMessages.

Example
export const test = defineMessages({
  hello: 'hello {name}',
})

           

export const test = defineMessages({
  hello: {
    id: 'path.to.file.test.hello',
    defaultMessage: 'hello {name}',
  },
})

If includeExportName is 'all', it will also add default to the id on default exports.

extractComments

Use leading comments as the message description.

Only works with defineMessages

Type: boolean
Default: true

Example
export const test = defineMessages({
  // Message used to greet the user
  hello: 'hello {name}',
})

           

export const test = defineMessages({
  hello: {
    id: 'path.to.file.test.hello',
    defaultMessage: 'hello {name}',
    description: 'Message used to greet the user',
  },
})

useKey

Only works with intl.formatMessage, FormattedMessage and FormattedHTMLMessage. Instead of generating an ID by hashing defaultMessage, it will use the key property if it exists.

Type: boolean
Default: false

Example
intl.formatMessage({
  key: 'foobar',
  defaultMessage: 'hello'
});

           

intl.formatMessage({
  key: 'foobar',
  defaultMessage: 'hello',
  "id": "path.to.file.foobar"
});
<FormattedMessage key="foobar" defaultMessage="hello" />

           

<FormattedMessage id="path.to.file.foobar" key="foobar" defaultMessage="hello" />

separator

Allows you to specify a custom separator

Type: string
Default: .

Example

when separator is "_"

export const test = defineMessages({
  hello: 'hello {name}',
})

           

export const test = defineMessages({
  hello: {
    id: 'path_to_file_test_hello',
    defaultMessage: 'hello {name}',
  },
})

relativeTo

Allows you to specify the directory that is used when determining a file's prefix.

This option is useful for monorepo setups.

Type: string
Default: process.cwd()

Example

Folder structure with two sibling packages. packageB contains babel config and depends on packageA.

|- packageA
| |
|  -- componentA
|
|- packageB
| |
|  -- componentB
| |
|  -- .babelrc

Set relativeTo to parent directory in packageB babel config

{
  "plugins": [
    [
      "react-intl-auto",
      {
        "relativeTo": "..",
        // ...
      },
    ],
  ]
}

Run babel in packageB

cd packageB && babel

Messages in componentA are prefixed relative to the project root

export const test = defineMessages({
  hello: 'hello {name}',
})

           

export const test = defineMessages({
  hello: {
    id: 'packageA.componentA.hello',
    defaultMessage: 'hello {name}',
  },
})

Support variable

Example
const messages = { hello: 'hello world' }

export default defineMessages(messages)

           

const messages = {
  hello: {
    id: 'path.to.file.hello',
    defaultMessage: 'hello wolrd'
  }
};

export default defineMessages(messages);

TypeScript

TypeScript support is bundled with this package. Be sure to include our type definition and run @babel/plugin-transform-typescript beforehand. This way, you can also be empowered by extract-react-intl-messages.

tsconfig.json

{
  "compilerOptions": {
    // ...
    "jsx": "preserve"
    // ...
  },
  "include": ["node_modules/babel-plugin-react-intl-auto/**/*.d.ts"]
}

.babelrc

{
  "plugins": [["@babel/plugin-transform-typescript"], ["react-intl-auto"]]
}

webpack.config.js

Use babel-loader along with ts-loader when using webpack as well.

module.exports = {
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: [/node_modules/],
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'ts-loader',
          },
        ],
      },
    ],
  },
}

Related

babel-plugin-react-intl-id-hash

If you want short consistent hash values for the ID, you can use react-intl-id-hash in addition to this plugin to help reduce your applications bundle size.

extract-react-intl-messages

Extract react-intl messages.

Contributors

Thanks goes to these wonderful people (emoji key):


akameco

💻 ⚠️ 👀 📖

Aleksander Heintz

💻 📖

Ryan Leckey

💻

Adam

💻 📖

Guylian Cox

💻 📖 ⚠️

Carl Grundberg

💡 📖

bradbarrow

💻 📖 ⚠️

Mauro Gabriel Titimoli

💻 ⚠️

Stanislav Ermakov

💻

Chitoku

💻

Kouta Kumagai

📖 💻 ⚠️

Shahyar G

💻

Remco Haszing

💻

jmarceli

💻 ⚠️
Dominik Żegleń
Dominik Żegleń

💻 ⚠️
Filip
Filip "Filson" Pasternak

💻
Eric Masiello
Eric Masiello

💻 ⚠️
Josh Poole
Josh Poole

💻 ⚠️

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

License

MIT © akameco

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].