Unity3D 使用谷歌翻译

可以在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 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 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);
            }
        }
    }
}
Unity3D 使用谷歌翻译_第1张图片

你可能感兴趣的:(Unity3D 使用谷歌翻译)