LocalizedStrings.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Xml.Serialization;
  9. namespace MediaBrowser.Controller.Localization
  10. {
  11. /// <summary>
  12. /// Class LocalizedStrings
  13. /// </summary>
  14. public class LocalizedStrings
  15. {
  16. /// <summary>
  17. /// The logger
  18. /// </summary>
  19. static internal ILogger Logger { get; set; }
  20. /// <summary>
  21. /// The base prefix
  22. /// </summary>
  23. public const string BasePrefix = "base-";
  24. /// <summary>
  25. /// The local strings
  26. /// </summary>
  27. protected ConcurrentDictionary<string, string> LocalStrings = new ConcurrentDictionary<string, string>();
  28. /// <summary>
  29. /// The _instance
  30. /// </summary>
  31. private static LocalizedStrings _instance;
  32. /// <summary>
  33. /// Gets the instance.
  34. /// </summary>
  35. /// <value>The instance.</value>
  36. public static LocalizedStrings Instance { get { return _instance ?? (_instance = new LocalizedStrings()); } }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="LocalizedStrings" /> class.
  39. /// </summary>
  40. public LocalizedStrings()
  41. {
  42. foreach (var stringObject in Kernel.Instance.StringFiles)
  43. {
  44. AddStringData(LoadFromFile(GetFileName(stringObject),stringObject.GetType()));
  45. }
  46. }
  47. /// <summary>
  48. /// Gets the name of the file.
  49. /// </summary>
  50. /// <param name="stringObject">The string object.</param>
  51. /// <returns>System.String.</returns>
  52. protected string GetFileName(LocalizedStringData stringObject)
  53. {
  54. var path = Kernel.Instance.ApplicationPaths.LocalizationPath;
  55. var name = Path.Combine(path, stringObject.Prefix + "strings-" + CultureInfo.CurrentCulture + ".xml");
  56. if (File.Exists(name))
  57. {
  58. return name;
  59. }
  60. name = Path.Combine(path, stringObject.Prefix + "strings-" + CultureInfo.CurrentCulture.Parent + ".xml");
  61. if (File.Exists(name))
  62. {
  63. return name;
  64. }
  65. //just return default
  66. return Path.Combine(path, stringObject.Prefix + "strings-en.xml");
  67. }
  68. /// <summary>
  69. /// Loads from file.
  70. /// </summary>
  71. /// <param name="file">The file.</param>
  72. /// <param name="t">The t.</param>
  73. /// <returns>LocalizedStringData.</returns>
  74. protected LocalizedStringData LoadFromFile(string file, Type t)
  75. {
  76. return new BaseStrings {FileName = file};
  77. //var xs = new XmlSerializer(t);
  78. //var strings = (LocalizedStringData)Activator.CreateInstance(t);
  79. //strings.FileName = file;
  80. //Logger.Info("Using String Data from {0}", file);
  81. //if (File.Exists(file))
  82. //{
  83. // using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  84. // {
  85. // strings = (LocalizedStringData)xs.Deserialize(fs);
  86. // }
  87. //}
  88. //else
  89. //{
  90. // strings.Save(); //brand new - save it
  91. //}
  92. //if (strings.ThisVersion != strings.Version && file.ToLower().Contains("-en.xml"))
  93. //{
  94. // //only re-save the english version as that is the one defined internally
  95. // strings = new BaseStrings {FileName = file};
  96. // strings.Save();
  97. //}
  98. //return strings;
  99. }
  100. /// <summary>
  101. /// Adds the string data.
  102. /// </summary>
  103. /// <param name="stringData">The string data.</param>
  104. public void AddStringData(object stringData )
  105. {
  106. //translate our object definition into a dictionary for lookups
  107. // and a reverse dictionary so we can lookup keys by value
  108. foreach (var field in stringData.GetType().GetFields().Where(f => f != null && f.FieldType == typeof(string)))
  109. {
  110. string value;
  111. try
  112. {
  113. value = field.GetValue(stringData) as string;
  114. }
  115. catch (TargetException ex)
  116. {
  117. Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  118. continue;
  119. }
  120. catch (FieldAccessException ex)
  121. {
  122. Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  123. continue;
  124. }
  125. catch (NotSupportedException ex)
  126. {
  127. Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  128. continue;
  129. }
  130. LocalStrings.TryAdd(field.Name, value);
  131. }
  132. }
  133. /// <summary>
  134. /// Gets the string.
  135. /// </summary>
  136. /// <param name="key">The key.</param>
  137. /// <returns>System.String.</returns>
  138. public string GetString(string key)
  139. {
  140. string value;
  141. LocalStrings.TryGetValue(key, out value);
  142. return value;
  143. }
  144. }
  145. }