CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-node-polyglot

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

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

translation-interpolation.mddocs/

Translation and Interpolation

Core translation functionality providing variable interpolation, default value support, and translation key existence checking.

Capabilities

Translation Method (t)

The primary method for translating keys with optional interpolation and pluralization.

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

Usage Examples:

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

const polyglot = new Polyglot({
  phrases: {
    'hello': 'Hello',
    'hello_name': 'Hello, %{name}!',
    'welcome_user': 'Welcome %{name}, you have %{count} messages',
    'items_zero': 'No items',
    'items_one': 'One item', 
    'items_other': '%{smart_count} items'
  }
});

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

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

// Multiple variable interpolation  
polyglot.t('welcome_user', { 
  name: 'Bob', 
  count: 5 
}); // "Welcome Bob, you have 5 messages"

// Translation with default value
polyglot.t('missing_key', { 
  _: 'Default message' 
}); // "Default message"

// Pluralization using smart_count
polyglot.t('items', { smart_count: 0 }); // "No items"
polyglot.t('items', { smart_count: 1 }); // "One item"
polyglot.t('items', { smart_count: 5 }); // "5 items"

// Using number shortcut for smart_count
polyglot.t('items', 0); // "No items"
polyglot.t('items', 1); // "One item"
polyglot.t('items', 5); // "5 items"

// Missing key fallback (returns key itself by default)
polyglot.t('nonexistent_key'); // "nonexistent_key"

Has Method

Check if a translation exists for a given key.

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

Usage Examples:

const polyglot = new Polyglot({
  phrases: {
    'hello': 'Hello',
    'nested.key': 'Nested value'
  }
});

// Check existing keys
polyglot.has('hello'); // true
polyglot.has('nested.key'); // true

// Check non-existent keys
polyglot.has('goodbye'); // false
polyglot.has('nested.missing'); // false

// Use has() to conditionally translate
if (polyglot.has('optional_message')) {
  console.log(polyglot.t('optional_message'));
} else {
  console.log('No optional message available');
}

// Provide fallback translations
const message = polyglot.has('custom_greeting') 
  ? polyglot.t('custom_greeting', { name: 'User' })
  : polyglot.t('default_greeting', { name: 'User' });

Interpolation System

Variable Interpolation

Replace tokens in translated phrases with provided values.

Token Format:

  • Default: %{variable_name}
  • Configurable via interpolation options in constructor

Interpolation Rules:

  • Missing variables remain as-is in the output
  • Null/undefined values are treated as missing
  • Variables can be any type that converts to string
const polyglot = new Polyglot({
  phrases: {
    'user_info': 'User: %{name}, Age: %{age}, Active: %{active}',
    'partial': 'Hello %{name}, your score is %{score}'
  }
});

// Complete interpolation
polyglot.t('user_info', {
  name: 'Alice',
  age: 25,
  active: true
}); // "User: Alice, Age: 25, Active: true"

// Partial interpolation (missing variables remain)
polyglot.t('partial', { 
  name: 'Bob' 
}); // "Hello Bob, your score is %{score}"

// Null/undefined values treated as missing
polyglot.t('partial', { 
  name: 'Charlie',
  score: null
}); // "Hello Charlie, your score is %{score}"

Default Values

Provide fallback text when translation keys are missing.

const polyglot = new Polyglot({
  phrases: {
    'existing': 'This exists'
  }
});

// Use default value for missing key
polyglot.t('missing_key', {
  _: 'This is the default value'
}); // "This is the default value"

// Default value with interpolation
polyglot.t('missing_greeting', {
  _: 'Hello, %{name}!',
  name: 'World'
}); // "Hello, World!"

// Existing key takes precedence over default
polyglot.t('existing', {
  _: 'This default will be ignored'
}); // "This exists"

Error Handling

Missing Key Behavior

Default behavior when translation keys are not found:

  1. Check for _ (underscore) default value in options
  2. Call custom onMissingKey handler if configured
  3. Display warning message if warn function is set
  4. Return the key itself as fallback
// Default behavior - returns key
const polyglot = new Polyglot();
polyglot.t('missing'); // "missing"

// Custom missing key handler
const customPolyglot = new Polyglot({
  onMissingKey: (key) => `[MISSING: ${key}]`
});
customPolyglot.t('missing'); // "[MISSING: missing]"

// Allow missing keys (uses transformPhrase as handler)
const lenientPolyglot = new Polyglot({
  allowMissing: true
});
lenientPolyglot.t('missing', { smart_count: 1 }); // "missing"

docs

index.md

instance-management.md

phrase-management.md

pluralization.md

translation-interpolation.md

tile.json