Coder Social home page Coder Social logo

babel-plugin-i18n's Introduction

UNSUPPORTED

Note that I (@valscion) don't currently use this plugin, so this is unsupported. If you'd like to start using this and maintain it, feel free to open an issue and share your motivation.

babel-plugin-i18n

Babel plugin to statically compile keypath denoted translation calls to literals.

Compiles these...

const myString = __("my.translation.path");
const anEnum = __obj("example.countries");
const lyrics = __arr("song");
const veryComplex = __dump("complex");

...with this kind of translation...

{
  "my": {
    "translation": {
      "path": "Hei, maailma!"
    }
  },
  "example": {
    "countries": {
      "finland": "Suomi",
      "sweden": "Ruotsi"
    }
  },
  "song": [
    "Hey!",
    "Ho!",
    "Let's go!"
  ],
  "complex": [[["multilevel array"], 123], null]
}

...into this:

const myString = "Hei, maailma!";
const anEnum = {
  finland: "Suomi",
  sweden: "Ruotsi"
};
const lyrics = ["Hey!", "Ho!", "Let's go!"];
const complex = [[["multilevel array"], 123], null];

Installation

$ npm install babel-plugin-i18n --save-dev

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["i18n", {
    "translationLoader": "./path/to/translationLoader"
  }]
}

Via CLI

TODO: How does one actually pass options via CLI?

Via Node API

require("babel-core").transform("code", {
  plugins: ["i18n", {
    "translationLoader": "./path/to/translationLoader"
  }]
});

Translation loader example

const join = require('path').join;
const readFileSync = require('fs').readFileSync;

const TRANSLATIONS_PATH = join(__dirname, 'completeTranslations.json');

module.exports = function myTranslationLoader() {
  const translationJSON = readFileSync(TRANSLATIONS_PATH, 'utf8');
  return JSON.parse(translationJSON);
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.