React Native Api Reference
    Preparing search index...

    Class useTranslation

    useTranslation provides reactive APIs and events to manage real-time translation and receive translated text within a room.

    Each participant declares two languages on the RoomProvider config: speakingLanguage, the language they speak, and translationLanguage, the language they want to receive. A participant with no translationLanguage receives no translated text, even while translation is running for the room.

    One translation service runs for the whole room, but each device receives only the language it asked for, so a device never receives two languages for the same speech. The receive language can be changed at any time with changeTranslationLanguage; the speaking language is fixed once the room is joined.

    A failure that happens after translation has started is delivered as VideoSDKError.TRANSLATION_FAILED on useRoom's onError, not by any of the methods below.

    Index
      • This method can be used to start real-time translation for the room.

      • All participants, including the local participant, will receive the TRANSLATION_STARTING state in the onTranslationStateChanged event.

      • A participant who joins a room that is already translating receives no second TRANSLATION_STARTED, so drive the UI from this event and from the resolution of your own startTranslation() call.

      • Each participant still receives only the language they asked for, so starting translation is a room-level action while the language is a per-device one.

      Parameters

      • Optionalconfig: object

        Optional translation configuration, forwarded to the server unchanged. Omit it — as the calls below do — to use the room's defaults.

      Returns Promise<void>

      const { startTranslation } = useTranslation();

      async function beginTranslation() {
      try {
      await startTranslation();
      } catch (err) {
      console.log("startTranslation failed", err);
      }
      }
      • Triggered when a participant's translation language is changed, once the server has echoed the change back.

      Parameters

      • data: { language: string; participantId: string }
        • language: string

          The language code that participant now receives translations in.

        • participantId: string

          ID of the participant whose translation language changed.

      Returns void

      import { useTranslation } from "@videosdk.live/react-native-sdk";

      function onTranslationLanguageChanged(data) {
      const { participantId, language } = data;
      console.log(`${participantId} now receives ${language}`);
      }

      useTranslation({
      onTranslationLanguageChanged,
      });
      • Triggered when the state of realtime translation changes.

      Parameters

      • data: { status: string }
        • status: string

          status has following values:

          • TRANSLATION_STARTING - Realtime Translation is in starting phase and hasn't started yet.
          • TRANSLATION_STARTED - Realtime Translation has started successfully.
          • TRANSLATION_STOPPING - Realtime Translation is in stopping phase and hasn't stopped yet.
          • TRANSLATION_STOPPED - Realtime Translation has stopped successfully.

      Returns void

      import { useTranslation } from "@videosdk.live/react-native-sdk";

      function onTranslationStateChanged(data) {
      const { status } = data;

      if (status === "TRANSLATION_STARTING") {
      console.log("Realtime translation is starting");
      } else if (status === "TRANSLATION_STARTED") {
      console.log("Realtime translation has started");
      } else if (status === "TRANSLATION_STOPPING") {
      console.log("Realtime translation is stopping");
      } else if (status === "TRANSLATION_STOPPED") {
      console.log("Realtime translation has stopped");
      }
      }

      useTranslation({
      onTranslationStateChanged,
      });
      • Triggered when realtime translated text is received.

      Parameters

      • data: {
            language: string;
            participantId: string;
            participantLanguage: string;
            participantName: string;
            text: string;
            timestamp: number;
            type: string;
        }
        • language: string

          Language code this text was translated into — the one the local participant asked for with translationLanguage or changeTranslationLanguage.

        • participantId: string

          ID of the participant whose speech was translated.

        • participantLanguage: string

          Language code the participant was speaking in — the speakingLanguage they joined with.

        • participantName: string

          Name of the participant whose speech was translated.

        • text: string

          Translated text content.

        • timestamp: number

          Timestamp (in milliseconds since epoch) when the translation was generated.

        • type: string

          Type of translation event:

          • partial - The service is still revising this utterance. Consecutive partial lines rewrite the same utterance rather than continuing it, so replace the last line drawn instead of appending.
          • full - The utterance has settled. The settled text is not always an extension of the last partial, so append to a transcript only on full.

      Returns void

      import { useTranslation } from "@videosdk.live/react-native-sdk";

      function onTranslationText(data) {
      const { participantName, text, language, type } = data;

      if (type === "full") {
      console.log(`${participantName} (${language}): ${text}`);
      }
      }

      useTranslation({
      onTranslationText,
      });