LocalizedStrings.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Common.Localization;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Xml.Serialization;
  10. namespace MediaBrowser.Controller.Localization
  11. {
  12. /// <summary>
  13. /// Class LocalizedStrings
  14. /// </summary>
  15. public class LocalizedStrings
  16. {
  17. /// <summary>
  18. /// The logger
  19. /// </summary>
  20. static internal ILogger Logger { get; set; }
  21. /// <summary>
  22. /// The base prefix
  23. /// </summary>
  24. public const string BasePrefix = "base-";
  25. /// <summary>
  26. /// The local strings
  27. /// </summary>
  28. protected ConcurrentDictionary<string, string> LocalStrings = new ConcurrentDictionary<string, string>();
  29. /// <summary>
  30. /// The _instance
  31. /// </summary>
  32. private static LocalizedStrings _instance;
  33. /// <summary>
  34. /// Gets the instance.
  35. /// </summary>
  36. /// <value>The instance.</value>
  37. public static LocalizedStrings Instance { get { return _instance ?? (_instance = new LocalizedStrings()); } }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="LocalizedStrings" /> class.
  40. /// </summary>
  41. public LocalizedStrings()
  42. {
  43. foreach (var stringObject in Kernel.Instance.StringFiles)
  44. {
  45. AddStringData(LoadFromFile(GetFileName(stringObject),stringObject.GetType()));
  46. }
  47. }
  48. /// <summary>
  49. /// Gets the name of the file.
  50. /// </summary>
  51. /// <param name="stringObject">The string object.</param>
  52. /// <returns>System.String.</returns>
  53. protected string GetFileName(LocalizedStringData stringObject)
  54. {
  55. var path = Kernel.Instance.ApplicationPaths.LocalizationPath;
  56. var name = Path.Combine(path, stringObject.Prefix + "strings-" + CultureInfo.CurrentCulture + ".xml");
  57. if (File.Exists(name))
  58. {
  59. return name;
  60. }
  61. name = Path.Combine(path, stringObject.Prefix + "strings-" + CultureInfo.CurrentCulture.Parent + ".xml");
  62. if (File.Exists(name))
  63. {
  64. return name;
  65. }
  66. //just return default
  67. return Path.Combine(path, stringObject.Prefix + "strings-en.xml");
  68. }
  69. /// <summary>
  70. /// Loads from file.
  71. /// </summary>
  72. /// <param name="file">The file.</param>
  73. /// <param name="t">The t.</param>
  74. /// <returns>LocalizedStringData.</returns>
  75. protected LocalizedStringData LoadFromFile(string file, Type t)
  76. {
  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. }