WebResources.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. namespace FastReport.Web.Application
  7. {
  8. internal interface IWebRes : IDisposable
  9. {
  10. void LoadLocale(string fileName);
  11. void LoadLocale(Stream stream);
  12. string Get(string id);
  13. string GetInternal(string id);
  14. void Root(string section);
  15. }
  16. internal sealed class WebRes : IWebRes
  17. {
  18. //private XmlItem root;
  19. private string[] categories;
  20. private const string badResult = "NOT LOCALIZED!";
  21. private XmlDocument locale;
  22. private static readonly XmlDocument builtinLocale;
  23. public void LoadLocale(string fileName)
  24. {
  25. if (File.Exists(fileName))
  26. {
  27. locale = new XmlDocument();
  28. locale.Load(fileName);
  29. }
  30. else
  31. locale = builtinLocale;
  32. }
  33. public void LoadLocale(Stream stream)
  34. {
  35. locale = new XmlDocument();
  36. locale.Load(stream);
  37. }
  38. public string Get(string id)
  39. {
  40. string result = Get(id, locale);
  41. // if no item found, try built-in (english) locale
  42. if (string.IsNullOrEmpty(result))
  43. {
  44. if (locale != builtinLocale)
  45. result = Get(id, builtinLocale);
  46. if (string.IsNullOrEmpty(result))
  47. result = id + " " + badResult;
  48. }
  49. return result;
  50. }
  51. public string GetInternal(string id)
  52. {
  53. return Get(id, locale);
  54. }
  55. private string Get(string id, XmlDocument locale)
  56. {
  57. XmlItem xi = locale.Root;
  58. int i;
  59. foreach (string category in categories)
  60. {
  61. i = xi.Find(category);
  62. if (i == -1)
  63. return null;
  64. xi = xi[i];
  65. }
  66. // find 'id'
  67. if(!string.IsNullOrEmpty(id))
  68. {
  69. i = xi.Find(id);
  70. if (i == -1)
  71. return null;
  72. xi = xi[i];
  73. }
  74. return xi.GetProp("Text");
  75. }
  76. public void Root(string section)
  77. {
  78. categories = section.Split(',');
  79. }
  80. public void Dispose()
  81. {
  82. if (locale != builtinLocale)
  83. locale.Dispose();
  84. }
  85. public WebRes(string section = "")
  86. {
  87. locale = builtinLocale;
  88. Root(section);
  89. }
  90. static WebRes()
  91. {
  92. builtinLocale = new XmlDocument();
  93. using (Stream stream = ResourceLoader.GetStream("en.xml"))
  94. {
  95. builtinLocale.Load(stream);
  96. }
  97. }
  98. }
  99. }