Files
calendar/packages/features/translation/services/TranslationService.ts
T
Udit TakkarGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>Udit TakkarUdit Takkar
4081d11fbe feat: workflow auto translation (#27087)
* feat: workflow auto translation

* tests: add unit tests

* refactor: tests and workflow

* fix: type err

* fix: type err

* fix: remove redundant index on WorkflowStepTranslation

The @@index on [workflowStepId, field, targetLocale] duplicates the @@unique
constraint on the same columns. A unique index already provides efficient
lookups, so the separate @@index adds storage overhead and write latency
without benefit.

Addresses Cubic AI review feedback (confidence 9/10).

Co-Authored-By: unknown <>

* fix: correct locale mapping when translation API returns null

Map translations with their corresponding locales before filtering to
preserve correct locale-to-translation associations. Previously, filtering
out null translations would reindex the array, causing incorrect locale
mappings when any translation in the batch failed.

Also fixes pre-existing lint warnings:
- Move exports to end of file
- Add explicit return type to processTranslations
- Replace ternary with if-else for upsertMany selection

Co-Authored-By: udit@cal.com <udit222001@gmail.com>

* fix: address review feedback for workflow auto-translation

- Add change detection before creating translation tasks
- Rename userLocale to sourceLocale in task props for clarity
- Show source language in UI with new translation key
- Extract SUPPORTED_LOCALES to shared translationConstants.ts
- Fix locale mapping bug in translateEventTypeData.ts
- Add WhatsApp translation support
- Abstract translation lookup into shared translationLookup.ts helper
- Restore if-else readability for SCANNING_WORKFLOW_STEPS

Co-authored-by: Udit Takkar <udit.takkar@cal.com>
Co-Authored-By: unknown <>

* fix: update test to use sourceLocale instead of userLocale

Co-Authored-By: unknown <>

* refactor: feedback

* fix: handle first time

* fix: tests

* fix: tests

* fix: address Cubic AI review feedback (confidence 9/10 issues)

- WhatsApp translation: Apply variable substitution using getSMSMessageWithVariables
  and clear contentSid when using translated body to ensure Twilio uses the
  translated text instead of the original template

- update.handler.ts: Change sourceLocale assignment from ?? to || for consistency
  with tasker payload behavior (line 481)

- ITranslationService.ts: Rename methods from plural to singular naming:
  - getWorkflowStepTranslations -> getWorkflowStepTranslation
  - getEventTypeTranslations -> getEventTypeTranslation
  Updated all call sites and tests accordingly

Co-Authored-By: unknown <>

* fix: address Cubic AI review feedback (confidence 9/10+ issues)

- Fix getSMSMessageWithVariables to handle WHATSAPP_ATTENDEE action for
  locale and timezone (confidence 9/10)
- Remove WhatsApp translation feature that set contentSid to undefined
  since Twilio ignores body parameter for WhatsApp and requires
  pre-approved Message Templates (confidence 10/10)

Co-Authored-By: unknown <>

* fix: translatio

* Add tests: packages/features/eventTypeTranslation/repositories/EventTypeTranslationRepository.test.ts

Generated by Paragon from proposal for PR #27087

* Add tests: packages/features/tasker/tasks/translateWorkflowStepData.test.ts

Generated by Paragon from proposal for PR #27087

* chore: nit

* chore: verfied atg

* fix: set sourceLocale for new steps, add shouldDirty to checkbox, remove spec docs

- Set sourceLocale fallback in addedSteps mapping to fix stale detection mismatch
- Add { shouldDirty: true } to autoTranslateEnabled checkbox onChange
- Remove specs/workflow-translation/ directory (planning docs, not for repo)

Co-authored-by: Udit Takkar <udit.07.takkar@gmail.com>
Co-Authored-By: unknown <>

* chore: add specs back

* fix: type error

* fix: type error

* fix: type err

* fix: tests

* refactor: feedback

* fix: type err

* refactor

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Udit Takkar <udit.takkar@cal.com>
Co-authored-by: Udit Takkar <udit.07.takkar@gmail.com>
2026-02-25 01:03:55 +05:30

145 lines
5.0 KiB
TypeScript

import type { WorkflowStepTranslationRepository } from "@calcom/features/ee/workflows/repositories/WorkflowStepTranslationRepository";
import type { EventTypeTranslationRepository } from "@calcom/features/eventTypeTranslation/repositories/EventTypeTranslationRepository";
import { locales as i18nLocales } from "@calcom/lib/i18n";
import logger from "@calcom/lib/logger";
import {
TRANSLATION_SUPPORTED_LOCALES,
type TranslationSupportedLocale,
} from "@calcom/lib/translationConstants";
import { EventTypeAutoTranslatedField, WorkflowStepAutoTranslatedField } from "@calcom/prisma/enums";
import type {
EventTypeTranslationLookupOptions,
EventTypeTranslationLookupResult,
ITranslationService,
TranslateTextParams,
TranslateTextResult,
TranslationResult,
WorkflowStepTranslationLookupOptions,
WorkflowStepTranslationLookupResult,
} from "./ITranslationService";
export interface ITranslationServiceDeps {
localizeText: (text: string, sourceLocale: string, targetLocale: string) => Promise<string | null>;
workflowStepTranslationRepository: WorkflowStepTranslationRepository;
eventTypeTranslationRepository: EventTypeTranslationRepository;
}
export class TranslationService implements ITranslationService {
constructor(private deps: ITranslationServiceDeps) {}
getTargetLocales(sourceLocale: string): TranslationSupportedLocale[] {
return TRANSLATION_SUPPORTED_LOCALES.filter(
(locale) => locale !== sourceLocale && i18nLocales.includes(locale)
);
}
async translateText(params: TranslateTextParams): Promise<TranslateTextResult> {
const { text, sourceLocale } = params;
if (!text?.trim()) {
return { translations: [], failedLocales: [] };
}
const targetLocales = this.getTargetLocales(sourceLocale);
const failedLocales: string[] = [];
try {
const translationPromises = targetLocales.map(async (targetLocale) => {
const translatedText = await this.deps.localizeText(text, sourceLocale, targetLocale);
return { translatedText, targetLocale };
});
const results = await Promise.all(translationPromises);
const translations: TranslationResult[] = [];
for (const result of results) {
if (result.translatedText !== null) {
translations.push({
translatedText: result.translatedText,
targetLocale: result.targetLocale,
});
} else {
failedLocales.push(result.targetLocale);
}
}
return { translations, failedLocales };
} catch (error) {
logger.error("TranslationService.translateText() failed:", error);
return { translations: [], failedLocales: targetLocales };
}
}
async getWorkflowStepTranslation(
workflowStepId: number,
targetLocale: string,
options: WorkflowStepTranslationLookupOptions = { includeBody: true, includeSubject: false }
): Promise<WorkflowStepTranslationLookupResult> {
const result: WorkflowStepTranslationLookupResult = {};
const promises: Promise<void>[] = [];
if (options.includeBody) {
promises.push(
this.deps.workflowStepTranslationRepository
.findByLocale(workflowStepId, WorkflowStepAutoTranslatedField.REMINDER_BODY, targetLocale)
.then((translation) => {
if (translation?.translatedText) {
result.translatedBody = translation.translatedText;
}
})
);
}
if (options.includeSubject) {
promises.push(
this.deps.workflowStepTranslationRepository
.findByLocale(workflowStepId, WorkflowStepAutoTranslatedField.EMAIL_SUBJECT, targetLocale)
.then((translation) => {
if (translation?.translatedText) {
result.translatedSubject = translation.translatedText;
}
})
);
}
await Promise.all(promises);
return result;
}
async getEventTypeTranslation(
eventTypeId: number,
targetLocale: string,
options: EventTypeTranslationLookupOptions = { includeTitle: false, includeDescription: true }
): Promise<EventTypeTranslationLookupResult> {
const result: EventTypeTranslationLookupResult = {};
const promises: Promise<void>[] = [];
if (options.includeTitle) {
promises.push(
this.deps.eventTypeTranslationRepository
.findByLocale(eventTypeId, EventTypeAutoTranslatedField.TITLE, targetLocale)
.then((translation) => {
if (translation?.translatedText) {
result.translatedTitle = translation.translatedText;
}
})
);
}
if (options.includeDescription) {
promises.push(
this.deps.eventTypeTranslationRepository
.findByLocale(eventTypeId, EventTypeAutoTranslatedField.DESCRIPTION, targetLocale)
.then((translation) => {
if (translation?.translatedText) {
result.translatedDescription = translation.translatedText;
}
})
);
}
await Promise.all(promises);
return result;
}
}