-
-
Save grimmdev/979877fcdc943267e44c to your computer and use it in GitHub Desktop.
// We need this for parsing the JSON, unless you use an alternative. | |
// You will need SimpleJSON if you don't use alternatives. | |
// It can be gotten hither. http://wiki.unity3d.com/index.php/SimpleJSON | |
using SimpleJSON; | |
using UnityEngine; | |
using System.Collections; | |
public class Translate : MonoBehaviour { | |
// Should we debug? | |
public bool isDebug = false; | |
// Here's where we store the translated text! | |
private string translatedText = ""; | |
// This is only called when the scene loads. | |
void Start () { | |
// Strictly for debugging to test a few words! | |
if(isDebug) | |
StartCoroutine (Process ("en","Bonsoir.")); | |
} | |
// 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"; | |
// 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); | |
// 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); | |
} | |
} | |
} | |
// Exactly the same as above but allow the user to change from Auto, for when google get's all Jerk Butt-y | |
public IEnumerator Process (string sourceLang, string targetLang, string sourceText) { | |
string url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" | |
+ sourceLang + "&tl=" + targetLang + "&dt=t&q=" + WWW.EscapeURL(sourceText); | |
WWW www = new WWW (url); | |
yield return www; | |
if (www.isDone) { | |
if(string.IsNullOrEmpty(www.error)){ | |
var N = JSONNode.Parse(www.text); | |
translatedText = N[0][0][0]; | |
if(isDebug) | |
print(translatedText); | |
} | |
} | |
} | |
} |
For those who are willing to make it work in Android, according to https://www.codeproject.com/Articles/12711/Google-Translator, a user-agent head should be added in order not to redirect to captcha
with UnityWebRequest, add uwr.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36") before uwr.SendWebRequest()
Hi, i upgrade the code to the UnityWebRequest to remove the debugging warnings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJSON;
using UnityEngine.Networking;
public class Translator : MonoBehaviour
{
// Should we debug?
public bool isDebug = false;
// Here's where we store the translated text!
private string translatedText = "";
// This is only called when the scene loads.
void Start()
{
// Strictly for debugging to test a few words!
if (isDebug)
StartCoroutine(Process("es", "all this code is working!"));
}
// 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";
// 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=" + UnityWebRequest.EscapeURL(sourceText);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
// Check to see if we don't have any errors.
if (string.IsNullOrEmpty(webRequest.error))
{
// Parse the response using JSON.
var N = JSONNode.Parse(webRequest.downloadHandler.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);
}
}
}
// Exactly the same as above but allow the user to change from Auto, for when google get's all Jerk Butt-y
public IEnumerator Process(string sourceLang, string targetLang, string sourceText)
{
string url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
+ sourceLang + "&tl=" + targetLang + "&dt=t&q=" + UnityWebRequest.EscapeURL(sourceText);
using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
// Check to see if we don't have any errors.
if (string.IsNullOrEmpty(webRequest.error))
{
// Parse the response using JSON.
var N = JSONNode.Parse(webRequest.downloadHandler.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);
}
}
}
}
-The translation stops working-
Hi, I'm using this translation method
I have the problem that translating 3 or 5 times a number of random lines stops working and I don't get any more results.
Can someone tell me if the same thing happens and what could be the problem?
I don't get any errors and debugs work fine.
You need change the font