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

easy-translate

v2.0.0

Published

A very simple translation library in commonjs format

Downloads

25

Readme

easy-translate

Build Status NPM version Dependencies status

Description

A very simple translation library in commonjs format. It uses almost the same approach as Yii 1 and Drupal. Can be used in node.js as well as in a browser.

Installation

Install with npm:

npm install easy-translate

Usage

var t       = require('easy-translate');
var package = require('./package.json');

t.language('ru');

console.log(t('Hello!'));
// → Привет

console.log(t('It\'s {0} v{1}', package.name, package.version));
// → Это easy-translate v1.0.0

console.log(t('This library can be found at {url}.', {
  url: package.homepage
}));
// → Вы можете найти эту библиотеку по адресу https://github.com/mega…

var count = Object.keys(package.dependencies).length;
console.log(t('It has {0} {0|dependency|dependencies}.', count));
// → Она содержит 0 зависимостей

console.log(t('The current language is {0}.', t.language()));
// Текущий язык - ru

Load translations

// Load built-in plural rules for specified language
var plural = require('easy-translate/langs/ru.js');

t.load({
  language: 'ru',
  translations: {
    'Hello!': 'Привет',
    'It has {0} {0|dependency|dependencies}.': 'Она содержит {0} {0|зависимость|зависимости|зависимостей}.',
    'It\'s {0} v{1}': 'Это {0} v{1}',
    'The current language is {0}.': 'Текущий язык - {0}.',
    'This library can be found at {url}.': 'Вы можете найти эту библиотеку по адресу {url}'
  },
  plural: plural
});

Methods

t(message: string, params?: any, ...otherParams?: any[]): string

Translate a string

params can be:

  • a string, number (template {0} or {n} or {s})
  • an array (templates {0}, {1}, …)
  • an object (templates {user}, {id}, …), also it has special values:
    • _lang: use this language instead of the current
    • _category: by default, all strings a loaded from category app

otherParams is used for positional templates {0}, {1}, …

t.load(languageData: LanguageData)

Loads translations

languageData is object:

  • language: string
  • category: string, "app" by default
  • translations: object similar to { 'Hello': 'Привет', … }
  • plural: function with plural rules for specified language. This library already contains plural rules for 200 languages. See langs folder.

t.language(): string

Get the current language

t.language(language: string)

Set the current language

Template language

t('test') Just returns a translation

t('hi {user}', { 'user': 'Mike' }) Replace '{user}' by 'Mike'

t('It\'s {0} v{1}', name, version) Replace '{0}' by the first argument, {1} by the second

t('{0|dog|dogs}', 2) Choose plural form corresponding to params and a plural rule for the language.

t('{n}', 1) t('{s}', 1) - Aliases for {0}

License

Licensed under MIT.