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

@soydaddy/i18n

v1.2.1

Published

Simple and lightweight message localisation

Downloads

7

Readme

@soydaddy/i18n

Simple and lightweight message localization for JavaScript projects.

Installation

  • pnpm add @soydaddy/i18n or
  • yarn add @soydaddy/i18n or
  • npm i @soydaddy/i18n

Usage

const I18n = require('@soydaddy/i18n');
const path = require('path');

// Initialize I18n with default locale and an empty locales object
const i18n = new I18n('english', {});

// Load locales asynchronously
(async () => {
    await i18n.loadLocales(path.join(__dirname, 'locales'));
})();

// Check if the locale exists and get the locale
const messages = i18n.getLocale('spanish');
console.log(messages('example')); // -> Hola, mundo

// Get a message from a specific locale
console.log(i18n.getMessage('spanish', 'example')); // -> Hola, mundo

Examples

Manually Loading Translations

Load translations manually by creating a locales object and passing it to the I18n constructor:

const I18n = require('@soydaddy/i18n');

const locales = {
    english: {
        greeting: {
            hello: 'Hello, world'
        }
    },
    spanish: {
        greeting: {
            hello: 'Hola, mundo'
        }
    }
};

const i18n = new I18n('english', locales);

console.log(i18n.getMessage('spanish', 'greeting.hello')); // -> Hola, mundo
console.log(i18n.getMessage('english', 'greeting.hello')); // -> Hello, world

Loading All Translation Files

To load all translation files from a directory, use the loadLocales method:

const I18n = require('@soydaddy/i18n');
const path = require('path');

const i18n = new I18n('english', {});

(async () => {
    await i18n.loadLocales(path.join(__dirname, 'locales'));

    console.log(i18n.getMessage('spanish', 'greeting.hello')); // -> Hola, mundo
    console.log(i18n.getMessage('english', 'greeting.hello')); // -> Hello, world
})();

Directory Structure for Translations

Ensure you have the following directory structure for your translation files:

locales/
├── english/
│   └── greeting.yml
├── spanish/
│   └── greeting.yml

locales/english/greeting.yml

greeting:
  hello: "Hello, world"

locales/spanish/greeting.yml

greeting:
  hello: "Hola, mundo"

Placeholders

i18n supports both positional and named placeholders.

{ // a locale object
    "positional": {
        "strings": "I like %s", 
        "numbers": "%d %s %d = %d"
    },
    "named": {
        "example1": "Hi, I'm {name} and I am from {location}",
        "example2": "Hi, I'm {person.name} and I am from {person.location}"
    }
}
messages('positional.strings', 'chocolate'); // I like chocolate

messages('positional.numbers', 5, '+', 5, 10); // 5 + 5 = 10

messages('named.example1', {
    name: 'Someone',
    location: 'Somewhere'
}); // Hi, I'm Someone and I am from Somewhere

messages('named.example2', {
    person: {
        name: 'Someone',
        location: 'Somewhere'
    }
}); // Hi, I'm Someone and I am from Somewhere

Pluralization

i18n supports basic pluralization. If the message is an array, the first placeholder value will be "eaten" (consumed) and the correct message will be returned.

{ // a locale object
    "example1": [
        "You only have one %s",
        "You have %d %ss"
    ],
    "example2": [
        "You don't have any {item}s",
        "You only have one {item}",
        "You have {number} {item}s"
    ]
}
messages('example1', 1, 1, 'item'); // You only have one item
messages('example2', 0, { number: 0, item: 'car' }); // You don't have any cars

API

new I18n(default_locale, locales, fallback_locale?)

Create a new I18n instance.

  • default_locale - The name of the default locale
  • locales - An object of localized messages
  • fallback_locale - Optional fallback locale if a translation is missing in the requested locale

I18n#default_locale

The name of the default locale.

I18n#locales

An array of the names of the locales you created.

I18n#loadLocales(localesPath: string): Promise<void>

Asynchronously loads locales from the specified path.

I18n#getAllMessages(message, ...args)

Get a message in all locales.

  • message - Dot notation string for the message
  • ...args - Placeholders/pluralization

I18n#getLocale(locale)

Get a locale.

  • locale - Locale name

Returns a function which calls I18n#getMessage using the given locale name (or the default).

I18n#getMessage(locale, message, ...args)

Get a message from a specific locale.

  • locale - Locale name
  • message - Dot notation string for the message
  • ...args - Placeholders/pluralization

I18n#getMessages(locales, message, ...args)

Get a message from multiple locales.

  • locales - Array of locale names
  • message - Dot notation string for the message
  • ...args - Placeholders/pluralization

Support

Discord support server

© 2023 soyDaddy