기술 자료

Vivox Core SDK

Vivox Core SDK

Translate text messages

Send text to the Vivox translation service and display the translated results.
읽는 시간 4분최근 업데이트: 6일 전

Translate text on request so that players can read messages written in other languages.
Use translation to help players who speak different languages communicate in text chat. The Vivox Core SDK can translate specific text on request, for example when a player selects a translate button next to a message.
Translate text on request when players choose which messages to translate, or when you need to translate text that doesn't come from Vivox.
For information about how translation works and how the service chooses languages, refer to Introduction to text translation.

Prerequisites

  1. Contact your Vivox representative to enable translation for your application.
  2. Initialize the Vivox Core SDK with
    vx_initialize3
    .
  3. Log in the player's account with
    vx_req_account_anonymous_login
    , and keep the account handle that you use in the login request.

Translate text on request

Send one or more texts to the Vivox service with a
vx_req_account_translate_text
request. The service returns one translation for each text, in the same order as the texts in the request. The service doesn't deliver the translations to anyone else, and doesn't archive or cache them.

Send the translation request

To send a translation request follow these steps:
  1. Call
    vx_req_account_translate_text_create
    to create the request.
  2. Set
    account_handle
    to the handle of the logged-in account.
  3. Set
    target_language
    to the language code to translate into, for example
    fr
    .
  4. Optional: Set
    source_language
    to the language code that the texts are written in, for example
    en-us
    . If you leave
    source_language
    as
    NULL
    , the service detects the language of each text.
  5. Call
    vx_string_list_create
    with the number of texts, and assign the result to
    texts
    . This function creates a
    NULL
    -terminated array.
  6. Copy each text into the array with
    vx_strdup
    .
  7. Call
    vx_issue_request3
    to send the request.
The following example translates two messages into French and lets the service detect the language of each message:
vx_req_account_translate_text_t *req = NULL;vx_req_account_translate_text_create(&req);req->account_handle = vx_strdup(accountHandle);req->target_language = vx_strdup("fr");// source_language stays NULL, so the service detects the language of each text.vx_string_list_create(2, &req->texts);req->texts[0] = vx_strdup("Good game!");req->texts[1] = vx_strdup("Meet me at the bridge.");int result = vx_issue_request3(&req->base, NULL);

Handle the translation response

The SDK delivers the result in a
vx_resp_account_translate_text
response. The response contains
translation_count
entries in
translations
, one for each text in the request, in the same order. Each entry is a
vx_translated_text_t
with the following fields:

Field

Description

translated
1
if the service translated the text.
0
if the service refused the text.
text
The translated text.
NULL
if
translated
is
0
.
detected_source_language
The language that the service detected, if the request didn't specify a source language. Otherwise,
NULL
.
status_code
The service's error code for this text.
0
if
translated
is
1
.
error
The service's error name for this text.
NULL
if
translated
is
1
.
The service can refuse individual texts without affecting the other texts in the request. If the service refuses the whole request, for example because the request exceeds a limit, the response's
base.status_string
describes the problem.
To handle the translation response follow these steps:
  1. In your message loop, check for a response with a
    type
    of
    resp_account_translate_text
    .
  2. Check
    base.return_code
    . If it isn't
    0
    , read
    base.status_string
    to find out why the service refused the request.
  3. For each entry in
    translations
    , check
    translated
    , and then either display
    text
    or handle
    status_code
    and
    error
    .
The following example prints each translation, or the error for each text that the service refused:
void HandleTranslateTextResponse(vx_resp_account_translate_text_t *resp){ if (resp->base.return_code != 0) { // The service refused the whole request. status_string describes the problem. printf("Translation failed: %s\n", resp->base.status_string ? resp->base.status_string : ""); return; } for (int i = 0; i < resp->translation_count; i++) { vx_translated_text_t *translation = resp->translations[i]; if (translation->translated) { printf("Text %d: %s\n", i + 1, translation->text); } else { printf("Text %d failed: %s (%d)\n", i + 1, translation->error ? translation->error : "", translation->status_code); } }}
After you handle the response, the translated texts are available to display in your application, in the same order as the texts in your request.