可以在Unity中使用谷歌翻译。
using SimpleJSON;
using UnityEngine;
using System.Collections;
public class Translate
{
// We have use googles own api built into google Translator.
public static IEnumerator Process(string targetLang, string sourceText, System.Action<string> result)
{
yield return Process("auto", targetLang, sourceText, result);
}
// Exactly the same as above but allow the user to change from Auto, for when google get's all Jerk Butt-y
public static IEnumerator Process(string sourceLang, string targetLang, string sourceText, System.Action<string> result)
{
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);
string translatedText = N[0][0][0];
result(translatedText);
}
}
}
}
网友评论