StrongLoop Globalize

6.0.6 · active · verified Tue Apr 21

strong-globalize is a JavaScript library for internationalization and localization (often called globalization) within Node.js applications. Currently at version 6.0.6, it provides a comprehensive API for formatting messages, currencies, dates, and numbers, as well as utility wrappers for Node.js console and `util.format`. It builds upon the Unicode CLDR data and the `jquery/globalize` library, offering features like autonomous message loading, runtime language switching, pseudo-localization, and deep string resource extraction from various file types (JSON, YAML, HTML). The library also includes command-line tools for extracting, linting, and translating strings from source code, facilitating a complete globalization workflow. It targets Node.js version 10 and above, ensuring compatibility with modern Node.js environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing strong-globalize with a default language, manually adding messages, and utilizing various formatting (messages, currency, date, number) and logging APIs. It also shows runtime language switching.

import g, { SG } from 'strong-globalize';
import path from 'path';

// Set the root directory for message files (e.g., 'locales/en.json') and default language
// In a real app, 'locales' might be in a dist folder or root.
SG.SetRootDir(path.join(process.cwd(), 'locales'));
SG.SetDefaultLanguage('en');

// Manually add messages for demonstration. In a real app, these would typically be loaded
// from JSON/YAML files located in the rootDir or added by the CLI 'extract' command.
SG.addMessages('en', {
  'app.greeting': 'Hello, {name}!',
  'app.welcome': 'Welcome to strong-globalize. Today is {date}.',
  'app.price': 'The price is {0, currency} at {1, number} units.'
});
SG.addMessages('fr', {
  'app.greeting': 'Bonjour, {name}!',
  'app.welcome': 'Bienvenue à strong-globalize. Aujourd\'hui, nous sommes le {date}.',
  'app.price': 'Le prix est de {0, currency} pour {1, number} unités.'
});

// Change language at runtime for the 'g' instance (e.g., based on user preference)
g.setLanguage('en');

console.log(g.f('app.greeting', { name: 'World' }));
console.log(g.formatMessage('app.welcome', { date: new Date() }));
console.log(g.c(123.45, 'USD')); // Defaults to US dollar format
console.log(g.formatCurrency(543.21, 'EUR', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));

// Switch to French
g.setLanguage('fr');
console.log(g.f('app.greeting', { name: 'Monde' }));
console.log(g.formatMessage('app.welcome', { date: new Date() }));

console.log(g.d(new Date(), { skeleton: 'long' })); // Date formatting
console.log(g.n(1234567.89, { maximumFractionDigits: 0 })); // Number formatting

// Example using RFC 5424 Syslog severity wrappers
g.error('A critical error occurred: {reason}', { reason: 'disk full' });
g.warning('Potential issue: {count} warnings detected.', { count: 3 });

view raw JSON →