Home

Awesome

tagged-translations

Build Status npm Coverage Status Babel Macro

A dead simple babel-plugin for translating texts in React applications.

Input

<Header>
  {t`Hello ${name}!`}
</Header>

Output

<Header>
  {`Xin chào ${name} 🤣!`}
</Header>

Features

Usage

Install yarn add --dev tagged-translations

then configure

Via .babelrc

{
  "plugins": [
    ["tagged-translations", {
      "translationFile": "./translation.json",
      "tagName": "t"
    }]
  ]
}

Via babel-plugin-macros

{
  "plugins": ["macros"]
}

then import the macro and use it.

import t from 'tagged-translations/macro';

const name = 'Vinh Le';

t`Hello ${name}`;

Multiple translations

In order to support multiple translations, we could specify the translation file based on an environment variable in the build time.

// babel-plugin-macros.config.js
const { REACT_APP_LANGUAGE: language } = process.env

module.exports = {
  taggedTranslations: {
    tagName: 't',
    translationFile: `./src/translations/buildTime/${language}.json`
  }
}

Contributing

Notes