Angular i18n Extractor Development
This skill provides guidelines and commands for working with the Angular Translation Extractor VS Code extension.
Environment Setup
- •
Install Dependencies:
bashnpm install
- •
Compile the Extension:
bashnpm run compile
or watch mode:
bashnpm run watch
Debugging
To debug the extension:
- •Open the project in VS Code.
- •Press
F5to launch the "Extension Development Host". - •In the host window, open an Angular project workspace.
- •Run the command
Angular: Extract translations.
Key Components
- •
src/extractJsTs.ts: Handles parsing of JS/TS files using Babel. Only extracts inline HTML templates from@Componentdecorators (thetemplateproperty). Does NOT extract from component methods or properties, and does NOT extract from other classes. Skips all decorator properties excepttemplate. - •
src/extractHtml.ts: Handles parsing of HTML files and inline templates using Regex. Extracts text content, attributes, and interpolations. Skips pipe format arguments (e.g.,date:'short',currency:'USD') to preserve Angular pipe configurations. - •
src/replaceSource.ts: Handles the replacement of extracted strings with translation keys in the source files. - •
src/generate.ts: Generates the JSON translation files.
Common Issues & Fixes
1. Strings not being extracted
- •Check
minStringLengthsetting. - •Verify
ignoreGlobsto ensure file is not excluded. - •Verify the class has
@Componentdecorator: Only strings in@Componentclasses are extracted from inline templates. - •Check template property: Only the
templateproperty of@Componentis scanned. Other properties likeselector,providers,stylesare ignored (framework-critical). - •Check
isProbablyUserFacingregex inextractHtml.tsfor false negatives.
2. Pipe format strings being extracted
- •The extension automatically skips pipe format arguments like
'short'indate:'short'. - •This prevents breaking Angular pipes (date, currency, number, time, etc.).
- •Pipes already with
| translateare skipped entirely.
3. @Component decorator properties being extracted
- •The extension does NOT extract from decorator properties like
selector: 'app-root'. - •Only the
templateproperty content is scanned. - •This preserves Angular framework functionality.
4. Replacements are incorrect or missing
- •Validation: The extension now uses
rawText(exact string from file) to validate that the content at the calculated position matches exactly before replacing. - •HTML/Templates: Ensure
extractHtml.tscalculates precise source locations and includesrawText. - •Troubleshooting: Check the "Angular Translation Extractor" output channel for "Mismatch" warnings. This indicates the calculated file position does not match the expected content.
Testing
- •Run
npm run compileafter code changes to verify build/type-check integration. - •Use
npm run lintto catch static issues insrc/. - •Use
npm testfor extension integration checks when needed. - •Create a reproduction script (e.g.,
src/reproduce_issue.ts) to isolate extraction/replacement logic without launching the full VS Code host.
Translation generation modes
The translation process uses three update modes to generate language-specific JSON files. These modes control how translated content is merged with existing translations.
Update Modes (updateMode configuration)
1. Merge Mode (default)
- •Only translates new properties that don't exist in the target language file yet
- •Only translates blank properties (properties with empty or whitespace-only values)
- •Preserves all existing translations (non-empty values)
- •Safe mode for incremental updates without losing manually fixed translations
- •Filter logic:
isForce || !translations[key] || (typeof translations[key] === 'string' && translations[key].trim() === '')
2. Overwrite Mode
- •Translates all non-default language files (any language except the base language)
- •Preserves the base language file as-is
- •Useful when you need to regenerate translations while keeping the base language intact
- •Does not overwrite completely new base language entries
3. Recreate Mode
- •Completely regenerates all language files from the base language
- •Erases all existing translations
- •Starts fresh with AI-generated translations for all properties
- •Use with caution as it will lose all manual translation corrections
Translation Script Behavior
The generated translation scripts (translate-google.cjs, translate-libretranslate.cjs) now correctly implement these modes:
- •
CLI Flags Available:
- •
--fast: Removes delays between API requests (speeds up translation) - •
--parallel: Enables parallel translation (up to 5 languages at once) - •
--diff: Shows preview of what would be translated without writing files - •
--force: Forces translation of all properties regardless of mode
- •
- •
Keys to Translate Filter:
javascript// Only translate keys that match these conditions: const keysToTranslate = Object.entries( content, ).filter( ([ key, value, ]) => isForce || // Force mode translates everything !translations[ key ] || // New properties (don't exist yet) (typeof translations[ key ] === 'string' && // OR blank existing properties translations[ key ].trim() === ''), ); - •
Translation Preservation: In merge mode, the script only modifies keys that meet the filter criteria, leaving all other existing translations untouched
Best Practices
- •For incremental updates: Use merge mode (default) to add translations for new properties while preserving manual corrections
- •For fixing broken base language: Update the base language JSON file, then use merge mode to translate new/blank properties only
- •For complete regeneration: Use recreate mode, but backup existing translations first
- •For testing: Use
--diffflag to preview what would be translated before applying changes - •For large translation batches: Use
--paralleland--fastflags for better performance