private void OnGetTranslation(Translations translation) { if (translation != null && translation.translations.Length > 0) { setTranslatedString(translation.translations [0].translation); vision.SetTranslated(translatedText); Audio.setText(translatedText, curr_voice); } else { translatedText = "*ERROR*"; } }
// We have use googles own api built into google Translator. public IEnumerator Process(string targetLang, string sourceText) { // We use Auto by default to determine if google can figure it out.. sometimes it can't. //string sourceLang = "auto"; string sourceLang = "en"; // Construct the url using our variables and googles api. string url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + sourceLang + "&tl=" + targetLang + "&dt=t&q=" + WWW.EscapeURL(sourceText); // string url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=AIzaSyDcaHwd9Bf4T9Mwen7D2jSwLL0KvVVi3EE=" + sourceLang + "&tl=" + targetLang + // "&dt=t&q=" + WWW.EscapeURL(sourceText); // Put together our unity bits for the web call. WWW www = new WWW(url); // Now we actually make the call and wait for it to finish. yield return(www); // Check to see if it's done. if (www.isDone) { // Check to see if we don't have any errors. if (string.IsNullOrEmpty(www.error)) { // Parse the response using JSON. var N = JSONNode.Parse(www.text); // Dig through and take apart the text to get to the good stuff. translatedText = N[0][0][0]; // This is purely for debugging in the Editor to see if it's the word you wanted. if (isDebug) { print(translatedText); } // here we have translatedText, do something with it: Audio.setText(translatedText, curr_voice); vision.SetTranslated(translatedText); } } }