LocalizedStrings.cs 5.2 KB

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