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<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);
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参考答案 concat()方法只接受字符串类型的参数,参数不能为空; concat()底层是依靠Arrays.co...
    程序熊大阅读 1,530评论 0 1
  • 我叫胖黑,从小我就不知道爸爸妈妈长什么样子,我是多么渴望爸爸妈妈能在我身边,我知道他们一定也是这么想的。 ...
    柒月柿子阅读 334评论 0 0
  • 史铁生先生曾对一位高位截瘫的病人说:“你要保持阅读,坚持写作,将来才有可能对付绝望。”说得多好,阅读能让人绝处逢生...
    诗鹤远方阅读 1,402评论 1 8
  • 我在碗里待了一个月 我在沙里待了一个月终于 终于我从这暗无天日的堕落之地 长了出来 一个我出来了 千千万万个我也已...
    勿說阅读 291评论 0 0