Code Monkey home page Code Monkey logo

rails-translations-webpack-plugin's Introduction

rails-translations-webpack-plugin

Shares Rails i18n translations with your javascripts without duplicating contents

Usage

  • Install it with npm install rails-translations-webpack-plugin --save-dev

  • To access the generated translations remember to install also json-loader with npm install json-loader --save-dev

  • Require it in your webpack.config.js:

    const RailsTranslationsPlugin = require("rails-translations-webpack-plugin");
  • Add it to your plugins block. See below for options documentation:

    plugins: [
      new RailsTranslationsPlugin({
        localesPath: path.resolve(__dirname, "../rails-app/config/locales"),
        root       : "src"
      })
    ]
  • Add JSON loader in your loaders block:

    module: {
      loaders: [
        {
          include : "src",
          test    : /\.json$/,
          loader  : 'json-loader'
        }
      ]
    }
  • Now you can require the translations from your code. E.g.

    // somewhere inside your javascripts
    const translations = require("translations.json");
    console.log(`English translation for hello.world key: ${translations["en"]["hello.world"]}`)

Options

The followings are the available options you can set for RailsTranslationsPlugin:

  • localesPath: Path where to look for yml files. Default: current dir
  • pattern: Pattern used to find yml files. Default: **/*.yml
  • name: Name of the generated json file. Default: translations
  • root: Root path of your javascripts. Default: current dir

So let's say you have the following file structure:

  • config/
    • locales/
      • en.yml
      • it.yml
    • client/
      • app/
        • index.js
      • webpack.config.js

Your webpack entry point is probably app/index.js. This can be the configuration for RailsTranslationsPlugin:

{
  root: path.resolve(__dirname, "app"),
  localesPath: path.resolve(__dirname, "../config/locales"),
  name: "my_translations"
  pattern: "*.yml"
}

Your client/app/index.js will then be able to require the translations with:

const translations = require("my_translations.json")

rails-translations-webpack-plugin's People

Contributors

nicolaracco avatar jbielick avatar

Watchers

James Cloos avatar Takuya Uehara avatar

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.