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

phrase-management.mddocs/

Phrase Management

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

Capabilities

Extend Method

Add or update phrases in the Polyglot instance, with support for nested objects and automatic key prefixing.

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

Usage Examples:

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

const polyglot = new Polyglot();

// Basic extend
polyglot.extend({
  'hello': 'Hello',
  'goodbye': 'Goodbye'
});

console.log(polyglot.phrases); 
// { 'hello': 'Hello', 'goodbye': 'Goodbye' }

// Extend with prefix
polyglot.extend({
  'morning': 'Good morning',
  'evening': 'Good evening'  
}, 'greetings');

console.log(polyglot.phrases);
// { 
//   'hello': 'Hello', 
//   'goodbye': 'Goodbye',
//   'greetings.morning': 'Good morning',
//   'greetings.evening': 'Good evening'
// }

// Nested object support (automatically flattened)
polyglot.extend({
  'nav': {
    'home': 'Home',
    'about': 'About',
    'contact': {
      'email': 'Email Us',
      'phone': 'Call Us'
    }
  }
});

console.log(polyglot.phrases);
// { 
//   ...existing phrases,
//   'nav.home': 'Home',
//   'nav.about': 'About', 
//   'nav.contact.email': 'Email Us',
//   'nav.contact.phone': 'Call Us'
// }

// Nested object with prefix
polyglot.extend({
  'login': 'Log In',
  'register': 'Sign Up',
  'settings': {
    'profile': 'Profile',
    'privacy': 'Privacy'
  }
}, 'user');

console.log(polyglot.phrases);
// {
//   ...existing phrases,
//   'user.login': 'Log In',
//   'user.register': 'Sign Up',
//   'user.settings.profile': 'Profile', 
//   'user.settings.privacy': 'Privacy'
// }

// Overwriting existing keys
polyglot.extend({
  'hello': 'Hi there!' // Overwrites previous 'hello'
});

Unset Method

Remove phrases from the Polyglot instance by key string or object structure.

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

Usage Examples:

const polyglot = new Polyglot({
  phrases: {
    'hello': 'Hello',
    'goodbye': 'Goodbye',
    'nav.home': 'Home',
    'nav.about': 'About',
    'user.login': 'Log In',
    'user.profile': 'Profile'
  }
});

// Remove single key by string
polyglot.unset('hello');
console.log(polyglot.has('hello')); // false

// Remove multiple keys using object structure
polyglot.unset({
  'home': 'Home',     // Removes 'nav.home'
  'about': 'About'    // Removes 'nav.about'
}, 'nav');

console.log(polyglot.has('nav.home'));  // false
console.log(polyglot.has('nav.about')); // false

// Remove nested structure 
polyglot.unset({
  'settings': {
    'profile': 'Profile',
    'privacy': 'Privacy'
  }
}, 'user');
// This would remove 'user.settings.profile' and 'user.settings.privacy'

// Remove object without prefix
polyglot.unset({
  'user.login': 'Log In',
  'user.profile': 'Profile'
});
console.log(polyglot.has('user.login'));   // false
console.log(polyglot.has('user.profile')); // false

Replace Method

Completely replace all phrases with a new set, clearing existing phrases first.

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

Usage Examples:

const polyglot = new Polyglot({
  phrases: {
    'old_hello': 'Old Hello',
    'old_goodbye': 'Old Goodbye'
  }
});

console.log(polyglot.phrases);
// { 'old_hello': 'Old Hello', 'old_goodbye': 'Old Goodbye' }

// Replace all phrases
polyglot.replace({
  'new_hello': 'New Hello',
  'new_welcome': 'Welcome',
  'nav': {
    'home': 'Home Page'
  }
});

console.log(polyglot.phrases);
// { 'new_hello': 'New Hello', 'new_welcome': 'Welcome', 'nav.home': 'Home Page' }

console.log(polyglot.has('old_hello'));    // false
console.log(polyglot.has('old_goodbye'));  // false
console.log(polyglot.has('new_hello'));    // true

Clear Method

Remove all phrases from the instance.

/**
 * Clear all phrases from the instance
 */
clear();

Usage Examples:

const polyglot = new Polyglot({
  phrases: {
    'hello': 'Hello',
    'goodbye': 'Goodbye',
    'nav.home': 'Home'
  }
});

console.log(Object.keys(polyglot.phrases).length); // 3

// Clear all phrases
polyglot.clear();

console.log(Object.keys(polyglot.phrases).length); // 0
console.log(polyglot.phrases); // {}

// All translation attempts will now return key or use missing key handler
console.log(polyglot.t('hello')); // "hello" (fallback behavior)

Nested Object Flattening

Dot Notation Keys

Nested phrase objects are automatically flattened into dot notation keys for internal storage.

// Input nested object
const phrases = {
  'app': {
    'title': 'My App',
    'sections': {
      'header': 'Header Text',
      'footer': 'Footer Text'
    }
  },
  'user': {
    'profile': {
      'name': 'Name',
      'email': 'Email Address'  
    }
  }
};

const polyglot = new Polyglot();
polyglot.extend(phrases);

// Results in flattened keys
console.log(polyglot.phrases);
// {
//   'app.title': 'My App',
//   'app.sections.header': 'Header Text',
//   'app.sections.footer': 'Footer Text', 
//   'user.profile.name': 'Name',
//   'user.profile.email': 'Email Address'
// }

// Access using flattened keys
polyglot.t('app.title');              // "My App"
polyglot.t('app.sections.header');    // "Header Text"
polyglot.t('user.profile.email');     // "Email Address"

Prefix Combination

When using prefixes with nested objects, the prefix and nested keys are combined using dot notation.

const polyglot = new Polyglot();

polyglot.extend({
  'messages': {
    'success': 'Operation successful',
    'error': {
      'validation': 'Validation failed',
      'network': 'Network error'
    }
  }
}, 'api');

console.log(polyglot.phrases);
// {
//   'api.messages.success': 'Operation successful',
//   'api.messages.error.validation': 'Validation failed', 
//   'api.messages.error.network': 'Network error'
// }

// Access with full path
polyglot.t('api.messages.success');              // "Operation successful"
polyglot.t('api.messages.error.validation');     // "Validation failed"

docs

index.md

instance-management.md

phrase-management.md

pluralization.md

translation-interpolation.md

tile.json