Skip to content

Instantly share code, notes, and snippets.

@rubenhorn
Created October 16, 2019 09:08
Show Gist options
  • Save rubenhorn/e8de0fb635c3936d47cd15dfbaafc3d4 to your computer and use it in GitHub Desktop.
Save rubenhorn/e8de0fb635c3936d47cd15dfbaafc3d4 to your computer and use it in GitHub Desktop.
Unity script for machine translation using the Yandex Translate API
using System.Collections;
using System;
using System.Xml;
using UnityEngine.Networking;
/*
Requirement:
Yandex Translate API key (https://tech.yandex.com/translate/)
Usage:
YTranslate translator = new YTranslate(apiKey);
string text = "Hello world";
StartCoroutine(translator.translate(text, YTranslate.Language.ES, result => {
Debug.Log(result.translatedText);
}));
*/
public class YTranslate {
private string apiKey;
public YTranslate(string apiKey) {
this.apiKey = apiKey;
}
public class Result {
public
const int
NETWORK_ERROR = -1,
SUCCESS = 200,
INVALID_API_KEY = 401,
BLOCKED_API_KEY = 402,
DAILY_REQUESTS_EXCEEDED = 403,
DAILY_TEXT_EXCEEDED = 404,
TEXT_LENGTH_EXCEEDED = 413,
CANNOT_TRANSLATE = 422,
UNSUPPORTED_DIRECTION = 501;
public readonly int status;
public readonly Language language;
public readonly string translatedText;
public Result(int status, Language language, string translatedText) {
this.status = status;
this.language = language;
this.translatedText = translatedText;
}
}
public IEnumerator translate(string text, Language to, System.Action < Result > callback) {
return translate(text, null, to, callback);
}
public IEnumerator translate(string text, Language ? from, Language to, System.Action < Result > callback) {
string encodedText = UnityWebRequest.EscapeURL(text);
string lang = ((from != null ? Enum.GetName(typeof(Language), from) + "-": "") + Enum.GetName(typeof(Language), to)).ToLower();
string requestUrl = string.Format("https://translate.yandex.net/api/v1.5/tr/translate?key={0}&text={1}&lang={2}", apiKey, text, lang);
UnityWebRequest request = UnityWebRequest.Get(requestUrl);
yield
return request.SendWebRequest();
Result result;
if (request.isNetworkError) {
result = new Result(Result.NETWORK_ERROR, to, null);
} else {
string responseText = request.downloadHandler.text;
XmlDocument doc = new XmlDocument();
doc.LoadXml(responseText);
XmlNode translation = doc.SelectSingleNode("Translation");
XmlNode error = doc.SelectSingleNode("Error");
int status;
string translatedText;
if (translation != null) {
status = int.Parse(translation.Attributes["code"].Value);
translatedText = doc.SelectSingleNode("Translation/text").InnerText;
} else {
status = int.Parse(error.Attributes["code"].Value);
translatedText = null;
}
result = new Result(status, to, translatedText);
}
callback(result);
}
public enum Language {
SQ,
EN,
AR,
HY,
AZ,
AF,
EU,
BE,
BG,
BS,
CY,
VI,
HU,
HT,
GL,
NL,
EL,
KA,
DA,
HE,
ID,
GA,
IT,
IS,
ES,
KK,
CA,
KY,
ZH,
KO,
LA,
LV,
LT,
MG,
MS,
MT,
MK,
MN,
DE,
NO,
FA,
PL,
PT,
RO,
RU,
SR,
SK,
SL,
SW,
TG,
TH,
TL,
TT,
TR,
UZ,
UK,
FI,
FR,
HR,
CS,
SV,
ET,
JA
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment