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

simple-tr8n

v1.1.2

Published

Really simple JavaScript (TypeScript) library for translating user-facing messages.

Downloads

9

Readme

simple-tr8n-js

Build Status

Really simple JavaScript (TypeScript) library for translating user-facing messages. Designed for JavaScript libraries that don't want to require their client applications to use any specific heavyweight i18n framework.

Places all translation operations behind the Translator interface (declared here). Clients of your library then have two options:

  1. Use the provided SimpleTranslator implementation.
  2. Provide their own implementation of Translator, which can then delegate to whichever i18n framework they choose.

The simple implementation supports argument substitution with %{argName} syntax as well as plurals (varying messages based on one numerical input argument).

It does NOT currently support gender or other more advanced i18n framework features.

How to Use in Your Library

Add dependency using npm or yarn.

$ npm install simple-tr8n

Define an enum for all messages to be translated, giving each one a unique string value. Document any interpolated arguments and their types in the comments. For example:

export const enum MyLibMsgType {
  /** Args: userFirstName (string), userAge (number). */
  EXAMPLE_MSG_A = 'mylib.a',

  /** Args: emailCount (number). */
  EXAMPLE_MSG_B = 'mylib.b',

  // ...
}

Define default language translations (or configure a tool to generate them), each with a simple string template or a list of plural cases:

const enTranslations: SimpleTranslatedMsgConfigs<MyLibMsgType> = {
  [MyLibMsgType.EXAMPLE_MSG_A]: '%{userFirstName} is %{userAge} years old',

  [MyLibMsgType.EXAMPLE_MSG_B]: [
    {count: 0, msg: 'You have no new email messages'},
    {count: 1, msg: 'You have 1 new email message'},

    // Covers emailCount >= 2 cases:
    {count: 2, msg: 'You have %{emailCount} new email messages'},
  ],

  // ...
};

For each translated language you support, define (or generate) a similar SimpleTranslatedMsgConfigs<MyLibMsgType> object. Note that different languages can have fewer or more plural cases as needed.

Parameterize your library so that it can be configured with an implementation of the Translator interface for your message type. For example:

export class MyLibContext {
  constructor(readonly translator: Translator<MyLibMsgType>) {}
  // ...
}

Optionally configure with your primary language translations (e.g. enTranslations) as a default value:

const translator = new SimpleTranslator<MyLibMsgType>(enTranslations);

For convenience, export a type alias to represent messages within your library:

import {TransMsg} from 'simple-tr8n';

export type MyLibMsg = TransMsg<MyLibMsgType>;

When your library needs to produce translated messages, use the configured implementation:

const msgA = translator.translate(new MyLibMsg(MyLibMsgType.EXAMPLE_MSG_A, {
  userFirstName: 'Alice',
  userAge: 34,
}));

// Last arg tells the library which key is the plural count.
const msgB = translator.translate(
    new MyLibMsg(MyLibMsgType.EXAMPLE_MSG_B, {emailCount: 3}, 'emailCount'));