Gettext Extractor Vue Support

5.2.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `gettext-extractor` with `gettext-extractor-vue` to parse both JavaScript and Vue Single File Components (SFCs), saving extracted messages to a `.pot` file. It shows how to integrate `decorateJSParserWithVueSupport` and highlights the conditional requirement for `vue-template-compiler` (for Vue 2) or `@vue/compiler-sfc` (for Vue 3).

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();

view raw JSON →