예제 #1
0
        /// <summary>
        /// Load translation from file
        /// </summary>
        /// <param name="filePath">Translation File</param>
        /// <returns><see cref="Translation" /> if file sucessfully loaded, <see cref="null" /> otherwise</returns>
        public static Translation FromFile(FileInfo file)
        {
            if (!file.Exists || !file.FullName.EndsWith(".xml"))
            {
                return(null);
            }
            XmlDocument doc = new XmlDocument();

            doc.Load(file.FullName);
            if (doc.DocumentElement == null || !doc.DocumentElement.HasAttribute("Locale"))
            {
                return(null);
            }
            Locale locale = LocaleHelper.GetLocaleFromString(doc.DocumentElement.GetAttribute("Locale"));

            if (locale == Locale.unk_UNK)
            {
                return(null);
            }
            string localeName = doc.DocumentElement.GetAttribute("Name");
            Dictionary <string, string> translations = new Dictionary <string, string>();

            foreach (XmlElement element in doc.DocumentElement.ChildNodes)
            {
                if (element.Name.ToLower() == "category")
                {
                    foreach (XmlElement subElement in element.ChildNodes)
                    {
                        if (!subElement.HasAttribute("Key") || !subElement.HasAttribute("Value"))
                        {
                            continue;
                        }
                        translations.Add(subElement.GetAttribute("Key"), subElement.GetAttribute("Value"));
                    }
                }
                else
                {
                    if (!element.HasAttribute("Key") || !element.HasAttribute("Value"))
                    {
                        continue;
                    }
                    translations.Add(element.GetAttribute("Key"), element.GetAttribute("Value"));
                }
            }

            return(new Translation(locale, localeName, translations));
        }