or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdinstance-management.mdphrase-management.mdpluralization.mdtranslation-interpolation.md
tile.json

tessl/npm-node-polyglot

JavaScript internationalization library providing translation, interpolation, and pluralization capabilities.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/node-polyglot@2.6.x

To install, run

npx @tessl/cli install tessl/npm-node-polyglot@2.6.0

index.mddocs/

Node Polyglot

Node Polyglot is a JavaScript internationalization (i18n) helper library that provides translation, interpolation, and pluralization capabilities. It works in both browser and Node.js environments, offering a simple solution for managing translated phrases with full support for complex pluralization rules across multiple languages.

Package Information

  • Package Name: node-polyglot
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install node-polyglot

Core Imports

const Polyglot = require('node-polyglot');

For ES modules:

import Polyglot from 'node-polyglot';

Basic Usage

const Polyglot = require('node-polyglot');

// Create a new instance
const polyglot = new Polyglot({
  phrases: {
    'hello': 'Hello',
    'hello_name': 'Hello, %{name}!',
    'inbox': 'You have %{smart_count} messages |||| You have one message'
  },
  locale: 'en'
});

// Basic translation
polyglot.t('hello'); // "Hello"

// Translation with interpolation
polyglot.t('hello_name', { name: 'Alice' }); // "Hello, Alice!"

// Pluralization
polyglot.t('inbox', { smart_count: 0 }); // "You have 0 messages"
polyglot.t('inbox', { smart_count: 1 }); // "You have one message"
polyglot.t('inbox', { smart_count: 5 }); // "You have 5 messages"

Architecture

Node Polyglot is built around several key components:

  • Polyglot Class: Main interface for creating translation instances with locale-specific phrase management
  • Phrase Management: Hierarchical phrase storage with dot notation support and nested object flattening
  • Interpolation Engine: Token-based variable substitution with configurable delimiters and custom replacement functions
  • Pluralization System: Locale-aware plural form selection supporting 13+ language types with complex rules
  • Static Transform Function: Standalone phrase transformation without instance creation

Capabilities

Instance Management

Core Polyglot class for creating and managing translation instances with locale-specific phrase storage.

/**
 * Creates a new Polyglot instance
 * @param {Object} options - Configuration options
 * @param {Object} options.phrases - Initial phrases object
 * @param {string} options.locale - Initial locale (default: 'en')
 * @param {boolean} options.allowMissing - Enable missing key handling
 * @param {Function} options.onMissingKey - Custom missing key handler
 * @param {Function} options.warn - Custom warning function
 * @param {Function} options.replace - Custom string replace implementation
 * @param {Object} options.interpolation - Token configuration
 * @param {string} options.interpolation.prefix - Token prefix (default: '%{')
 * @param {string} options.interpolation.suffix - Token suffix (default: '}')
 * @param {Object} options.pluralRules - Custom pluralization rules
 */
function Polyglot(options);

Instance Management

Translation and Interpolation

Core translation functionality with variable interpolation and default value support.

/**
 * Translate a key with optional interpolation and pluralization
 * @param {string} key - Translation key
 * @param {Object|number} options - Interpolation options or smart_count
 * @param {string} options._ - Default value if key not found
 * @param {number} options.smart_count - Count for pluralization
 * @returns {string} Translated and interpolated string
 */
t(key, options);

/**
 * Check if translation exists for given key
 * @param {string} key - Translation key to check
 * @returns {boolean} Whether translation exists
 */
has(key);

Translation and Interpolation

Phrase Management

Methods for adding, updating, and removing phrases with support for nested objects and prefixing.

/**
 * Add or update phrases (supports nested objects)
 * @param {Object} morePhrases - Phrases to add
 * @param {string} prefix - Optional prefix for all keys
 */
extend(morePhrases, prefix);

/**
 * Remove phrases by key or object
 * @param {string|Object} morePhrases - Key string or phrases object to remove
 * @param {string} prefix - Optional prefix for keys
 */
unset(morePhrases, prefix);

/**
 * Replace all phrases with new set
 * @param {Object} newPhrases - New phrases object
 */
replace(newPhrases);

/**
 * Clear all phrases
 */
clear();

Phrase Management

Pluralization

Advanced pluralization system supporting multiple languages with complex plural rules.

/**
 * Get or set current locale
 * @param {string} newLocale - Optional new locale to set
 * @returns {string} Current locale
 */
locale(newLocale);

/**
 * Static method for phrase transformation without instance
 * @param {string} phrase - Phrase to transform
 * @param {Object|number} substitutions - Substitution values or smart_count
 * @param {string} locale - Locale for pluralization
 * @returns {string} Transformed phrase
 */
Polyglot.transformPhrase(phrase, substitutions, locale);

Pluralization

Types

/**
 * Polyglot constructor options
 * @typedef {Object} PolyglotOptions
 * @property {Object} phrases - Initial phrases object
 * @property {string} locale - Initial locale (default: 'en')
 * @property {boolean} allowMissing - Enable missing key handling
 * @property {Function} onMissingKey - Custom missing key handler
 * @property {Function} warn - Custom warning function
 * @property {Function} replace - Custom string replace implementation
 * @property {Object} interpolation - Token configuration
 * @property {string} interpolation.prefix - Token prefix (default: '%{')
 * @property {string} interpolation.suffix - Token suffix (default: '}')
 * @property {Object} pluralRules - Custom pluralization rules
 */

/**
 * Pluralization rules configuration
 * @typedef {Object} PluralRules
 * @property {Object.<string, Function>} pluralTypes - Mapping from plural type names to functions
 * @property {Object.<string, Array<string>>} pluralTypeToLanguages - Mapping from plural types to language codes
 */

/**
 * Translation options for t() method
 * @typedef {Object} TranslationOptions
 * @property {string} _ - Default value if key not found
 * @property {number} smart_count - Count for pluralization
 * @property {...*} [variable] - Additional variables for interpolation
 */