Skip to content

Commit 558f5e0

Browse files
committed
Improve comments, fix lint
1 parent 6ffa79f commit 558f5e0

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

scripts/generateTranslations.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ class TranslationGenerator {
117117

118118
// If a compareRef is provided, fetch the old version of the files, and traverse the ASTs in parallel to extract existing translations
119119
if (this.compareRef) {
120-
// A map of locale => translations node, where "translations node" refers to the main object in en.ts and
121-
// other locale files that contains all the translations.
122120
const allLocales: Locale[] = [LOCALES.EN, ...this.targetLanguages];
121+
122+
// An array of labeled "translation nodes", where "translations node" refers to the main object in en.ts and
123+
// other locale files that contains all the translations.
123124
const oldTranslationNodes: Array<LabeledNode<Locale>> = [];
124125
const downloadPromises = [];
125126
for (const targetLanguage of allLocales) {
@@ -142,7 +143,7 @@ class TranslationGenerator {
142143
await Promise.all(downloadPromises);
143144

144145
// Traverse ASTs of all downloaded files in parallel, building a map of {locale => {translationKey => translation}}
145-
// Note: that traversing in parallel is not just a performance optimization. We need the translation key
146+
// Note: traversing in parallel is not just a performance optimization. We need the translation key
146147
// from en.ts to map to translations in other files, but we can't rely on dot-notation style paths alone
147148
// because sometimes there are strings defined elsewhere, such as in functions or nested templates.
148149
// So instead, we rely on the fact that the AST structure of en.ts will very nearly match the AST structure of other locales.
@@ -154,7 +155,9 @@ class TranslationGenerator {
154155
return;
155156
}
156157

158+
// Use English for the translation key
157159
const translationKey = this.getTranslationKey(enNode);
160+
158161
for (const targetLanguage of this.targetLanguages) {
159162
const translatedNode = nodes[targetLanguage];
160163
if (!this.shouldNodeBeTranslated(translatedNode)) {
@@ -172,6 +175,7 @@ class TranslationGenerator {
172175
translationsForLocale.set(translationKey, serializedNode);
173176
translations.set(targetLanguage, translationsForLocale);
174177

178+
// For complex template expressions, we need a way to look up the English span hash for each translated span hash, so we track those here
175179
if (ts.isTemplateExpression(enNode) && ts.isTemplateExpression(translatedNode) && !this.isSimpleTemplateExpression(enNode)) {
176180
for (let i = 0; i < enNode.templateSpans.length; i++) {
177181
const enSpan = enNode.templateSpans[i];
@@ -195,6 +199,7 @@ class TranslationGenerator {
195199
const translationPromises = [];
196200
for (const [key, {text, context}] of stringsToTranslate) {
197201
if (translationsForLocale.has(key)) {
202+
// This means that the translation for this key was already parsed from an existing translation file, so we don't need to translate it with ChatGPT
198203
continue;
199204
}
200205
const translationPromise = promisePool.add(() => this.translator.translate(targetLanguage, text, context).then((result) => translationsForLocale.set(key, result)));

tests/unit/generateTranslationsTest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ describe('generateTranslations', () => {
463463
greeting: 'Hello',
464464
unchanged: 'Unchanged',
465465
func: (name: string) => \`Hello \${name}\`,
466-
noSubstitutionTemplate: \`Helloooo\`,
466+
noSubstitutionTemplate: \`Salutations\`,
467467
complexFunc: (numScanning: number, numPending: number) => {
468468
const statusText: string[] = [];
469469
if (numScanning > 0) {
@@ -488,7 +488,7 @@ describe('generateTranslations', () => {
488488
greeting: '[it] Hello',
489489
unchanged: '[it] Unchanged',
490490
func: (name: string) => \`[it] Hello \${name}\`,
491-
noSubstitutionTemplate: \`[it] Helloooo\`,
491+
noSubstitutionTemplate: \`[it] Salutations\`,
492492
complexFunc: (numScanning: number, numPending: number) => {
493493
const statusText: string[] = [];
494494
if (numScanning > 0) {
@@ -525,7 +525,7 @@ describe('generateTranslations', () => {
525525
greeting: 'Hello',
526526
unchanged: 'Unchanged',
527527
func: (name: string) => \`Hello \${name}\`,
528-
noSubstitutionTemplate: \`Helloooo\`,
528+
noSubstitutionTemplate: \`Salutations\`,
529529
complexFunc: (numScanning: number, numPending: number) => {
530530
const statusText: string[] = [];
531531
if (numScanning > 0) {
@@ -554,7 +554,7 @@ describe('generateTranslations', () => {
554554
expect(itContent).toContain('[it] Unchanged');
555555
// eslint-disable-next-line no-template-curly-in-string
556556
expect(itContent).toContain('[it] Hello ${name}');
557-
expect(itContent).toContain('[it] Helloooo');
557+
expect(itContent).toContain('[it] Salutations');
558558
expect(itContent).toContain('[it] New value!');
559559
expect(translateSpy).toHaveBeenCalledTimes(1);
560560
expect(translateSpy).toHaveBeenCalledWith('it', 'New value!', undefined);

0 commit comments

Comments
 (0)