Code Monkey home page Code Monkey logo

translations's Introduction

translations version build

translations api for node.js, browsers and the command line.

Installation

npm install translations

Usage

var locale = require('locales/de-DE.json'); // node.js;
var locale = window.locale; // browser example

var t = translations(locale); // production mode: ignores most errors
var t = translations(locale, true); // development mode: throws more errors

t('How are you?');
// "Wie geht es dir?"

t('Hello {name}, how are you?', { name: 'Max' });
// "Hallo Max, wie geht es dir?"

t('Hello {name}, how is {thing}?', { name: 'Max', thing: t('the weather') });
// "Hallo Max, wie ist das Wetter?"

t('undefined key in locale');
// throws error in development mode for undefined keys
// returns empty string in production mode

t('Hello {name}, how is {thing}?', { name: 'Max' });
// throws error in development mode for undefined variables
// prints placeholders in production mode

de-DE.json

{
  "How are you?": "Wie geht es dir?",
  "Hello {name}, how are you?": "Hallo {name}, wie geht es dir?",
  "Hello {name}, how is {thing}?": "Hallo {name}, wie ist {thing}?",
  "the weather": "das Wetter"
}

Command Line

Installation

npm install translations -g # if you want to use the cli globally

sync

# sync locales
translations sync <master> <target>

# example
translations sync locales/en.json locales/de-DE.json

translations sync syncs the master translations file with the target one:

If target has values that are not strings, it throws an error.

If target does not exist yet, a copy of master will be created with the target’s name (de-DE.json).

If target does already exist, the command does the following:

  • every key not found in master is removed
  • every key not found in target is added

export

# export locales for browser usage
translations export <inputDirectory> <outputDirectory> --assign <variable>

translations export will create or overwrite the given output directory with js files for valid locales in the input directory. Each js file assigns the locale’s translations to the given variable (or window.locale by default).

LICENSE

MIT (2015) Maximilian Hoffmann

translations's People

Contributors

kittygiraudel avatar

Stargazers

Tom Meiselbach avatar Lucas avatar

Watchers

Richard Bausek avatar James Cloos avatar  avatar

Forkers

exa-online

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.