Gettext Extractor Vue Support
gettext-extractor-vue is a specialized extension for the `gettext-extractor` library, designed to facilitate the extraction of gettext messages from Vue Single File Components (SFCs). It works by decorating `gettext-extractor`'s `JSExtractor` to preprocess `.vue` files, transforming their template and script sections into standard JavaScript that the core `gettext-extractor` can then parse. The current stable version is 5.2.0. This package differentiates itself by offering compatibility with both Vue 2 applications (requiring `vue-template-compiler` as a user-provided dependency) and Vue 3 applications (requiring `@vue/compiler-sfc`). Additionally, it provides utility functions, such as `decorateExtractorWithHelpers`, to add message transformation capabilities to the core `GettextExtractor` instance. While its release cadence isn't strictly defined, it generally aligns with major versions of `gettext-extractor` and Vue itself to ensure ongoing compatibility.
Common errors
-
Error: Cannot find module 'vue-template-compiler' or 'Error: Cannot find module '@vue/compiler-sfc''
cause The required Vue compiler dependency (either for Vue 2 or Vue 3) was not installed in your project.fixInstall the appropriate Vue compiler: `npm install vue-template-compiler` for Vue 2, or `npm install @vue/compiler-sfc` for Vue 3. -
TypeError: extractor.createJsParser is not a function
cause The `gettext-extractor` package is either not installed, or an incompatible version is installed, preventing access to its core methods.fixEnsure `gettext-extractor` is correctly installed and its version satisfies the peer dependency requirement: `npm install gettext-extractor`.
Warnings
- gotcha This package is an extension for `gettext-extractor` and requires it as a peer dependency. Ensure `gettext-extractor` is installed in your project at a compatible version.
- gotcha To parse Vue Single File Components, you must explicitly provide the correct Vue compiler: either `vue-template-compiler` for Vue 2 or `@vue/compiler-sfc` for Vue 3. These are peer dependencies of `gettext-extractor-vue` and must be installed by your project.
- gotcha While the README examples primarily use CommonJS `require()`, the package generally supports both CommonJS and ES Modules. Be consistent with your project's module system. If using ES Modules, use `import` statements.
Install
-
npm install gettext-extractor-vue -
yarn add gettext-extractor-vue -
pnpm add gettext-extractor-vue
Imports
- decorateJSParserWithVueSupport
const { decorateJSParserWithVueSupport } = require('gettext-extractor-vue');import { decorateJSParserWithVueSupport } from 'gettext-extractor-vue'; - decorateExtractorWithHelpers
const { decorateExtractorWithHelpers } = require('gettext-extractor-vue');import { decorateExtractorWithHelpers } from 'gettext-extractor-vue'; - * as gettextVue
import gettextVue from 'gettext-extractor-vue';
import * as gettextVue from 'gettext-extractor-vue';
Quickstart
const { GettextExtractor, JsExtractors } = require('gettext-extractor');
const { decorateJSParserWithVueSupport } = require('gettext-extractor-vue');
const extractor = new GettextExtractor();
const jsParser = extractor.createJsParser([
JsExtractors.callExpression('getText', {
arguments: {
text: 0,
context: 1,
},
}),
JsExtractors.callExpression('getPlural', {
arguments: {
text: 1,
textPlural: 2,
context: 3,
},
}),
]);
// For vue@2 support, ensure vue-template-compiler is installed
let vueParser = decorateJSParserWithVueSupport(jsParser, {
vue2TemplateCompiler: require('vue-template-compiler'),
});
// For vue@3 support, ensure @vue/compiler-sfc is installed
// vueParser = decorateJSParserWithVueSupport(jsParser, {
// vue3TemplateCompiler: require('@vue/compiler-sfc'),
// });
vueParser.parseFilesGlob('./src/**/*.@(js|vue)');
extractor.savePotFile('./messages.pot');
extractor.printStats();