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

i18n-plus

v3.1.7

Published

Node.js package providing i18n with variable interpolation & conjugation of words with respect to quantifiers, supporting all languages' conjugation rules.

Downloads

6

Readme

i18n-plus

GitHub issues GitHub forks GitHub stars GitHub license

A node.js package providing i18n with variable interpolation & conjugation of words with respect to quantifiers, supporting all languages' conjugation rules.

Features

  • Translating strings using user-defined dictionaries (key-value system)
  • Interpolation of strings with variables
  • User-defined conjugation (e.g. for pluralization, grammatical case conjugation, etc.) with respect to the quantity, passed in as a variable
  • Strongly typed with TypeScript
  • Transpilation of YAML-defined translation key dictionaries into statically-typed, importable TypeScript code
  • Supports splitting key dictionaries into multiple files with !include() directives

Documentation

You can read the full documentation with examples there.


Usage

Basic usage in a brief

The key function provided is trans(key, interpolationParams = {})

There are two specifiers available for use:

  1. Interpolation syntax :variableName - simply replaces all such fields with the corresponding values supplied as Object properties in the second argument of trans call
  2. Conjugation syntax :[quantityVarName, { zero: 'values', one: 'value', other: 'values' } ] -
import { Locales, LocaleHelper, Translation } from "i18n-plus";

// these will be the keys used in the dictionary to identify texts
const localeKeys = {
	home: { welcome: "home.welcome" },
};

// this is the actual translation for a single language
const translationEN: Translation<typeof LocaleKeys> = {
	home: {
		welcome:
			"Welcome, :user! You have :[messages, { zero: 'messages', one: 'message', other: 'messages' }]",
	},
};

let localeHelper = new LocaleHelper(localeKeys, {
	[Locales.en]: translationEN,
});

// here, you can do whatever you want with the translated & interpolated text, e.g. send it with an HTTP response, render it as a React or HTML component, log it to the console, etc.

let welcomeMessage = localeHelper.trans(localeKeys.home.welcome, {
	user: "Elon Musk",
	messages: 5,
});

Advanced usage - defining

What makes this library special is the integrated CLI that enables developers to define their translation keys in a ligthweight, clean manner, by placing them in YAML with support for additional directives.

Considering the above example:

const localeKeys = {
	home: { welcome: "home.welcome" },
};

can be re-written as:

home:
    - welcome

which is transpiled by the CLI to just the TS code above which can be imported right into the actual project code. To do so, the project supports two scenarios:

  • single-time compilation, e.g. in a buildscript: i18n-plus compileKeys keys/entrypoint.yaml i18n/LocaleKeys.ts, which would compile keys/entrypoint.yaml into TS and output the bundle to i18n/LocaleKeys.ts
  • development compilation with watching, e.g. in a development script, which does the same as the above scenario, but also watches for changes in all files referenced by the entrypoint and the entrypoint itself, recompiling & rebuilding file dependency tree on file changes. The command is the same, all that is needed is just appending the --dev (or short -d) switch: i18n-plus compileKeys keys/entrypoint.yaml i18n/LocaleKeys.ts --dev

You can easily access the documentation of the tool by running i18n-plus -?, i18n-plus -h, or just failing to provide a valid command syntax, which will trigger help automatically and describe the problem on the bottom, e.g. running i18n-plus compileKeys keys/entrypoint.yaml (please note the missing output path) will print the following:

compileI18n compileKeys <input> <output>

compile I18n yaml files to importable TS bundle

Positionals:
  input   root YAML input file path                          [string] [required]
  output  output TS bundle file path                         [string] [required]

Options:
      --version   Show version number                                  [boolean]
  -d, --dev                                           [boolean] [default: false]
  -?, -h, --help  Show help                                            [boolean]

Not enough non-option arguments: got 1, need at least 2

Moreover, for the sake of larger projects, keys can be split into multiple files. Consider the example:

const localeKeys = {
	home: {
		welcome: "home.welcome",
		mainPanel: {
			text1: "home.mainPanel.text1",
			text2: "home.mainPanel.text2",
		},
	},
	login: {
		heading: "login.heading",
		buttons: {
			signIn: "login.buttons.signIn",
			register: "login.buttons.register",
		},
	},
};

To simplify the definition and maintain readability, the structure can be split into three files:

entrypoint.yaml

home: !include(./home.yaml)
login: !include(./login.yaml)

home.yaml

- welcome
- mainPanel:
      - text1
      - text2

login.yaml

- heading
- buttons:
      - signIn
      - register

Compatibility

TS files generated by the CLI with compileKeys are compatible with react-i18next.


Unit tests

This project uses jest accompanied by ts-jest for unit testing. You can run all tests using npm test.

Documentation

This project uses jsdoc to compile documentation to HTML files to docs directory. You can run the process with npm run genDocs. The docs will be written to docs/i18n-plus/X.X.X, and the only manual requirement is to put a proper entry to line $10$ in docs/index.html: const VERSIONS = [..., "X.X.X"];.

Changelog

The changelog is available on github and is auto-generated by auto-changelog, available as a script: npm run changelog.