WebResources.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. namespace FastReport.Web
  7. {
  8. internal class WebRes
  9. {
  10. private string category;
  11. private const string badResult = "NOT LOCALIZED!";
  12. private XmlDocument locale;
  13. private static readonly XmlDocument builtinLocale;
  14. public void LoadLocale(string fileName)
  15. {
  16. if (File.Exists(fileName))
  17. {
  18. locale = new XmlDocument();
  19. locale.Load(fileName);
  20. }
  21. else
  22. locale = builtinLocale;
  23. }
  24. public void LoadLocale(Stream stream)
  25. {
  26. locale = new XmlDocument();
  27. locale.Load(stream);
  28. }
  29. public string Get(string id)
  30. {
  31. if (!String.IsNullOrEmpty(category))
  32. {
  33. return InternalGet(category + "," + id);
  34. }
  35. else
  36. return InternalGet(id);
  37. }
  38. private string InternalGet(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. private string Get(string id, XmlDocument locale)
  52. {
  53. string[] categories = id.Split(',');
  54. XmlItem xi = locale.Root;
  55. int i;
  56. foreach (string category in categories)
  57. {
  58. i = xi.Find(category);
  59. if (i == -1)
  60. return null;
  61. xi = xi[i];
  62. }
  63. return xi.GetProp("Text");
  64. }
  65. public void Root(string Section)
  66. {
  67. category = Section;
  68. }
  69. public WebRes(string Section = "")
  70. {
  71. locale = builtinLocale;
  72. Root(Section);
  73. }
  74. static WebRes()
  75. {
  76. builtinLocale = new XmlDocument();
  77. using (Stream stream = ResourceLoader.GetStream("en.xml"))
  78. {
  79. builtinLocale.Load(stream);
  80. }
  81. }
  82. }
  83. }