Git Product home page Git Product logo

node-gettext-json's Introduction

node-gettext-json

node-gettext-json is a Node.JS module to use .json files.

Fork of andris9 node-gettext. But only supports json to be able to use it with React Native.

Features

  • Load json files
  • Supports contexts and plurals

Installation

npm install node-gettext-json

Usage

Create a new Gettext object

var Gettext = require("node-gettext-json");

var gt = new Gettext();

Add a language

addTextdomain(domain, file)

Language data needs to be in the Buffer format - it can be either contents of a json file.

addTextdomain(domain[, fileContents])

Load from a json file

var gt = new Gettext();
var jsonFile = JSON.parse(fs.readFileSync(__dirname + '/fixtures/latin13.json'));

gt.addTextdomain('et-EE', jsonFile);

Plural rules are automatically detected from the language code

gt.addTextdomain("et");
gt.setTranslation("et", false, "hello!", "tere!");

Check or change default language

textdomain(domain)

gt.textdomain("et");

The function also returns the current texdomain value

var curlang = gt.textdomain();

Translation methods

Load a string from default language file

gettext(msgid)

var greeting = gt.gettext("Hello!");

Load a string from a specific language file

dgettext(domain, msgid)

var greeting = gt.dgettext("et", "Hello!");

Load a plural string from default language file

ngettext(msgid, msgid_plural, count)

gt.ngettext("%d Comment", "%d Comments", 10);

Load a plural string from a specific language file

dngettext(domain, msgid, msgid_plural, count)

gt.dngettext("et", "%d Comment", "%d Comments", 10)

Load a string of a specific context

pgettext(msgctxt, msgid)

gt.pgettext("menu items", "File");

Load a string of a specific context from specific language file

dpgettext(domain, msgctxt, msgid)

gt.dpgettext("et", "menu items", "File");

Load a plural string of a specific context

npgettext(msgctxt, msgid, msgid_plural, count)

gt.npgettext("menu items", "%d Recent File", "%d Recent Files", 3);

Load a plural string of a specific context from specific language file

dnpgettext(domain, msgctxt, msgid, msgid_plural, count)

gt.dnpgettext("et", "menu items", "%d Recent File", "%d Recent Files", 3);

Get comments for a translation (if loaded from PO)

getComment(domain, msgctxt, msgid)

gt.getComment("et", "menu items", "%d Recent File");

Returns an object in the form of {translator: "", extracted: "", reference: "", flag: "", previous: ""}

Advanced handling

If you need the translation object for a domain, for example et_EE, you can access it from gt.domains.et_EE.

License

MIT

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.