nextintl-injector
v1.0.0
Published
A static text extractor and injector for Next.js + next-intl.
Maintainers
Readme
nextintl-injector
A static text extractor and injector for Next.js + next-intl projects.
nextintl-injector scans your JSX/TSX files, extracts static strings into locale messages, then replaces those strings with translation calls.
Install
pnpm add -D nextintl-injector
# or
npm i -D nextintl-injector
# or
yarn add -D nextintl-injectorQuick Start
- Create a config file at your project root:
nextintl-injector.config.ts - Generate extracted messages:
npx nextintl-injector generate - Apply translations in source files:
npx nextintl-injector apply
CLI
generate
Scans your project and writes extracted texts to the default locale JSON file.
npx nextintl-injector generateapply
Replaces static JSX text/attributes with translation calls and injects the needed next-intl imports/hooks.
npx nextintl-injector applyDry run mode (no file changes):
npx nextintl-injector apply --dryConfiguration
Create nextintl-injector.config.ts at your project root:
import type { I18nInjectorConfig } from 'nextintl-injector';
const config: I18nInjectorConfig = {
projectRootDir: process.cwd(),
defaultLocale: 'en',
i18nMessagesFolderName: 'messages',
validExtensions: ['.tsx', '.jsx'],
ignoreDirs: ['node_modules', '.next', 'dist'],
ignoreWords: ['OK', 'ID'],
jsxAttributes: ['title', 'placeholder', 'aria-label'],
rulesToSkip: [],
};
export default config;rulesToSkip values
short-uppercasenumbersdigitsdate-formatsemailsurlsno-letterspure-symbols
Example Flow
Before
export default function Page() {
return (
<div>
<h1>Hello World</h1>
<input placeholder='Your email' />
</div>
);
}After apply
import { useTranslations } from 'next-intl';
export default function Page() {
const t_page = useTranslations('page');
return (
<div>
<h1>{t_page('hello_world')}</h1>
<input placeholder={t_page('your_email')} />
</div>
);
}Notes
generatemust run beforeapply.- The tool supports
.tsx/.jsx(or whatever you configure invalidExtensions). - Extracted output is written to your configured default locale (example:
messages/en.json).
Development
pnpm build
pnpm dev
pnpm formatLicense
ISC
