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

translation-dictionary

v0.1.1

Published

JavaScript gettext-style translation output library with sprintfs parameter replace.

Downloads

5

Readme

translation-dictionary

JavaScript gettext-style translation output library with sprintf-js parameter replace.

Build Status Dependency Status NPM version

Install

Node.js

$ npm install translation-dictionary

Browser

$ bower install translation-dictionary

<!-- with EventEmitter ~9 kb -->
<script src="bower_components/translation-dictionary/build/translation-dictionary.min.js"></script>

<!-- or without EventEmitter ~6.4 kb -->
<script src="bower_components/translation-dictionary/build/translation-dictionary.woe.min.js"></script>

Usage

var dict = new TranslationDictionary();

dict.registerTranslation('de', {
    'I have a car'               : 'Ich habe ein Auto',
    'I have %d cars'             : 'Ich habe %d Autos',
    'Hello %s %s'                : 'Hallo %s %s',
    'Name: %2$s, Firstname: %1$s': 'Name: %2$s, Vorname: %1$s',
    'Named arguments %(arg1)s'   : 'Benannte Argumente %(arg1)s'
});
dict.setLocale('de');

dict.__('I have a car');                                  // 'Ich habe ein Auto'
dict._p('I have a car', 'I have %d cars', 2);             // 'Ich habe 2 Autos'
dict.__('Hello %s %s', 'John', 'Doe');                    // 'Hallo John Doe'
dict.__('Name: %2$s, Firstname: %1$s', 'John', 'Doe');    // 'Name: Doe, Vorname: John'
dict.__('Named arguments %(arg1)s', { arg1: 'inserted'}); // 'Named arguments inserted'

View example/ for detailed examples.

API

getLocale ()

Return the current locale (default: 'en').

registerPluralizer (locale, pluralizer[, nPlurals=2])

Set a pluralizer for a locale with the number of plurals.

registerPluralizer('cs', function(n) {
    return (n === 1) ? 0 : ( n >= 2 && n <= 4) ? 1 : 2; 
}, 3);

registerTranslation (locale, translations)

Merge translations to a locale.

// nPlurals=2 in german
registerTranslation('de', {
    'wolve' : 'wolf',
    'wolves': 'wölfe'
});

// nPlurals=3 in czech
dict.registerTranslation('cs', {
    'wolve' : 'vlk',
    'wolves': [
        'vlci',
        'vlků'
    ]
});

setBaseLocale (locale[, nPlurals=2])

Set the applications base language.

setLocale (locale)

Set the current locale.

translate (arguments)

Translate a singular text.

arguments[0] The text to translate. [arguments[n]] Optional parameters for sprintf to replace markers in the text.

Depending on the markers a list of parameters, an array, or an object can be used.

translate('a text');
translate('a text %s %s', 'str1', 'str2');
translate('a text %s %s', [ 'str1', 'str2' ]);
translate('a text %(name)s', { name: 'John Doe' });

__ (arguments)

Alias for translate ().

translatePlural (arguments)

Translate a text as singular or plural, depending on the count.

arguments[0] The singular text to translate. arguments[1..(nPlurals-1)] The plural text to translate. arguments[nPlurals..n] The count and parameters for sprintf to replace markers in the text.

Depending on the markers a list of parameters, an array, or an object can be used.

translatePlural('a text', 'some text', 47);
translatePlural('a text %d %s', 'some text %d %s', 47, 'str1');
translatePlural('a text %d %s', 'some text %d %s', [ 47, 'str1' ]);
translatePlural('a text %(name)s', 'some text %(name)s', { count: 47, name: 'John Doe' });

_p (arguments)

Alias for translatePlural ().