{"id":18994,"library":"babel-gettext-extractor","title":"babel-gettext-extractor","description":"Babel plugin to extract gettext strings from JavaScript/JSX code, supporting ES6+ and JSX syntax. Current stable version is 4.1.3, maintained by Sentry. It is a fork of babel-gettext-plugin with added support for references and earlier Node.js versions. Works with Babel 7. Key differentiators: flexible configuration of function names, custom output file naming via a callback, base directory for relative paths, and optional whitespace stripping from template literals. Alternatives like babel-plugin-react-intl focus on React Intl, while this plugin targets the standard gettext workflow.","status":"active","version":"4.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/getsentry/babel-gettext-extractor","tags":["javascript","babel","gettext","plugin","i18n"],"install":[{"cmd":"npm install babel-gettext-extractor","lang":"bash","label":"npm"},{"cmd":"yarn add babel-gettext-extractor","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-gettext-extractor","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a Babel plugin, not a standalone module. It's used via Babel's plugins array, not imported directly in application code.","wrong":"import babelGettextExtractor from 'babel-gettext-extractor'","symbol":"plugin","correct":"module.exports = { plugins: ['babel-gettext-extractor'] }"},{"note":"Options are passed as an array entry [plugin, options], not as separate arguments.","wrong":"module.exports = { plugins: ['babel-gettext-extractor', { headers: {} }] }","symbol":"plugin with options","correct":"module.exports = { plugins: [['babel-gettext-extractor', { headers: { 'content-type': 'text/plain; charset=UTF-8' } }]] }"},{"note":"The plugin is a string reference; requiring it directly and using the object is incorrect because Babel resolves it by name from node_modules.","wrong":"const babelGettextExtractor = require('babel-gettext-extractor'); babel.transform(code, { plugins: [babelGettextExtractor] })","symbol":"require in code","correct":"const babel = require('babel'); babel.transform(code, { plugins: ['babel-gettext-extractor'] })"}],"quickstart":{"code":"// .babelrc or babel.config.js\nmodule.exports = {\n  plugins: [\n    ['babel-gettext-extractor', {\n      headers: {\n        'content-type': 'text/plain; charset=UTF-8',\n        'plural-forms': 'nplurals=2; plural=(n!=1);'\n      },\n      functionNames: {\n        _t: ['msgid'],\n        _n: ['msgid', 'msgid_plural'],\n        _p: ['msgctxt', 'msgid']\n      },\n      fileName: './messages.pot',\n      baseDirectory: './src',\n      stripTemplateLiteralIndent: false\n    }]\n  ]\n};\n\n// Run: babel --plugins babel-gettext-extractor src/ -d lib/","lang":"javascript","description":"Configures babel-gettext-extractor with custom function names and headers, outputs a .pot file."},"warnings":[{"fix":"Replace 'babel-gettext-plugin' with 'babel-gettext-extractor' in your Babel config.","message":"Plugin name changed from 'babel-gettext-plugin' to 'babel-gettext-extractor' in v4.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Replace require('babel') with require('@babel/core') and update config accordingly.","message":"The 'babel' package is deprecated; use '@babel/core' with Babel 7.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Return false from the fileName function to prevent output for that file.","message":"fileName option can be a function returning false to skip extraction per file.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Ensure your gettext runtime also strips leading indent when using this option.","message":"If stripTemplateLiteralIndent is true, gettext implementations must also strip leading indent; otherwise translations may have extra whitespace.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Use fileName function or Babel's ignore/include patterns to control which files are processed.","message":"baseDirectory only affects file references in the .pot output; it does not filter which files are processed.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install --save-dev babel-gettext-extractor' and verify the plugin string is correct.","cause":"Plugin not installed or misspelled in Babel config.","error":"Error: Cannot find module 'babel-gettext-extractor'"},{"fix":"Check the option names: valid ones are 'headers', 'functionNames', 'fileName', 'baseDirectory', 'stripTemplateLiteralIndent'.","cause":"Invalid option name in the plugin configuration object.","error":"ReferenceError: [BABEL] unknown: Unknown option: .plugins[0][1].foo"},{"fix":"Ensure you are using Babel 7 and pass the file object correctly; check the example in the README.","cause":"fileName function receives an object without the expected property, possibly due to Babel version mismatch.","error":"TypeError: Cannot read property 'sourceFileName' of undefined"},{"fix":"Ensure your source files contain calls to configured functionNames with string literals. Check console logs for skipped files. Confirm fileName path is absolute or relative to the working directory.","cause":"Either no translatable strings found, fileName returns false, or output path is incorrect (e.g., relative path resolves to a non-existent directory).","error":"No output .pot file generated"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}