@joaofranciscosantos/babel-plugin-i18n

1.1.12 • Public • Published

Install Size Npm Version License Dependencies Status Known Vulnerabilities

babel-plugin-i18n

Multi language builds with Babel. Setup a different build for each language.

Provide translations during compile time.

No need to include MB translations files in your bundle.

End of repetitive http request with translations.

API

i18n(text: string, language?: string) : string
  • text original text.
  • language (optional) overrides the language set by the plugin.

Setup

npm install --save-dev @joaofranciscosantos/babel-plugin-i18n

and add to .babelrc.js:

module.exports = api => ({
  plugins: [["@joaofranciscosantos/babel-plugin-i18n"]]
});

to override options:

module.exports = api => ({
  plugins: [["@joaofranciscosantos/babel-plugin-i18n", {
    source: ["translations.json", "translations/canada.json", "yo.json"],
    target: "i18n",
    language: "pt"
  }]]
});
  • source (array) the path with translations files. They will be all merged during resolution. Defaults to .dictionary.json.
  • target (string) function that will apply the translation. Defaults to i18n.
  • language (string) set the translation language. Defaults to en.

Examples

  • i18n("dog", "es") transpiles to "perro"
  • i18n("dog") transpiles to "doggy"
  • i18n("none", "?") transpiles to "none" (returns the original text because no translation was provided)

and translations files must have a similar structure:

{
  "keyword": {
    "language": "translation"
  },
  "dog": {
    "en": "doggy",
    "pt": "cão",
    "es": "perro",
    "it": "cane"
  }
}

FAQ

ReferenceError: i18n is not defined

Check your build folder. You should not see the 'i18n' function. Review your babel configuration.

TS2304: Cannot find name 'i18n'

You should declare var i18n: any;

Readme

Keywords

Package Sidebar

Install

npm i @joaofranciscosantos/babel-plugin-i18n

Weekly Downloads

15

Version

1.1.12

License

MIT

Unpacked Size

7.92 kB

Total Files

10

Last publish

Collaborators

  • joaofranciscosantos