npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rjaros/gettext.js

v1.1.3

Published

gettext.js is a lightweight (3k minified!) yet complete and accurate GNU gettext port for node and the browser. Manage your i18n translations the right way in your javascript projects.

Downloads

5

Readme

gettext.js npm version

gettext.js is a lightweight (3k minified!) yet complete and accurate GNU gettext port for node and the browser. Manage your i18n translations the right way in your javascript projects.

Why another i18n javascript library?

gettext.js aim is to port the famous GNU gettext and ngettext functions to javascript node and browser applications. It should be standards respectful and lightweight (no dictionary loading management, no extra features).

The result is a < 200 lines javascript tiny lib yet implementing fully gettext() and ngettext() and having the lighter json translation files possible (embeding only translated forms, and not much headers).

There are plenty good i18n libraries out there, notably Jed and i18n-next, but either they are too complex and too heavy, or they do not embrace fully the gettext API and philosophy.

There is also gettext.js which is pretty good and heavily inspired this one, but not active since 2012 and without any tests.

Installation

Debian (9, stretch)

apt-get install libjs-gettext.js

Node

Install the lib with the following command: npm install gettext.js --save

Require it in your project:

var i18n = require('gettext.js')();
i18n.gettext('foo');

For TypeScript definitions, use the third-party @types/gettext.js module.

Browser

Download the latest archive or get it through bower: bower install gettext.js --save

<script src="/path/to/dist/gettext.iife.js" type="text/javascript"></script>
<script>
  var i18n = window.i18n(options);
  i18n.gettext('foo');
</script>

In addition to the IIFE version, we also provide CommonJS (Node), AMD, and ESM releases. Instead of downloading, you may use a NPM CDN online such as unpkg or jsDelivr.

Usage

Load your messages

You can load your messages these ways:

// i18n.setMessages(domain, locale, messages, plural_form);
i18n.setMessages('messages', 'fr', {
  "Welcome": "Bienvenue",
  "There is %1 apple": [
    "Il y a %1 pomme",
    "Il y a %1 pommes"
  ]
}, 'nplurals=2; plural=n>1;');
// i18n.loadJSON(jsonData /*, domain */);
var json = {
  "": {
    "language": "fr",
    "plural-forms": "nplurals=2; plural=n>1;"
  },
  "Welcome": "Bienvenue",
  "There is %1 apple": [
    "Il y a %1 pomme",
    "Il y a %1 pommes"
  ]
};
i18n.loadJSON(json, 'messages');

See Required JSON format section below for more info.

Set the locale

You could do it from your dom

<html lang="fr">

or from javascript

i18n.setLocale('fr');

Gettext functions

  • gettext(msgid): Translate a string. Shorthand is __().
  • ngettext(msgid, msgid_plural, n): Translate a pluralizable string. Shorthand is _n().
  • pgettext(msgctxt, msgid): Translate a string specified by context. Shorthand is _p().
  • dcnpgettext(domain, msgctxt, msgid, msgid_plural, n): Translate a potentially pluralizable string, potentially specified by context, and potentially of a different domain (as specified in setMessages or loadJSON). No shorthand.

Variabilized strings

All four functions above can take extra arguments for variablization.

gettext('There are %1 in the %2', 'apples', 'bowl'); -> "There are apples in the bowl

ngettext('One %2', '%1 %2', 10, 'bananas'); -> "10 bananas"

It uses the public method i18n.strfmt("string", var1, var2, ...) you could reuse elsewhere in your project.

Literal percent sign (%)

When you need to have literal percent sign followed by a number (common in Hebrew or Turkish) you can escape it using another percent sign, for example:

gettext('My credit card has an interest rate of %%%1', 20); -> "My credit card has an interest rate of %20"

or without variables

gettext('My credit card has an interest rate of %%20'); -> "My credit card has an interest rate of %20"

Required JSON format

You'll find in /bin a po2json.js converter, based on the excellent po2json project that will dump your .po files into the proper json format below:

{
    "": {
        "language": "en",
        "plural-forms": "nplurals=2; plural=(n!=1);"
    },
    "simple key": "It's tranlation",
    "another with %1 parameter": "It's %1 tranlsation",
    "a key with plural": [
        "a plural form",
        "another plural form",
        "could have up to 6 forms with some languages"
    ],
    "a context\u0004a contextualized key": "translation here"
}

Use bin/po2json.js input.po output.json or bin/po2json.js input.po output.json -p for pretty format.

Parsers

You could use xgettext-php parser to parse your files. It provides helpful javascript and handlebars parsers.

License

MIT