有时候临时改了加了一些中文,然后多语言那边没有及时给到外包翻译,这时候拿个百度翻译做一下翻译,不过简单的翻译还是可以用的。
http://api.fanyi.baidu.com/api/trans/product/apidoc 这个是百度翻译api文档
代码里面必须要加appid和password(秘钥)
上面地址打开管理控制台就可以申请,是免费的
如下图,最下面是appid和秘钥
工具在tool/翻译工具
打开之后执行翻译如下图
如果定义语言类型,这个枚举定义了几种具体可以看看下面截图
public enum Language
{
zh,
en,
spa,
fra,
th,
ara,
ru,
pt,
de,
el,
vie,
cht,
yue,
}
下面是完整代码
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;
public class TranslateLanguageTool : EditorWindow
{
//可以直接到百度翻译API的官网申请
//一定要去申请,不然程序的翻译功能不能使用
private static string appId = "";
private static string password = "";
[MenuItem("Tools/翻译工具")]
public static void Open()
{
GetWindow("工具");
}
private Dictionary> dic = new Dictionary>();
string sourceStr = "";
string id = "";
public void OnGUI()
{
id = EditorGUILayout.TextField("id", id);
sourceStr = EditorGUILayout.TextField("中文", sourceStr);
if (GUILayout.Button("程序专用翻译"))
{
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.ara);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.en);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.fra);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.de);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.pt);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.ru);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.spa);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.cht);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.th);
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.vie);
}
if (GUILayout.Button("翻译"))
{
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.ara, "阿拉伯");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.fra, "法语");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.de, "德语");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.pt, "葡萄牙");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.ru, "俄语");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.spa, "西班牙");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.th, "泰语");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.cht, "繁体中文");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.vie, "越南语");
GetTranslationFromBaiduFanyi(id, sourceStr, Language.zh, Language.en, "英文");
}
}
private static 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(jsonResult);
if (null != temp)
{
if (string.IsNullOrEmpty(language))
{
for (int i = 0; i < temp.trans_result.Length; i++)
{
string str = @"{""Key"":" + id + @",""Content"":" + @"""" + temp.trans_result[i].dst + @"""" + "},";
Debug.LogError(str);
}
}
else
{
for (int i = 0; i < temp.trans_result.Length; i++)
{
string str = language + "|" + temp.trans_result[i].dst;
Debug.LogError(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; }
}
工程下载地址
链接:https://pan.baidu.com/s/142PoZyS9jYKYDIAq9HY-Gg
提取码:wnk9
注意必须要填appid和秘钥才能使用哦