Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

i18n-plus

2.0.6 • Public • Published

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

Documentation

You can read the full documentation with examples there.

Simple usage in a brief

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

There are two specifiers available for you to 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 } 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 dictionary
const localeValues = {
    [Locales.en]: {
        [localeKeys.home.welcome]:
            "Welcome, :user! You have :[messages, { zero: 'messages', one: 'message', other: 'messages' }]"
    }
};
 
let localeHelper = new LocaleHelper(localeKeys, localeValues);
 
// 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
});

Running tests

You can run all tests using npm run test

Compiling documentation

This project uses jsdoc to compile documentation to HTML files to docs directory. You can run the process with npm run genDocs.

Install

npm i i18n-plus

DownloadsWeekly Downloads

41

Version

2.0.6

License

MIT

Unpacked Size

49.2 kB

Total Files

16

Last publish

Collaborators

  • avatar