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
- Contact your Vivox representative to enable translation for your application.
- Initialize the Vivox Core SDK with .
vx_initialize3 - Log in the player's account with , and keep the account handle that you use in the login request.
vx_req_account_anonymous_login
Translate text on request
Send one or more texts to the Vivox service with a 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.
vx_req_account_translate_textSend the translation request
To send a translation request follow these steps:
- Call to create the request.
vx_req_account_translate_text_create - Set to the handle of the logged-in account.
account_handle - Set to the language code to translate into, for example
target_language.fr - Optional: Set to the language code that the texts are written in, for example
source_language. If you leaveen-usassource_language, the service detects the language of each text.NULL - Call with the number of texts, and assign the result to
vx_string_list_create. This function creates atexts-terminated array.NULL - Copy each text into the array with .
vx_strdup - Call to send the request.
vx_issue_request3
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 response. The response contains entries in , one for each text in the request, in the same order. Each entry is a with the following fields:
vx_resp_account_translate_texttranslation_counttranslationsvx_translated_text_tField | Description |
|---|---|
| |
| The translated text. |
| The language that the service detected, if the request didn't specify a source language. Otherwise, |
| The service's error code for this text. |
| The service's error name for this text. |
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 describes the problem.
base.status_stringTo handle the translation response follow these steps:
- In your message loop, check for a response with a of
type.resp_account_translate_text - Check . If it isn't
base.return_code, read0to find out why the service refused the request.base.status_string - For each entry in , check
translations, and then either displaytranslatedor handletextandstatus_code.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.