Beispiel #1
0
        //请注意:语种对应内容为空时,才进行翻译
        private static void TranslateData(LocalizationData data)
        {
            string sourceLang = "auto";
            string sourceText = string.Empty;

            //查看当前配置语言中,是否有存在的文本
            foreach (string lang in Enum.GetNames(typeof(LanguageType)))
            {
                sourceText = data.GetValue(lang);
                if (!string.IsNullOrEmpty(sourceText))
                {
                    sourceLang = lang;
                    break;
                }
            }

            if (sourceLang == "auto" || string.IsNullOrEmpty(sourceText))
            {
                Debug.LogError("There is no source text which id is " + data.id + " in Localization.json!");
                return;
            }

            foreach (string targetLang in Enum.GetNames(typeof(LanguageType)))
            {
                if (targetLang != "auto" && targetLang != sourceLang)
                {
                    //检测是否当前语种有内容,有则跳过
                    string cur = data.GetValue(targetLang);
                    if (!string.IsNullOrEmpty(cur))
                    {
                        continue;
                    }
                    //获取百度翻译结果
                    TranslationResult result = Translate.TranslateByBaidu(sourceText, sourceLang, targetLang);
                    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(targetLang, resultText);
                    }
                }
            }
            LocalizationDataHelper.WriteConfig(data.id, data);
        }
        private static int GetAKey()
        {
            //因为字典的key不能保证连续,而且是编辑器模式,所以无视性能从1开始遍历
            int index = 1;

            while (true)
            {
                LocalizationData data = null;
                if (!LocalizationDataHelper.GetLocalizationDic().TryGetValue(index, out data))
                {
                    return(index);
                }
                index++;
            }
        }
Beispiel #3
0
 //缓存表数据
 public static void WriteConfig(int id, LocalizationData data, bool writeJson = false)
 {
     if (locDatasDic.ContainsKey(id))
     {
         locDatasDic[id] = data;
     }
     else
     {
         locDatasDic.Add(id, data);
     }
     if (writeJson)
     {
         WriteJson();
     }
 }
Beispiel #4
0
        public static void WriteConfig(int id, string text, LanguageType lang, string sourceText, LanguageType sourceLang, bool writeJson = false)
        {
            LocalizationData data;

            if (!locDatasDic.TryGetValue(id, out data))
            {
                data = new LocalizationData();
            }
            data.SetValue(lang.ToString(), text);
            data.SetValue(sourceLang.ToString(), sourceText);
            if (writeJson)
            {
                WriteJson();
            }
        }
 //如果你没有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();
     }
 }