phrasebook.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const translation = require("./translation.js").translation;
  2. /**
  3. * This class repesents phrasebook, which is something like dictionary,
  4. * but not for words, for all phrases in app. It give functions which
  5. * could find and translates phrases.
  6. */
  7. class phrasebook {
  8. /**
  9. * @var {Map}
  10. * This store phrases in flat notation.
  11. */
  12. #phrases;
  13. /**
  14. * @var {?object}
  15. * This store object for nested object notation.
  16. */
  17. #objects;
  18. /**
  19. * This create new phrasebook from phrases map, and optional object
  20. * for phrases in object notation.
  21. *
  22. * @param {Map} phrases - This contain phrases in flat notation.
  23. * @param {?object} objects - This contain phrases in object notation.
  24. */
  25. constructor(phrases, objects = null) {
  26. if (!(phrases instanceof Map)) {
  27. throw new TypeError("Phrases must an map.");
  28. }
  29. if (objects !== null && typeof(objects) !== "object") {
  30. throw new TypeError("Objects must be null or object.");
  31. }
  32. this.#phrases = phrases;
  33. this.#objects = objects;
  34. }
  35. /**
  36. * This translate given phrase. When phrase is in the nested object
  37. * notation, then try to find phrase in objects. When not, try to find
  38. * phrase in the flat phrases. When could not find phrase, then return
  39. * not translated phrase. Content always is returned as translation
  40. * object, which could be also formated wich numbers, dates and
  41. * much more.
  42. *
  43. * @param {string} phrase - Phrase to translate.
  44. * @returns {translation} - Translated phrase.
  45. */
  46. translate(phrase) {
  47. if (this.#is_nested(phrase)) {
  48. return this.#translate_nested(phrase);
  49. }
  50. return this.#translate_flat(phrase);
  51. }
  52. /**
  53. * This translate given phrase. When phrase is in the nested object
  54. * notation, then try to find phrase in objects. When not, try to find
  55. * phrase in the flat phrases. When could not find phrase, then return
  56. * not translated phrase. Content always is returned as translation
  57. * object, which could be also formated wich numbers, dates and
  58. * much more.
  59. *
  60. * @param {string} phrase - Phrase to translate.
  61. * @returns {translation} - Translated phrase.
  62. */
  63. get(phrase) {
  64. return this.translate(phrase);
  65. }
  66. /**
  67. * Check that phrase is nested or not.
  68. *
  69. * @param {string} phrase - Phrase to check that is nested
  70. * @returns {bool} - True when nested, false when not
  71. */
  72. #is_nested(phrase) {
  73. return phrase.indexOf(".") !== -1;
  74. }
  75. /**
  76. * This translate object notated phrase.
  77. *
  78. * @param {string} phrase - Phrase to translate.
  79. * @returns {translation} - Translated phrase.
  80. */
  81. #translate_nested(phrase) {
  82. if (this.#objects === null) {
  83. return this.#translate_flat(phrase);
  84. }
  85. const parts = phrase.trim().split(".");
  86. let current = this.#objects;
  87. for (const part in parts) {
  88. if (!(part in current)) {
  89. return new translation(phrase, false);
  90. }
  91. }
  92. if (typeof(current) !== "string") {
  93. return new translation(phrase, false);
  94. }
  95. return new translation(current, true);
  96. }
  97. /**
  98. * This translate flat phrase.
  99. *
  100. * @param {string} phrase - Phrase to translate.
  101. * @returns {translation} - Translated phrase.
  102. */
  103. #translate_flat(phrase) {
  104. const prepared = phrasebook.prepare(phrase);
  105. const found = this.#phrases.has(prepared);
  106. const translated = found ? this.#phrases.get(prepared) : phrase;
  107. return new translation(translated, found);
  108. }
  109. /**
  110. * This prepars phrase, that mean replece all spaces with "_", trim
  111. * and also replace all big letters with lowwer.
  112. *
  113. * @param {string} content - Phrase to preapre.
  114. * @return {string} - Prepared phrase.
  115. */
  116. static prepare(content) {
  117. return content
  118. .trim()
  119. .replaceAll(" ", "_")
  120. .replaceAll(".", "_")
  121. .toLowerCase();
  122. }
  123. }
  124. exports.phrasebook = phrasebook;