/Application/GUI/Languages/XmlLanguageDictionary.cs

http://yet-another-music-application.googlecode.com/ · C# · 148 lines · 138 code · 10 blank · 0 comment · 18 complexity · 34393c72520e76c91444bba385a65c2a MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Xml;
  6. using System.Globalization;
  7. using System.Diagnostics;
  8. using System.Windows;
  9. using System.ComponentModel;
  10. namespace Tomers.WPF.Localization
  11. {
  12. public class XmlLanguageDictionary : LanguageDictionary
  13. {
  14. private Dictionary<string, Dictionary<string, string>> _data =
  15. new Dictionary<string, Dictionary<string, string>>();
  16. private string _path;
  17. private string _cultureName;
  18. private string _englishName;
  19. public string Path
  20. {
  21. get { return _path; }
  22. set { _path = value; }
  23. }
  24. public override string CultureName
  25. {
  26. get { return _cultureName; }
  27. }
  28. public override string EnglishName
  29. {
  30. get { return _englishName; }
  31. }
  32. public XmlLanguageDictionary(string path)
  33. {
  34. if (!File.Exists(path))
  35. {
  36. throw new ArgumentException(string.Format("File {0} doesn't exist", path));
  37. }
  38. this._path = path;
  39. }
  40. protected override void OnLoad()
  41. {
  42. XmlDocument xmlDocument = new XmlDocument();
  43. xmlDocument.Load(_path);
  44. if (xmlDocument.DocumentElement.Name != "Dictionary")
  45. {
  46. throw new XmlException("Invalid root element. Must be Dictionary");
  47. }
  48. XmlAttribute englishNameAttribute = xmlDocument.DocumentElement.Attributes["EnglishName"];
  49. if (englishNameAttribute != null)
  50. {
  51. _englishName = englishNameAttribute.Value;
  52. }
  53. XmlAttribute cultureNameAttribute = xmlDocument.DocumentElement.Attributes["CultureName"];
  54. if (cultureNameAttribute != null)
  55. {
  56. _cultureName = cultureNameAttribute.Value;
  57. }
  58. foreach (XmlNode node in xmlDocument.DocumentElement.ChildNodes)
  59. {
  60. if (node.Name == "Value")
  61. {
  62. Dictionary<string, string> innerData = new Dictionary<string, string>();
  63. foreach (XmlAttribute attribute in node.Attributes)
  64. {
  65. if (attribute.Name == "Id")
  66. {
  67. if (!_data.ContainsKey(attribute.Value))
  68. {
  69. _data[attribute.Value] = innerData;
  70. }
  71. }
  72. else
  73. {
  74. innerData[attribute.Name] = attribute.Value;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. protected override void OnUnload()
  81. {
  82. _data.Clear();
  83. }
  84. protected override object OnTranslate(string uid, string vid, object defaultValue, Type type)
  85. {
  86. if (string.IsNullOrEmpty(uid))
  87. {
  88. #region Trace
  89. Debug.WriteLine(string.Format("Uid must not be null or empty"));
  90. #endregion
  91. return defaultValue;
  92. }
  93. if (string.IsNullOrEmpty(vid))
  94. {
  95. #region Trace
  96. Debug.WriteLine(string.Format("Vid must not be null or empty"));
  97. #endregion
  98. return defaultValue;
  99. }
  100. if (!_data.ContainsKey(uid))
  101. {
  102. #region Trace
  103. Debug.WriteLine(string.Format("Uid {0} was not found in the {1} dictionary", uid, EnglishName));
  104. #endregion
  105. return defaultValue;
  106. }
  107. Dictionary<string, string> innerData = _data[uid];
  108. if (!innerData.ContainsKey(vid))
  109. {
  110. #region Trace
  111. Debug.WriteLine(string.Format("Vid {0} was not found for Uid {1}, in the {2} dictionary", vid, uid, EnglishName));
  112. #endregion
  113. return defaultValue;
  114. }
  115. string textValue = innerData[vid];
  116. try
  117. {
  118. if (type == typeof(object))
  119. {
  120. return textValue;
  121. }
  122. else
  123. {
  124. TypeConverter typeConverter = TypeDescriptor.GetConverter(type);
  125. object translation = typeConverter.ConvertFromString(textValue);
  126. return translation;
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. #region Trace
  132. Debug.WriteLine(string.Format("Failed to translate text {0} in dictionary {1}:\n{2}", textValue, EnglishName, ex.Message));
  133. #endregion
  134. return null;
  135. }
  136. }
  137. }
  138. }