i18next-scanner TypeScript Transform

1.2.1 · active · verified Sun Apr 19

i18next-scanner-typescript provides essential TypeScript support for the i18next-scanner tool, enabling it to extract internationalization keys from `.ts` and `.tsx` source files. As of its current stable version `1.2.1`, this package acts as a transform layer, transpiling TypeScript code into JavaScript internally before `i18next-scanner` processes it. This ensures that the scanner can correctly identify `t()` calls and `<Trans>` components in modern TypeScript applications without requiring a separate pre-build step. It is an active part of the `i18next` ecosystem, maintained to ensure compatibility with both `i18next-scanner` and contemporary TypeScript versions, currently requiring `typescript@5.x` as a peer dependency. Its primary differentiator is simplifying the integration of i18n scanning into TypeScript-based projects by handling the necessary transpilation transparently.

Common errors

Warnings

Install

Imports

Quickstart

This configuration sets up `i18next-scanner` to process TypeScript and TSX files using the `i18next-scanner-typescript` transform. It demonstrates how to specify the TS/TSX file extensions for the transform while correctly excluding them from the scanner's primary `func` and `trans` options. The custom transform function provides access to `i18next-scanner`'s internal parser to process the transpiled output. To run this, first install `i18next-scanner`, `i18next-scanner-typescript`, and `typescript` as dev dependencies. Create an example `src/App.tsx` file with i18n keys (e.g., `<Trans>Hello</Trans>` or `t('key')`). Then, execute `npx i18next-scanner --config i18next-scanner.config.js` to extract keys.

const typescriptTransform = require('i18next-scanner-typescript');
const path = require('path');

module.exports = {
  input: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.spec.{js,jsx,ts,tsx}'],
  output: path.join(__dirname, 'public/locales/{{lng}}/translation.json'),
  options: {
    debug: true,
    sort: true,
    lngs: ['en', 'fr'],
    defaultValue: (lng, namespace, key) => key,
    resource: {
      loadPath: path.join(__dirname, 'public/locales/{{lng}}/{{ns}}.json'),
      savePath: path.join(__dirname, 'public/locales/{{lng}}/{{ns}}.json'),
      jsonIndent: 2,
    },
    func: {
      extensions: ['.js', '.jsx'], // Important: Only list JS/JSX extensions here
      list: ['i18next.t', 't'],
    },
    trans: {
      extensions: ['.js', '.jsx'], // Important: Only list JS/JSX extensions here
      component: 'Trans',
    },
  },
  transform: typescriptTransform(
    {
      extensions: ['.ts', '.tsx'], // List TS/TSX extensions for the transform itself
      tsOptions: {
        target: 'es2017',
        jsx: 'react',
      },
    },
    function customTransform(outputText, file, enc, done) {
      // `this` context here is the i18next-scanner parser instance
      // Access parsing functions via `this.parseFuncFromString`, etc.
      this.parser.parseTransFromString(outputText);
      this.parser.parseFuncFromString(outputText);
      done();
    }
  ),
};

view raw JSON →