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