Use session.translation to receive real-time translations of what the user (or people around them) are saying. Translations are generated by the cloud from the live audio stream.

Basic Usage

session.translation.on((data) => {
  session.display.showTextWall(data.text);
});
Every call to .on() returns a cleanup function:
const cleanup = session.translation.on((data) => {
  session.display.showTextWall(data.text);
});

// Stop receiving translations
cleanup();

Target Language

Subscribe to translations in a specific target language:
const cleanup = session.translation.to("es", (data) => {
  // Only Spanish translations
  session.display.showTextWall(data.text);
});
You can subscribe to multiple target languages simultaneously. Each call is independent:
const cleanupEs = session.translation.to("es", (data) => {
  // Spanish translations
});

const cleanupFr = session.translation.to("fr", (data) => {
  // French translations
});

Source and Target Language

Subscribe to a specific source-to-target language pair:
const cleanup = session.translation.fromTo("en", "es", (data) => {
  // English to Spanish translations only
  session.display.showTextWall(data.text);
});

Showing Original and Translation

A common pattern is to show both the original text and the translation side by side:
session.translation.to("es", (data) => {
  session.display.showDoubleTextWall(data.originalText, data.text);
});
Or show the translation on the display and speak it aloud:
session.translation.to("es", (data) => {
  if (data.isFinal) {
    session.display.showTextWall(data.text);
    session.speaker.speak(data.text);
  }
});

Stopping Translation

Call the cleanup function returned by .on(), .to(), or .fromTo():
const cleanup = session.translation.on((data) => { ... });

cleanup();
Or stop all translation streams:
session.translation.stop();

Permissions

Your app needs the microphone permission to receive translations (the audio stream powers both transcription and translation).

Complete Example

A real-time translation app that translates English to Spanish:
import { MiniAppServer, type MentraSession } from "@mentra/sdk";

const app = new MiniAppServer({
  packageName: "com.example.translator",
  apiKey: process.env.API_KEY!,
  port: 3000,
});

app.onSession((session: MentraSession) => {
  session.transcription.configure({
    languageHints: ["en"],
  });

  session.translation.to("es", (data) => {
    if (data.isFinal) {
      session.display.showDoubleTextWall(data.originalText, data.text);
    } else {
      session.display.showTextWall(data.text);
    }
  });

  session.display.showTextWall("Translator ready - start speaking");
});

await app.start();

Migrating from v2

// v2
session.events.onTranslation((data) => { ... });

// v3
session.translation.on((data) => { ... });
Language-specific subscriptions (.to(), .fromTo()) are new in v3 and have no v2 equivalent. See the Migration Guide for the full list of changes.