LocalizedStrings.cs 5.3 KB

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