美文网首页
Unity3D 使用谷歌翻译

Unity3D 使用谷歌翻译

作者: UnityAsk | 来源:发表于2019-08-17 21:41 被阅读0次

可以在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);
            }
        }
    }
}

相关文章

网友评论

      本文标题:Unity3D 使用谷歌翻译

      本文链接:https://www.haomeiwen.com/subject/bumvjctx.html