{"id":25624,"library":"gettextjs","title":"gettext.js","description":"Browser-side GNU gettext implementation for internationalization (i18n). Current stable version is 2.1.1. Provides a `gettext`-like interface in the browser, compiles MO files to JavaScript, includes a webpack loader for MO files, and supports plural forms with `ngettext`. Key differentiators: lightweight, browser-focused, integrates with webpack, and offers both runtime API and pre-compilation. Compared to i18next or Polyglot, gettext.js is closer to the GNU gettext workflow and simpler for projects already using MO files. Release cadence: occasional maintenance updates, as last release was several years ago.","status":"maintenance","version":"2.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/ojii/gettext.js","tags":["javascript"],"install":[{"cmd":"npm install gettextjs","lang":"bash","label":"npm"},{"cmd":"yarn add gettextjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add gettextjs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"gettext is a named export, not default.","wrong":"import gettext from 'gettextjs'","symbol":"gettext (as _)","correct":"import { gettext as _ } from 'gettextjs'"},{"note":"Works with CJS, but preferred import is ESM as shown. set_catalog is required before using gettext/ngettext.","wrong":"const { set_catalog } = require('gettextjs')","symbol":"set_catalog","correct":"import { set_catalog } from 'gettextjs'"},{"note":"It's fine to alias, but the symbol name is ngettext.","wrong":"import { ngettext as _n } from 'gettextjs'","symbol":"ngettext","correct":"import { ngettext } from 'gettextjs'"}],"quickstart":{"code":"import { gettext as _, ngettext, set_catalog } from 'gettextjs';\nimport fs from 'fs';\n\n// Load a .mo file as bytes (e.g., from fs) and parse it\nconst moBuffer = fs.readFileSync('messages.mo');\nconst catalog = { locales: ['en'], messages: {} }; // simplified; real usage uses mo-parse\n// For simplicity, set a dummy catalog\nset_catalog({\n  'en': {\n    'hello world': 'hello world',\n    'bug': ['bug', 'bugs']\n  }\n});\n\nconsole.log(_('hello world')); // 'hello world'\nconsole.log(ngettext('bug', 'bugs', 4)); // 'bugs' (since count > 1)","lang":"typescript","description":"Shows basic setup: import named exports, set_catalog with translations, and use gettext/ngettext."},"warnings":[{"fix":"Always call set_catalog with a valid catalog object (locale -> { message: translation }) at initialization.","message":"set_catalog must be called before any gettext/ngettext calls, otherwise they return the untranslated string.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use the webpack loader or include a precompiled catalog via set_catalog.","message":"The `compiler` module and the CLI tool `gettextjs compile` are deprecated in favor of webpack loader or manual parsing.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Use the provided webpack loader for `.mo` files, or compile the `.mo` to a JS file using the CLI (`gettextjs compile input.mo output.js`) and then import that file.","message":"In browser, `.mo` files cannot be read directly; you must use a loader (webpack) or convert to JSON/catalog object via the runtime.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use named imports: `import { gettext as _, ngettext, set_catalog } from 'gettextjs'`.","message":"Version 2.0.0 changed imports from default to named exports. `import gettext from 'gettextjs'` no longer works.","severity":"breaking","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Change `import gettext from 'gettextjs'` to `import { gettext as _ } from 'gettextjs'`.","cause":"Using default import instead of named import after v2.0.0.","error":"TypeError: gettext is not a function"},{"fix":"Run `npm install gettextjs` and ensure the import path is correct.","cause":"Package not installed or path mistake.","error":"Module not found: Can't resolve 'gettextjs' in '...'"},{"fix":"Call `set_catalog({ 'en': { 'key': 'value' } })` with a valid catalog object.","cause":"Called set_catalog without arguments or with wrong type.","error":"Error: set_catalog must be called with an object"},{"fix":"Ensure the catalog includes the exact string key and that the locale matches. Use `set_catalog({ locale: { msgid: msgstr } })`.","cause":"Catalog might not have the correct locale or the string key is missing.","error":"Calls to gettext return original string even though catalog is set"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}