XmlLocalizationDictionary.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Xml;
  6. using Abp.Collections.Extensions;
  7. using Abp.Extensions;
  8. using Abp.Xml.Extensions;
  9. namespace Abp.Localization.Dictionaries.Xml
  10. {
  11. /// <summary>
  12. /// This class is used to build a localization dictionary from XML.
  13. /// </summary>
  14. /// <remarks>
  15. /// Use static Build methods to create instance of this class.
  16. /// </remarks>
  17. public class XmlLocalizationDictionary : LocalizationDictionary
  18. {
  19. /// <summary>
  20. /// Private constructor.
  21. /// </summary>
  22. /// <param name="cultureInfo">Culture of the dictionary</param>
  23. private XmlLocalizationDictionary(CultureInfo cultureInfo)
  24. : base(cultureInfo)
  25. {
  26. }
  27. /// <summary>
  28. /// Builds an <see cref="XmlLocalizationDictionary"/> from given file.
  29. /// </summary>
  30. /// <param name="filePath">Path of the file</param>
  31. public static XmlLocalizationDictionary BuildFomFile(string filePath)
  32. {
  33. try
  34. {
  35. return BuildFomXmlString(File.ReadAllText(filePath));
  36. }
  37. catch (Exception ex)
  38. {
  39. throw new AbpException("Invalid localization file format! " + filePath, ex);
  40. }
  41. }
  42. /// <summary>
  43. /// Builds an <see cref="XmlLocalizationDictionary"/> from given xml string.
  44. /// </summary>
  45. /// <param name="xmlString">XML string</param>
  46. public static XmlLocalizationDictionary BuildFomXmlString(string xmlString)
  47. {
  48. var xmlDocument = new XmlDocument();
  49. xmlDocument.LoadXml(xmlString);
  50. var localizationDictionaryNode = xmlDocument.SelectNodes("/localizationDictionary");
  51. if (localizationDictionaryNode == null || localizationDictionaryNode.Count <= 0)
  52. {
  53. throw new AbpException("A Localization Xml must include localizationDictionary as root node.");
  54. }
  55. var cultureName = localizationDictionaryNode[0].GetAttributeValueOrNull("culture");
  56. if (string.IsNullOrEmpty(cultureName))
  57. {
  58. throw new AbpException("culture is not defined in language XML file!");
  59. }
  60. var dictionary = new XmlLocalizationDictionary(CultureInfo.GetCultureInfo(cultureName));
  61. var dublicateNames = new List<string>();
  62. var textNodes = xmlDocument.SelectNodes("/localizationDictionary/texts/text");
  63. if (textNodes != null)
  64. {
  65. foreach (XmlNode node in textNodes)
  66. {
  67. var name = node.GetAttributeValueOrNull("name");
  68. if (string.IsNullOrEmpty(name))
  69. {
  70. throw new AbpException("name attribute of a text is empty in given xml string.");
  71. }
  72. if (dictionary.Contains(name))
  73. {
  74. dublicateNames.Add(name);
  75. }
  76. dictionary[name] = (node.GetAttributeValueOrNull("value") ?? node.InnerText).NormalizeLineEndings();
  77. }
  78. }
  79. if (dublicateNames.Count > 0)
  80. {
  81. throw new AbpException("A dictionary can not contain same key twice. There are some duplicated names: " + dublicateNames.JoinAsString(", "));
  82. }
  83. return dictionary;
  84. }
  85. }
  86. }