i18next-scanner TypeScript Transform
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
-
TypeError: (0 , i18next_scanner_typescript_1.default) is not a function
cause Attempting to import `i18next-scanner-typescript` using an ES module `import` syntax in a CommonJS context (e.g., a `.js` config file). The package exports a CommonJS module with a default function.fixUse CommonJS `require` syntax: `const typescriptTransform = require('i18next-scanner-typescript');` -
SyntaxError: 'import' and 'export' may only appear with 'sourceType: module'
cause i18next-scanner is attempting to parse a TypeScript file directly because the `typescriptTransform` is either not configured correctly or the `.ts`/`.tsx` extensions are mistakenly included in the main `options.func.extensions` or `options.trans.extensions`.fixVerify that `typescriptTransform` is correctly added to the `transform` array in your `i18next-scanner` configuration, and crucially, ensure that `.ts` and `.tsx` are NOT listed in the `options.func.extensions` or `options.trans.extensions`.
Warnings
- gotcha Do NOT include `.ts` or `.tsx` file extensions in the `options.func.extensions` or `options.trans.extensions` arrays in your `i18next-scanner` configuration. These options are for `i18next-scanner` to process already-JavaScript files. The `typescriptTransform` handles transpilation of TS/TSX independently.
- breaking The `i18next-scanner-typescript` package specifies a peer dependency on `typescript` version `5.x`. Using an incompatible version of TypeScript (e.g., 4.x or 6.x) may lead to transpilation errors or unexpected behavior.
Install
-
npm install i18next-scanner-typescript -
yarn add i18next-scanner-typescript -
pnpm add i18next-scanner-typescript
Imports
- typescriptTransform
import typescriptTransform from 'i18next-scanner-typescript';
const typescriptTransform = require('i18next-scanner-typescript');
Quickstart
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();
}
),
};