LocalizedStrings.cs 5.6 KB

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