LocalizedStrings.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. namespace MediaBrowser.Controller.Localization
  8. {
  9. /// <summary>
  10. /// Class LocalizedStrings
  11. /// </summary>
  12. public class LocalizedStrings
  13. {
  14. public static IServerApplicationPaths ApplicationPaths;
  15. /// <summary>
  16. /// The base prefix
  17. /// </summary>
  18. public const string BasePrefix = "base-";
  19. /// <summary>
  20. /// The local strings
  21. /// </summary>
  22. protected ConcurrentDictionary<string, string> LocalStrings = new ConcurrentDictionary<string, string>();
  23. /// <summary>
  24. /// The _instance
  25. /// </summary>
  26. private static LocalizedStrings _instance;
  27. private readonly IServerApplicationPaths _appPaths;
  28. /// <summary>
  29. /// Gets the instance.
  30. /// </summary>
  31. /// <value>The instance.</value>
  32. public static LocalizedStrings Instance { get { return _instance ?? (_instance = new LocalizedStrings(ApplicationPaths)); } }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="LocalizedStrings" /> class.
  35. /// </summary>
  36. public LocalizedStrings(IServerApplicationPaths appPaths)
  37. {
  38. _appPaths = appPaths;
  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 = _appPaths.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. return new BaseStrings {FileName = file};
  74. //var xs = new XmlSerializer(t);
  75. //var strings = (LocalizedStringData)Activator.CreateInstance(t);
  76. //strings.FileName = file;
  77. //Logger.Info("Using String Data from {0}", file);
  78. //if (File.Exists(file))
  79. //{
  80. // using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
  81. // {
  82. // strings = (LocalizedStringData)xs.Deserialize(fs);
  83. // }
  84. //}
  85. //else
  86. //{
  87. // strings.Save(); //brand new - save it
  88. //}
  89. //if (strings.ThisVersion != strings.Version && file.ToLower().Contains("-en.xml"))
  90. //{
  91. // //only re-save the english version as that is the one defined internally
  92. // strings = new BaseStrings {FileName = file};
  93. // strings.Save();
  94. //}
  95. //return strings;
  96. }
  97. /// <summary>
  98. /// Adds the string data.
  99. /// </summary>
  100. /// <param name="stringData">The string data.</param>
  101. public void AddStringData(object stringData )
  102. {
  103. //translate our object definition into a dictionary for lookups
  104. // and a reverse dictionary so we can lookup keys by value
  105. foreach (var field in stringData.GetType().GetFields().Where(f => f != null && f.FieldType == typeof(string)))
  106. {
  107. string value;
  108. try
  109. {
  110. value = field.GetValue(stringData) as string;
  111. }
  112. catch (TargetException ex)
  113. {
  114. //Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  115. continue;
  116. }
  117. catch (FieldAccessException ex)
  118. {
  119. //Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  120. continue;
  121. }
  122. catch (NotSupportedException ex)
  123. {
  124. //Logger.ErrorException("Error getting value for field: {0}", ex, field.Name);
  125. continue;
  126. }
  127. LocalStrings.TryAdd(field.Name, value);
  128. }
  129. }
  130. /// <summary>
  131. /// Gets the string.
  132. /// </summary>
  133. /// <param name="key">The key.</param>
  134. /// <returns>System.String.</returns>
  135. public string GetString(string key)
  136. {
  137. string value;
  138. LocalStrings.TryGetValue(key, out value);
  139. return value;
  140. }
  141. }
  142. }