{"id":18995,"library":"babel-gettext-plugin","title":"babel-gettext-plugin","description":"A Babel plugin (v3.0.0) that extracts gettext translation strings (msgid, msgctxt, etc.) from JavaScript files, supporting JSX, ES6, and other syntax via Babel. Built on node-gettext. It replaces older extractors like xgettext which lack modern syntax support. Maintenance is sparse; last release 2019.","status":"maintenance","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/jruchaud/babel-gettext-plugin","tags":["javascript","babel","gettext","plugin","i18n"],"install":[{"cmd":"npm install babel-gettext-plugin","lang":"bash","label":"npm"},{"cmd":"yarn add babel-gettext-plugin","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-gettext-plugin","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Requires Babel 7 to transform code and apply the plugin","package":"babel","optional":false},{"reason":"Core library used for gettext parsing and .po file generation","package":"node-gettext","optional":false}],"imports":[{"note":"Plugin is used via Babel's transform API; not imported directly. Must be required in CommonJS context.","wrong":"import babel from 'babel'; // ESM import not supported; Babel 7's API is CJS","symbol":"babel","correct":"const babel = require('babel'); babel.transform(code, { plugins: ['babel-gettext-plugin'] })"},{"note":"Options must be passed as the second element of an array, not as separate arguments.","wrong":"plugins: ['babel-gettext-plugin', { headers: {} }] // wrong: array nesting","symbol":"plugin config","correct":"plugins: [['babel-gettext-plugin', { headers: {}, functionNames: {} }]]"},{"note":"Use --plugins, not --plugin. Options can be appended with colon, e.g. --plugins babel-gettext-plugin:{\"fileName\":\"test.po\"}","wrong":"babel --plugin babel-gettext-plugin code.js // wrong: '--plugin' should be '--plugins'","symbol":"CLI usage","correct":"babel --plugins babel-gettext-plugin input.js -o output.js"}],"quickstart":{"code":"const babel = require('@babel/core');\nconst code = `\nconst _ = require('gettext');\nconst msg = _('Hello world');\nconsole.log(_('Goodbye', 'context'));\n`;\n\nbabel.transform(code, {\n  plugins: [\n    ['babel-gettext-plugin', {\n      headers: {\n        'content-type': 'text/plain; charset=UTF-8',\n        'plural-forms': 'nplurals=2; plural=(n!=1);'\n      },\n      functionNames: {\n        mygettext: ['msgid'],\n        myngettext: ['msgid', 'msgid_plural', 'count']\n      },\n      fileName: './locales/messages.pot',\n      defaultTranslate: false\n    }]\n  ]\n}).then(result => {\n  console.log('Extraction complete, check .pot file.');\n}).catch(err => console.error(err));","lang":"javascript","description":"Demonstrates plugin setup with Babel 7, custom function names for extraction, and .pot file output."},"warnings":[{"fix":"Install babel-gettext-plugin@2.x for Babel 6 projects.","message":"Plugin requires Babel 7; incompatible with Babel 6 or older. Use v2.x for Babel 6.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Consider alternatives like 'babel-plugin-gettext-extractor' or manual extraction with xgettext.","message":"Last release in 2019; no updates for newer Babel versions (8.x) or modern ECMAScript features.","severity":"deprecated","affected_versions":"<3.0.0"},{"fix":"Ensure all your translatable strings are string literals, e.g., _('Hello') not _(textVar).","message":"Plugin does not support parsing dynamic strings or template literals; only string literals extracted.","severity":"gotcha","affected_versions":"*"},{"fix":"Always specify headers with 'content-type' charset to avoid encoding issues.","message":"Output encoding: Generated .po/.pot files may have incorrect charset if headers not provided; default is UTF-8.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use '@babel/core' instead of 'babel': npm install @babel/core and const babel = require('@babel/core');","cause":"Incorrect Babel package: babel 6 vs @babel/core 7 naming.","error":"Error: Cannot find module 'babel'"},{"fix":"Install v2.x: npm install babel-gettext-plugin@2.x --save-dev","cause":"Plugin used with Babel 6 but installed v3.0.0 (requires Babel 7).","error":"TypeError: Cannot read property 'traverse' of undefined"},{"fix":"Define functionNames matching your gettext wrapper names, e.g., 'mygettext': ['msgid']","cause":"Plugin cannot find gettext call syntax (default function name '_'). No custom functionNames set.","error":"Warning: No strings extracted."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}