Translation Service Integration
Relevant source files
The following files were used as context for generating this wiki page:The translation service integration is a core component of the Discord Live Translator bot, enabling the conversion of transcribed speech from a source language (English) into a user-selected target language. This system is designed around a microservice architecture, where the main bot application communicates with a dedicated, self-hosted translation service. The integration is responsible for taking the text output from the Speech-to-Text (STT) module and forwarding it to the translation service, then passing the result to the Text-to-Speech (TTS) module to be read aloud in the voice channel.
This modular approach, defined in the project's Docker configuration, separates the concerns of voice processing, transcription, translation, and speech synthesis. The primary interface for this functionality within the bot's codebase is the translate processor, which abstracts the API call to the external translation service.
Architecture
The translation system relies on a set of containerized services orchestrated by Docker Compose. The main bot application does not perform translation itself but instead delegates the task to a libretranslate service.
Sources: docker-compose.prod.yml
This diagram illustrates the relationship between the main bot application and its dependent services for a full translation cycle. The bot service communicates with both the stt and libretranslate services via HTTP requests.
Sources: docker-compose.prod.yml:5-15
Service Configuration
The connection between the main bot and the translation service is configured through environment variables. The docker-compose.prod.yml file defines the TRANSLATION_HOST variable for the bot service, pointing it to the internal Docker network address of the libretranslate service.
| Service | Environment Variable | Value | Description |
|---|---|---|---|
bot | TRANSLATION_HOST | http://libretranslate:5000 | The address of the translation service. |
bot | STT_HOST | http://stt:8000 | The address of the Speech-to-Text service. |
libretranslate | LT_LOAD_ONLY | de,en,es,fr,it | Restricts loaded models to save memory. |
Sources: docker-compose.prod.yml:11-12, 21-22, src/env.ts:18-19
The env.ts file ensures that TRANSLATION_HOST and STT_HOST are available within the application runtime.
// src/env.ts
const env = createEnv({
// ...
server: {
// ...
STT_HOST: z.string(),
TRANSLATION_HOST: z.string(),
// ...
},
runtimeEnv: process.env
});
Sources: src/env.ts:10-21
End-to-End Translation Flow
The translation process is initiated when a user with translation enabled speaks in a voice channel. The flow involves multiple processors, from audio recording to playing the final translated audio.
This sequence diagram shows the detailed interaction between components during a live translation event.
Initial Transcription to English
A key detail is that the Speech-to-Text (STT) service is configured to always translate the input audio into English. This provides a common source language (en) for all subsequent translation tasks.
// stt/src/main.rs
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 });
// ...
params.set_translate(true);
params.set_language(Some("en"));
// ...
Sources: stt/src/main.rs:38-41
This means the translate function in the bot always receives English text as its input.
Translation API (translate.ts)
The translate function is the core of the integration, responsible for communicating with the libretranslate service.
Sources: src/processors/translate.ts
Function Definition
// src/processors/translate.ts
const translate = async (text: string, target: Exclude<keyof typeof languages, 'en'>): Promise<string> => {
// ...
};
Sources: src/processors/translate.ts:6-7
The function takes the text to be translated and a target language code. It returns the translated text as a string.
API Request
It sends a POST request to the /translate endpoint on the TRANSLATION_HOST.
Request Payload:
| Field | Type | Value | Description |
|---|---|---|---|
q | string | text | The text to be translated. |
source | string | 'en' | The source language, hardcoded to English. |
target | string | target | The desired target language code. |
Sources: src/processors/translate.ts:16-20
Response Handling
The function expects a JSON response and extracts the translatedText field from the response body. If the request fails (i.e., status is not 2xx), it logs an error and returns an empty string.
// src/processors/translate.ts
const data = await response.json() as { translatedText: string };
return data.translatedText;
Sources: src/processors/translate.ts:27-29
Language Configuration
The available languages for translation are defined in src/languages.ts. This object maps a short language key to its display properties and technical codes.
Language Data Structure
Each language entry contains:
icon: A Discord-formatted emoji flag.displayName: The full name of the language.translatorCode: The code used for the translation API (e.g., 'de', 'fr').ttsModel: The model path for the Text-to-Speech engine.
// src/languages.ts
const languages = {
de: {
icon: ':flag_de:',
displayName: 'German',
translatorCode: 'de',
ttsModel: 'tts_models/de/thorsten/vits'
},
// ... other languages
} as const;
Sources: src/languages.ts:1-24
This configuration is used by the /start command to present language choices to the user and by the join command handler to pass the correct translatorCode to the translate function.
Sources: src/commands/start.ts:9-13, src/commands/join.ts:114
Summary
The translation service integration is a well-defined system that leverages a microservice architecture to provide real-time translation. It uses libretranslate as the backend, configured via environment variables. The process is standardized by first transcribing all speech to English via the stt service, and then using the translate processor to convert that English text into a user's chosen language. This modular and configurable design makes the translation feature robust and extensible.