Skip to content

Commit c103222

Browse files
committed
Add core logic for paths inputs
1 parent d05ad75 commit c103222

1 file changed

Lines changed: 62 additions & 4 deletions

File tree

scripts/generateTranslations.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ class TranslationGenerator {
213213
const translationsForLocale = translations.get(targetLanguage) ?? new Map<number, string>();
214214

215215
// Extract strings to translate
216-
// TODO: filter stringsToTranslate based on paths
217216
const stringsToTranslate = new Map<number, StringWithContext>();
218217
this.extractStringsToTranslate(this.sourceFile, stringsToTranslate);
219218

@@ -287,6 +286,11 @@ class TranslationGenerator {
287286
return false;
288287
}
289288

289+
// Don't translate property keys (the name part of property assignments)
290+
if (node.parent && ts.isPropertyAssignment(node.parent) && node.parent.name === node) {
291+
return false;
292+
}
293+
290294
// Don't translate any strings or expressions that affect code execution by being part of control flow.
291295
// We want to translate only strings that are "leaves" or "results" of any expression or code block
292296
const isPartOfControlFlow =
@@ -424,14 +428,47 @@ class TranslationGenerator {
424428
return hashStr(keyBase);
425429
}
426430

431+
/**
432+
* Check if a given translation path should be translated based on the paths filter.
433+
* If no paths are specified, all paths should be translated.
434+
* If paths are specified, only paths that match exactly or are nested under a specified path should be translated.
435+
*/
436+
private shouldTranslatePath(currentPath: string): boolean {
437+
if (!this.paths || this.paths.length === 0) {
438+
return true;
439+
}
440+
441+
for (const targetPath of this.paths) {
442+
// Exact match
443+
if (currentPath === targetPath) {
444+
return true;
445+
}
446+
// Current path is nested under target path
447+
if (currentPath.startsWith(`${targetPath}.`)) {
448+
return true;
449+
}
450+
// Target path is nested under current path (for parent path matching)
451+
if (targetPath.startsWith(`${currentPath}.`)) {
452+
return true;
453+
}
454+
}
455+
456+
return false;
457+
}
458+
427459
/**
428460
* Recursively extract all string literals and templates to translate from the subtree rooted at the given node.
429461
* Simple templates (as defined by this.isSimpleTemplateExpression) can be translated directly.
430462
* Complex templates must have each of their spans recursively translated first, so we'll extract all the lowest-level strings to translate.
431463
* Then complex templates will be serialized with a hash of complex spans in place of the span text, and we'll translate that.
432464
*/
433-
private extractStringsToTranslate(node: ts.Node, stringsToTranslate: Map<number, StringWithContext>) {
465+
private extractStringsToTranslate(node: ts.Node, stringsToTranslate: Map<number, StringWithContext>, currentPath = '') {
434466
if (this.shouldNodeBeTranslated(node)) {
467+
// Check if this translation path should be included based on the paths filter
468+
if (!this.shouldTranslatePath(currentPath)) {
469+
return; // Skip this node and its children if the path doesn't match
470+
}
471+
435472
const context = this.getContextForNode(node);
436473
const translationKey = this.getTranslationKey(node);
437474

@@ -448,12 +485,33 @@ class TranslationGenerator {
448485
if (this.verbose) {
449486
console.debug('😵‍💫 Encountered complex template, recursively translating its spans first:', node.getText());
450487
}
451-
node.templateSpans.forEach((span) => this.extractStringsToTranslate(span, stringsToTranslate));
488+
node.templateSpans.forEach((span) => this.extractStringsToTranslate(span, stringsToTranslate, currentPath));
452489
stringsToTranslate.set(translationKey, {text: this.templateExpressionToString(node), context});
453490
}
454491
}
455492
}
456-
node.forEachChild((child) => this.extractStringsToTranslate(child, stringsToTranslate));
493+
494+
// Continue traversing children
495+
node.forEachChild((child) => {
496+
let childPath = currentPath;
497+
498+
// If the child is a property assignment, update the path
499+
if (ts.isPropertyAssignment(child)) {
500+
let propName: string | undefined;
501+
502+
if (ts.isIdentifier(child.name)) {
503+
propName = child.name.text;
504+
} else if (ts.isStringLiteral(child.name)) {
505+
propName = child.name.text;
506+
}
507+
508+
if (propName) {
509+
childPath = currentPath ? `${currentPath}.${propName}` : propName;
510+
}
511+
}
512+
513+
this.extractStringsToTranslate(child, stringsToTranslate, childPath);
514+
});
457515
}
458516

459517
/**

0 commit comments

Comments
 (0)