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

translate-js

v1.3.0

Published

manage translations and localization with simple api

Downloads

4,882

Readme

Translate JS

Build Status Coverage Status NPM Status

Lightweight library for managing translations and localization with simple api. Works client and server side. Less then 1.5KB.

Installation

npm install translate-js --save

Basic usage

// add items to translate registry
translate.add({
    projectTitle: 'Project title',
    button: {
        text: 'Click me {{ userName }}!',
        caption: 'Please click me!'
    },
    itemsCaption: [
        '{{ count }} item',
        '{{ count }} items'
    ],
    deep: {
        nested: {
            label: 'Deep nested label'
        }
    }
});

// get translations
translate('projectTitle'); // outputs  "Project title"
translate('button.text', {userName: 'George'}); // outputs  "Click me George!"
translate('button.caption'); // outputs  "Please click me!"
translate('itemsCaption', {count: 1}); // outputs  "1 item"
translate('itemsCaption', {count: 2}); // outputs  "2 items"
translate('deep.nested.label'); // outputs  "Deep nested label"

Advanced usage

// add items to specific locale
translate.add({projectTitle: 'Project title'}, 'en');
translate.add({projectTitle: 'Título del Proyecto'}, 'es');

// add items to specific locale and prefix keys with custom namespace
translate.add({numberInput: 'Value is not a number'}, 'en', 'errors');
translate.add({numberInput: 'El valor no es un número'}, 'es', 'errors');

// get translations
translate('projectTitle'); // outputs  "Project title"
translate('projectTitle', null, {locale: 'es'}); // outputs  "Título del Proyecto!"
translate('errors.numberInput'); // outputs  "Value is not a number"

// set locale for future translate calls
translate.setLocale('es');
translate.getLocale(); // outputs "es"
translate('errors.numberInput'); // outputs  "El valor no es un número"

// change what happens when item is not in registry
translate.whenUndefined = function(key, locale) {
    return key + ':undefined:' + locale
};
translate('errors.dateInput'); // outputs  "errors.dateInput:undefined:es"

// change how translate interpolates template strings
translate.add({welcomeMessage: 'Hello $userName'});
translate.interpolateWith(/\$(\w+)/g);
translate('welcomeMessage', {userName: 'George'}); // outputs "Hello George"

// clear / empty translate registry
translate.clear();

// create new translate registry
translate.createRegistry();

API

translate

translate(key, templateData, options);

Translates string stored under specified key to current locale. Interpolates template string if templateData is given.

Specific locale translation can be specified via options.locale.

Pluralization is done in reference to "count" template data value when multiple variables are passed to translate function (override with options.pluralizeTo).

translate.add

translate.add(items, locale, keyPrefix);

Imports items (plain or nested object) to translate registry under specific locale (defaults to "en"). Prefix on item keys can optionally be added as keyPrefix parameter; Plural translation form variations must me supplied as array.

translate.setLocale

translate.setLocale(locale);

Sets current locale for future translate calls.

translate.getLocale

translate.getLocale();

Gets current locale

translate.interpolateWith

translate.interpolateWith(interpolateRE);

Sets regular expression for template strings interpolation.

translate.whenUndefined

translate.whenUndefined = function(key, locale) {};

Define custom handler for use case when requested item is not in registry.

translate.clear

translate.clear();

clear / empty all items in translate registry

translate.setPluralizationRule

translate.setPluralizationRule('en', function(count) {
    return (1 === count) ? 0 : 1;
}, {pluralizeTo: 'count'});

Set locale specific pluralization rule function to determine plural form variation index.

translate.createRegistry

translate.createRegistry();

Creates new isolated translate registry