private static void ReadJson() { EditorUtility.DisplayProgressBar("EasyLocalization", "Reading Lolization.json", 1f); //读取完毕再开始检测 IEnumerator tempIe = LocalizationDataHelper.ReadConfig(); EditorCoroutine.StartCoroutine(tempIe, () => { EditorCoroutine.StartCoroutine(CheckJson()); }); }
//请注意:语种对应内容为空时,才进行翻译 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++; } }
public override void OnInspectorGUI() { DrawDefaultInspector(); LanguageType temp = (LanguageType)EditorGUILayout.EnumPopup("切换显示语言:", selectLang); if (temp != selectLang) { Localization scr = (Localization)target; string value = LocalizationDataHelper.GetTextById(scr.id, temp); if (!string.IsNullOrEmpty(value)) { scr.ForceSetText(value); selectLang = temp; } } }
private static IEnumerator CheckJson() { Dictionary <int, LocalizationData> dic = new Dictionary <int, LocalizationData>(LocalizationDataHelper.GetLocalizationDic()); int progress = 0; foreach (int key in dic.Keys) { EditorUtility.DisplayProgressBar("EasyLocalization", "Checking Lolization.json. LocalizationData id :" + dic[key].id, progress++ / dic.Keys.Count); TranslateData(LocalizationDataHelper.GetLocalizationDic()[key]); yield return(new WaitForSomeTime(0.1f)); } LocalizationDataHelper.WriteJson(); EditorUtility.ClearProgressBar(); EditorUtility.DisplayDialog("EasyLocalization Setting", "Done!", "OK"); }
/// <summary> /// 响应多语言重置 /// </summary> public void DoChangeLanguage() { //重新获取默认文字 string value = LocalizationDataHelper.GetTextById(id); //表内无数据 if (string.IsNullOrEmpty(value)) { if (!string.IsNullOrEmpty(originalText)) { StartCoroutine(Translate.TranslateByBaidu(originalText, LanguageType.auto.ToString(), LocalizationManager.Language.ToString(), DoChangeAgain)); } } else { originalText = value; ChangeLanguage(); } }
//初始化,请保证需要在多语言使用前生效 public static void Init() { //如果是第一次进游戏,读取系统语言作为默认语言 Language = LanguageType.en; //TODO 增加识别类型 if (Application.systemLanguage == SystemLanguage.Chinese || Application.systemLanguage == SystemLanguage.ChineseSimplified) { Language = LanguageType.zh; } IEnumerator tempIe = LocalizationDataHelper.ReadConfig(); while (true) { //没有下一步的时候表示,表要么有->读到了数据,要么没有->初始化了数据 if (!tempIe.MoveNext()) { ChangeLanguage(Language); break; } } }
//如果你没有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(); } }
public static void Check() { EditorCoroutine.StartCoroutine(LocalizationDataHelper.ReadConfig(), CheckSelected); }