LocalizedStrings.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. var xs = new XmlSerializer(t);
  77. var strings = (LocalizedStringData)Activator.CreateInstance(t);
  78. strings.FileName = file;
  79. Logger.Info("Using String Data from {0}", file);
  80. if (File.Exists(file))
  81. {
  82. using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  83. {
  84. strings = (LocalizedStringData)xs.Deserialize(fs);
  85. }
  86. }
  87. else
  88. {
  89. strings.Save(); //brand new - save it
  90. }
  91. if (strings.ThisVersion != strings.Version && file.ToLower().Contains("-en.xml"))
  92. {
  93. //only re-save the english version as that is the one defined internally
  94. strings = new BaseStrings {FileName = file};
  95. strings.Save();
  96. }
  97. return strings;
  98. }
  99. /// <summary>
  100. /// Adds the string data.
  101. /// </summary>
  102. /// <param name="stringData">The string data.</param>
  103. public void AddStringData(object stringData )
  104. {
  105. //translate our object definition into a dictionary for lookups
  106. // and a reverse dictionary so we can lookup keys by value
  107. foreach (var field in stringData.GetType().GetFields().Where(f => f != null && f.FieldType == typeof(string)))
  108. {
  109. string value;
  110. try
  111. {
  112. value = field.GetValue(stringData) as string;
  113. }
  114. catch (TargetException ex)
  115. {
  116. Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  117. continue;
  118. }
  119. catch (FieldAccessException ex)
  120. {
  121. Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  122. continue;
  123. }
  124. catch (NotSupportedException ex)
  125. {
  126. Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  127. continue;
  128. }
  129. LocalStrings.TryAdd(field.Name, value);
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the string.
  134. /// </summary>
  135. /// <param name="key">The key.</param>
  136. /// <returns>System.String.</returns>
  137. public string GetString(string key)
  138. {
  139. string value;
  140. LocalStrings.TryGetValue(key, out value);
  141. return value;
  142. }
  143. }
  144. }