A Node.js CLI utility that extracts display text from Angular applications and generates JSON files for internationalization.
- 🔍 Extracts text from Angular HTML templates
- 📝 Extracts string literals from TypeScript component files
- 🌍 Generates JSON translation files
- 🔄 Optionally replaces extracted text with i18n pipe placeholders
- ⚙️ Configurable key prefixes and output paths
npm install -g angular-i18n-extractorOr run directly with npx:
npx angular-i18n-extractor extractng-i18n-extract extractng-i18n-extract extract \
--src ./src/app \
--output ./i18n/en.json \
--locale en \
--key-prefix myapp \
--replace| Option | Short | Description | Default |
|---|---|---|---|
--src |
-s |
Source directory path | ./src |
--output |
-o |
Output JSON file path | ./i18n/messages.json |
--locale |
-l |
Locale code for extraction | en |
--key-prefix |
-k |
Prefix for generated keys | app |
--replace |
-r |
Replace text with i18n placeholders | false |
- Text content within HTML elements
- Attribute values:
title,alt,placeholder,aria-label
- String literals that appear to be user-facing text
- Excludes technical strings like URLs, paths, and identifiers
The generated JSON file contains:
{
"locale": "en",
"translations": {
"app.welcome_message_1": "Welcome to our application",
"app.submit_button_2": "Submit",
"app.error_message_3": "An error occurred"
},
"metadata": {
"extractedAt": "2024-01-01T12:00:00.000Z",
"totalTexts": 3,
"keyPrefix": "app"
}
}When using the --replace option, the tool will:
Replace text content with Angular i18n pipe:
<!-- Before -->
<h1>Welcome</h1>
<button title="Click me">Submit</button>
<!-- After -->
<h1>{{ 'app.welcome_1' | translate }}</h1>
<button title="{{ 'app.click_me_2' | translate }}">{{ 'app.submit_3' | translate }}</button>Replace string literals with translation service calls:
// Before
this.message = "Hello World";
// After
this.message = this.translate.get('app.hello_world_1');ng-i18n-extract extract --src ./src --output ./i18n/en.jsonng-i18n-extract extract --src ./src --output ./i18n/en.json --key-prefix myapp --replaceng-i18n-extract extract --locale es --output ./i18n/es.json- Node.js >= 16.0.0
- Angular project structure
MIT