{"id":16229,"library":"strong-globalize","title":"StrongLoop Globalize","description":"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.","status":"active","version":"6.0.6","language":"javascript","source_language":"en","source_url":"git://github.com/strongloop/strong-globalize","tags":["javascript","StrongLoop","globalize","cldr","typescript"],"install":[{"cmd":"npm install strong-globalize","lang":"bash","label":"npm"},{"cmd":"yarn add strong-globalize","lang":"bash","label":"yarn"},{"cmd":"pnpm add strong-globalize","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The 'g' object represents the primary globalization instance, providing methods for message formatting, date/number/currency localization, and logging. It is typically the default export of the module.","wrong":"const g = require('strong-globalize').g;","symbol":"g","correct":"import g, { SG } from 'strong-globalize';"},{"note":"SG (Strong Globalize) is a static object containing system-wide configuration methods, such as `SetDefaultLanguage` and `SetRootDir`. It is exposed as a named export.","wrong":"import SG from 'strong-globalize';","symbol":"SG","correct":"import { SG } from 'strong-globalize';"},{"note":"TypeScript types `G` for the 'g' instance and `SGType` for the static 'SG' configuration object are available to improve type safety in your application.","symbol":"Types","correct":"import type { G, SG as SGType } from 'strong-globalize';"}],"quickstart":{"code":"import g, { SG } from 'strong-globalize';\nimport path from 'path';\n\n// Set the root directory for message files (e.g., 'locales/en.json') and default language\n// In a real app, 'locales' might be in a dist folder or root.\nSG.SetRootDir(path.join(process.cwd(), 'locales'));\nSG.SetDefaultLanguage('en');\n\n// Manually add messages for demonstration. In a real app, these would typically be loaded\n// from JSON/YAML files located in the rootDir or added by the CLI 'extract' command.\nSG.addMessages('en', {\n  'app.greeting': 'Hello, {name}!',\n  'app.welcome': 'Welcome to strong-globalize. Today is {date}.',\n  'app.price': 'The price is {0, currency} at {1, number} units.'\n});\nSG.addMessages('fr', {\n  'app.greeting': 'Bonjour, {name}!',\n  'app.welcome': 'Bienvenue à strong-globalize. Aujourd\\'hui, nous sommes le {date}.',\n  'app.price': 'Le prix est de {0, currency} pour {1, number} unités.'\n});\n\n// Change language at runtime for the 'g' instance (e.g., based on user preference)\ng.setLanguage('en');\n\nconsole.log(g.f('app.greeting', { name: 'World' }));\nconsole.log(g.formatMessage('app.welcome', { date: new Date() }));\nconsole.log(g.c(123.45, 'USD')); // Defaults to US dollar format\nconsole.log(g.formatCurrency(543.21, 'EUR', { minimumFractionDigits: 2, maximumFractionDigits: 2 }));\n\n// Switch to French\ng.setLanguage('fr');\nconsole.log(g.f('app.greeting', { name: 'Monde' }));\nconsole.log(g.formatMessage('app.welcome', { date: new Date() }));\n\nconsole.log(g.d(new Date(), { skeleton: 'long' })); // Date formatting\nconsole.log(g.n(1234567.89, { maximumFractionDigits: 0 })); // Number formatting\n\n// Example using RFC 5424 Syslog severity wrappers\ng.error('A critical error occurred: {reason}', { reason: 'disk full' });\ng.warning('Potential issue: {count} warnings detected.', { count: 3 });\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade your Node.js environment to version 10.x or newer to ensure compatibility.","message":"As of strong-globalize v5.0.0, the package explicitly requires Node.js version 10 or higher. Running it on earlier Node.js versions (e.g., 8.x) will result in runtime errors and is not supported.","severity":"breaking","affected_versions":"<5.0.0"},{"fix":"Ensure that all necessary CLDR locale data is loaded for your supported languages. This often involves calling `Globalize.load()` from `jquery/globalize` or configuring `SG.SetRootDir` to point to a directory containing CLDR JSON data.","message":"strong-globalize relies on Unicode CLDR data for accurate internationalization. If locale data for a target language is missing or incorrectly loaded, formatting functions might produce incorrect output, fall back to default locales, or throw errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For applications needing dynamic language selection, either create new 'g' instances for each request/context or pass the desired locale explicitly to formatting methods, rather than relying solely on global state.","message":"There's a distinction between global language configuration via `SG.SetDefaultLanguage` and instance-specific language switching using `g.setLanguage`. `SG.SetDefaultLanguage` affects the global default, while `g.setLanguage` changes the locale for a particular 'g' instance. For multi-tenant or per-request localization in Node.js, manage 'g' instances carefully to avoid cross-request language contamination.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Verify that the message key (`app.greeting`) exists in your locale-specific message files (e.g., `en.json`, `en.yaml`) and that `SG.SetRootDir` is pointing to the correct directory. Also, ensure messages are added via `SG.addMessages` if managed programmatically.","cause":"The requested message key was not found in the loaded message catalog for the current locale, or the message file was not loaded correctly for that locale.","error":"Error: Message 'app.greeting' not found for locale 'en'."},{"fix":"Ensure that you are importing 'g' correctly (e.g., `import g from 'strong-globalize';` or `const g = require('strong-globalize');` in CommonJS) and that the library is fully initialized before attempting to call its methods.","cause":"The 'g' object was not correctly imported or initialized, or the `require` statement did not resolve to the expected globalization instance containing the formatting methods.","error":"TypeError: g.formatMessage is not a function"},{"fix":"Install and explicitly load the required CLDR data for all locales your application supports. This often involves using `Globalize.load(cldrData)` from `jquery/globalize` after retrieving the appropriate CLDR JSON files. Check the `strong-globalize` documentation for guidance on automatic CLDR data loading if configured with `SG.SetRootDir`.","cause":"The underlying `jquery/globalize` library, which `strong-globalize` builds upon, cannot find the necessary CLDR locale data for the specified language ('en').","error":"Locale data for 'en' is missing. Please ensure the locale data for 'en' is loaded."}],"ecosystem":"npm"}