Exemple #1
0
        /// <summary>
        /// Builds an <see cref="XmlLocalizationDictionary"/> from given xml string.
        /// </summary>
        /// <param name="xmlString">XML string</param>
        public static XmlLocalizationDictionary BuildFomXmlString(string xmlString)
        {
            var xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(xmlString);

            var localizationDictionaryNode = xmlDocument.SelectNodes("/localizationDictionary");

            if (localizationDictionaryNode == null || localizationDictionaryNode.Count <= 0)
            {
                throw new FirstNewsException("A Localization Xml must include localizationDictionary as root node.");
            }

            var cultureName = localizationDictionaryNode[0].GetAttributeValueOrNull("culture");

            if (string.IsNullOrEmpty(cultureName))
            {
                throw new FirstNewsException("culture is not defined in language XML file!");
            }

            var dictionary = new XmlLocalizationDictionary(CultureInfo.GetCultureInfo(cultureName));

            var dublicateNames = new List <string>();

            var textNodes = xmlDocument.SelectNodes("/localizationDictionary/texts/text");

            if (textNodes != null)
            {
                foreach (XmlNode node in textNodes)
                {
                    var name = node.GetAttributeValueOrNull("name");
                    if (string.IsNullOrEmpty(name))
                    {
                        throw new FirstNewsException("name attribute of a text is empty in given xml string.");
                    }

                    if (dictionary.Contains(name))
                    {
                        dublicateNames.Add(name);
                    }

                    dictionary[name] = (node.GetAttributeValueOrNull("value") ?? node.InnerText).NormalizeLineEndings();
                }
            }

            if (dublicateNames.Count > 0)
            {
                throw new FirstNewsException("A dictionary can not contain same key twice. There are some duplicated names: " + dublicateNames.JoinAsString(", "));
            }

            return(dictionary);
        }
 protected virtual XmlLocalizationDictionary CreateXmlLocalizationDictionary(string xmlString)
 {
     return(XmlLocalizationDictionary.BuildFomXmlString(xmlString));
 }
Exemple #3
0
 protected virtual XmlLocalizationDictionary CreateXmlLocalizationDictionary(string fileName)
 {
     return(XmlLocalizationDictionary.BuildFomFile(fileName));
 }