1.上百度翻译开放平台注册账号
2.unity接入json解析的相关dll文件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using UnityEngine.UI;
public class LanguageTool1 :MonoBehaviour
{
//可以直接到百度翻译API的官网申请
//一定要去申请,不然程序的翻译功能不能使用
private static string appId = "20190724000321073";
private static string password = "74HzbIN_uKglcHJoRSsS";
//button_arr
public Button[] button_arr;
public InputField wen_arr;
public GameObject wen_show;
private Dictionary<string, Dictionary<int, string>> dic = new Dictionary<string, Dictionary<int, string>>();
string sourceStr;
string id = "";
//开始
private void Start()
{
for (int i = 0; i < button_arr.Length; i++)
{
int temp = i;
button_arr[temp].onClick.AddListener(delegate () { Set_event(button_arr[temp].name); } );
}
}
public void Set_event(string str)
{
sourceStr = wen_arr.text;
switch (str)
{
case "英文":
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.en,"英语");
break;
case "阿拉伯":
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.ara, "阿拉伯");
break;
case "法语":
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.fra, "法语");
break;
case "德语":
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.de, "德语");
break;
case "葡萄牙":
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.pt, "葡萄牙");
break;
case "俄语":
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.ru, "俄语");
break;
}
}
private TranslationResult GetTranslationFromBaiduFanyi(string id, string q, Language from, Language to, string language = "")
{
string jsonResult = String.Empty;
//源语言
string languageFrom = from.ToString().ToLower();
//目标语言
string languageTo = to.ToString().ToLower();
//随机数
string randomNum = System.DateTime.Now.Millisecond.ToString();
//md5加密
string md5Sign = GetMD5WithString(appId + q + randomNum + password);
//url
string url = String.Format("http://api.fanyi.baidu.com/api/trans/vip/translate?q={0}&from={1}&to={2}&appid={3}&salt={4}&sign={5}",
q,
languageFrom,
languageTo,
appId,
randomNum,
md5Sign
);
WebClient wc = new WebClient();
try
{
jsonResult = wc.DownloadString(url);
}
catch
{
jsonResult = string.Empty;
}
//结果转json
TranslationResult temp = LitJson.JsonMapper.ToObject<TranslationResult>(jsonResult);
if (null != temp)
{
//print(temp.trans_result[0].dst);
if (string.IsNullOrEmpty(language))
{
//for (int i = 0; i < temp.trans_result.Length; i++)
//{
string str = @"{""Key"":" + id + @",""Content"":" + @"""" + temp.trans_result[0].dst + @"""" + "},";
// Debug.LogError(str);
//print(str);
wen_show.GetComponent<Text>().text = temp.trans_result[0].dst;
//}
}
else
{
//for (int i = 0; i < temp.trans_result.Length; i++)
//{
string str = language + "|" + temp.trans_result[0].dst;
// Debug.LogError(str);
wen_show.GetComponent<Text>().text = temp.trans_result[0].dst;
print(str);
//}
}
}
return null;
}
private static string GetMD5WithString(string input)
{
if (input == null)
{
return null;
}
MD5 md5Hash = MD5.Create();
//将输入字符串转换为字节数组并计算哈希数据
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
//创建一个 Stringbuilder 来收集字节并创建字符串
StringBuilder sBuilder = new StringBuilder();
//循环遍历哈希数据的每一个字节并格式化为十六进制字符串
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
//返回十六进制字符串
return sBuilder.ToString();
}
}
public class Translation
{
public string src { get; set; }
public string dst { get; set; }
}
public enum Language
{
//百度翻译API官网提供了多种语言,这里只列了几种
zh,
en,
spa,
fra,
th,
ara,
ru,
pt,
de,
el,
vie,
cht,
yue,
}
public class TranslationResult
{
错误码,翻译结果无法正常返回
//public string Error_code { get; set; }
//public string Error_msg { get; set; }
public string from { get; set; }
public string to { get; set; }
// public string Query { get; set; }
//翻译正确,返回的结果
//这里是数组的原因是百度翻译支持多个单词或多段文本的翻译,在发送的字段q中用换行符(\n)分隔
public Translation[] trans_result { get; set; }
}