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

translatee

v1.0.1

Published

A simple and flexible translate function for js.

Downloads

3

Readme

Translatee

2020-04-02

A simple and flexible translate function for js.

Install

npm install translatee

Example

const Translatee = require('translatee');

/**
 * Notice that for my personal tastes in french/english, I use only two plural forms: the singular form if n=1,
 * and the plural form if n=0 or n>1. The cool thing with translatee is that we can create our own plural rules,
 * and so for instance another person might prefer to have three plural forms: one for 0, one for 1, and one for >1 for instance.
 *
 */
var dictionaryFR = {
    'hello': [
        'bonjour',
    ],
    'hello you': [
        'bonjour {name}',
    ],
    'I have x cats': [
        "J'ai 1 chat",
        "J'ai {x} chats",
    ],
};

var dictionaryEN = {
    'hello': [
        'hello',
    ],
    'hello you': [
        'hello {name}',
    ],
    'I have x cats': [
        "I have 1 cat",
        "I have {x} cats",
    ],
};


//----------------------------------------
// SETUP
//----------------------------------------
var translatee = new Translatee();
/**
 * Note: this is the default plural rule in translatee if none is passed.
 * I explicitly write it for the sake of understanding the api.
 * This function must return the plural index.
 */
var myPluralRule = function (n) {
    if (1 === n) {
        return 0;
    }
    return 1;
};
translatee.addDictionary("fr", dictionaryFR, myPluralRule);
translatee.addDictionary("en", dictionaryEN, myPluralRule);
translatee.setDefaultLang("fr");


//----------------------------------------
// USAGE
//----------------------------------------

console.log(translatee._("hello")); // bonjour
console.log(translatee._("hello you", {
    name: "boris",
})); // bonjour boris
console.log(translatee._n("I have x cats", 5)); // J'ai 5 chats

Class Overview

Plural/non-plural

Translatee distinguishes two types of strings:

  • non-plural string (which doesn't involve a plural)
  • plural string (which involves a plural)

The _ method fetches non-plural translations. The _n method fetches plural translations.

Plural selector and dictionary structure

The plural selector is a function that returns a plural index. The plural index indicates which translation (in the dictionary) to choose from when a plural form is required.

The dictionary is a map of msgId to array of translations. In singular string (aka non-plural string), the array contains only one translation. In plural strings, the array contains more than one translation, depending on the language, and the plural selector function returns the index of the translation to return. The order of the translation depends on the person who creates the dictionary.

In translatee, the plural selector function is always bound to a dictionary.

Strict mode

With strict mode on, all errors throw an exception. With strict mode off, the translation methods always return the msg id if there is an error.

History log

  • 1.0.0 -- 2020-04-02

    • initial commit