using GameFramework.Localization; using System; using System.Collections; using System.Collections.Generic; using System.Xml; using UnityEngine; using UnityGameFramework.Runtime;
namespace FurryGF { /// <summary> /// XML格式本地化辅助器 /// </summary> public class XmlLocalizationHelper : DefaultLocalizationHelper { /// <summary> /// 解析字典。 /// </summary> /// <param name="dictionaryString">要解析的字典字符串。</param> /// <param name="userData">用户自定义数据。</param> /// <returns>是否解析字典成功。</returns> public override bool ParseData(ILocalizationManager localizationManager, string dictionaryString, object userData) { try { string currentLaunguage = GameEntry.Localization.Language.ToString(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(dictionaryString); XmlNode xmlRoot = xmlDocument.SelectSingleNode("Dictionaries"); XmlNodeList xmlNodedictionaryList = xmlRoot.ChildNodes; for (int i = 0; i < xmlNodedictionaryList.Count; ++i) { XmlNode xmlNodeDictionary = xmlNodedictionaryList.Item(i); if (xmlNodeDictionary.Name != "Dictionary") { continue; } string language = xmlNodeDictionary.Attributes.GetNamedItem("Language").Value; if (language != currentLaunguage) { continue; } XmlNodeList xmlNodeStringList = xmlNodeDictionary.ChildNodes; for (int j = 0; j < xmlNodeStringList.Count; ++j) { XmlNode xmlNodeString = xmlNodeStringList.Item(j); if (xmlNodeString.Name != "String") { continue; } string key = xmlNodeString.Attributes.GetNamedItem("Key").Value; string value = xmlNodeString.Attributes.GetNamedItem("Value").Value; if (!localizationManager.AddRawString(key, value)) { Log.Warning("本地化中,加入key失败 '{0}' 键值可能是重复了.", key); return false; } } } return true; } catch (Exception exception) { Log.Warning("Can not parse dictionary data with exception '{0}'.", exception.ToString()); return false; } } } }