Пример #1
0
 public static void TranslateJson()
 {
     //创建完毕 更新Localization.json的翻译情况
     if (CheckBaiduAppID.Check(true))
     {
         EditorCoroutine.StartCoroutine(waitToReadJson());
     }
 }
 //如果你没有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();
     }
 }