コード例 #1
0
        /// <summary>
        /// 协程回调方式(推荐)
        /// </summary>
        /// <param name="q"></param> 要翻译的文本内容
        /// <param name="sourceLanguage"></param> 源语言
        /// <param name="targetLanguage"></param> 目标语言
        /// <param name="callback"></param> 回调事件 错误的情况下返回null
        /// <returns></returns>
        public static IEnumerator TranslateByBaidu(string q, string sourceLanguage, string targetLanguage, Action <TranslationResult> callback)
        {
            //随机数
            System.Random rd   = new System.Random();
            string        salt = rd.Next(100000).ToString();
            //md5加密
            string md5Sign = EncryptString(appId + q + salt + 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}",
                                       UnityWebRequest.EscapeURL(q),
                                       sourceLanguage,
                                       targetLanguage,
                                       appId,
                                       salt,
                                       md5Sign
                                       );

            //使用UnityWebRequest配合协程
            using (UnityWebRequest webRequest = new UnityWebRequest(url))
            {
                webRequest.downloadHandler = new DownloadHandlerBuffer();
                yield return(webRequest.SendWebRequest());

                if (webRequest.isDone)
                {
                    if (webRequest.isHttpError || webRequest.isNetworkError)
                    {
                        callback.Invoke(null);
                    }
                    else
                    {
                        TranslationResult temp = JsonUtility.FromJson <TranslationResult>(webRequest.downloadHandler.text);
                        if (temp.error_code == -1 || temp.error_code == 0)
                        {
                            callback.Invoke(temp);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 直接参数返回方式
        /// </summary>
        /// <param name="q"></param> 要翻译的文本内容
        /// <param name="sourceLang"></param> 源语言
        /// <param name="targetLang"></param> 目标语言
        /// <returns></returns>
        public static TranslationResult TranslateByBaidu(string q, string sourceLanguage, string targetLanguage)
        {
            string jsonResult = String.Empty;

            //随机数
            System.Random rd   = new System.Random();
            string        salt = rd.Next(100000).ToString();
            //md5加密
            string md5Sign = EncryptString(appId + q + salt + password);
            //url
            //https addresss : https://fanyi-api.baidu.com/api/trans/vip/translate
            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}",
                                       UnityWebRequest.EscapeURL(q),
                                       sourceLanguage,
                                       targetLanguage,
                                       appId,
                                       salt,
                                       md5Sign
                                       );
            WebClient wc = new WebClient();

            try
            {
                jsonResult = wc.DownloadString(url);
            }
            catch
            {
                jsonResult = String.Empty;
            }
            if (!string.IsNullOrEmpty(jsonResult))
            {
                TranslationResult temp = JsonUtility.FromJson <TranslationResult>(jsonResult);
                if (temp.error_code == -1 || temp.error_code == 0)
                {
                    return(temp);
                }
            }
            return(null);
        }
コード例 #3
0
 //如果你没有localization.json,那么会看到相关的报错若干,请忽视。
 //会自动生成1个localization.json
 private static void CheckGameObject(GameObject go, string path)
 {
     if (go != null)
     {
         Text[] uiTexts = go.transform.GetComponentsInChildren <Text>(true);
         for (int i = 0; i < uiTexts.Length; i++)
         {
             string text = uiTexts[i].text;
             int    id   = LocalizationDataHelper.GetIdByText(text, LanguageType.zh);
             //数据表找不到,添加
             if (id == 0)
             {
                 LocalizationData data = new LocalizationData();
                 data.id = GetAKey();
                 foreach (LanguageType item in Enum.GetValues(typeof(LanguageType)))
                 {
                     if (item != LanguageType.auto)
                     {
                         if (CheckBaiduAppID.Check())
                         {
                             TranslationResult result = Translate.TranslateByBaidu(text, LanguageType.auto.ToString(), item.ToString());
                             if (result != null)
                             {
                                 string resultText = string.Empty;
                                 //字符串中如果有一些转义符,结果会是多个,直接用\n拼接了。。。一般也用不到别的转义符了
                                 for (int j = 0; j < result.trans_result.Length; j++)
                                 {
                                     if (j != result.trans_result.Length - 1)
                                     {
                                         resultText += result.trans_result[j].dst + "\n";
                                     }
                                     else
                                     {
                                         resultText += result.trans_result[j].dst;
                                     }
                                 }
                                 data.SetValue(item.ToString(), resultText);
                             }
                         }
                         else
                         {
                             Debug.LogError("Please set your own Baidu apppid & passwordkey at Translate.cs. You can get them on https://api.fanyi.baidu.com/.");
                         }
                     }
                 }
                 LocalizationDataHelper.WriteConfig(data.id, data);
                 id = data.id;
             }
             //修改或者添加
             Localization localization = uiTexts[i].gameObject.GetComponent <Localization>();
             if (localization == null)
             {
                 localization    = uiTexts[i].gameObject.AddComponent <Localization>();
                 localization.id = id;
                 EditorUtility.SetDirty(go);
             }
             if (localization.id != id)
             {
                 localization.id = id;
                 EditorUtility.SetDirty(go);
             }
         }
         LocalizationDataHelper.WriteJson();
     }
 }