autotranslate.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const { phrasebook } = require("./phrasebook");
  2. /**
  3. * This class is responsible for automatic translate site content of the
  4. * HTML elements. It could automatic translate content for all elements
  5. * which are in the "translate" class, and has "phrase" attribute. This
  6. * attrobite store phrase to translate, and insert into element innerText
  7. * or placeholder (for inputs). It could also observate HTML Node, and
  8. * automatic update transltions when any item had been changed.
  9. */
  10. class autotranslate {
  11. /**
  12. * @var {?phrasebook}
  13. * This store phrasebook to get translates from, or store null, to get
  14. * translate content from global translate function.
  15. */
  16. #phrasebook;
  17. /**
  18. * @var {?MutationObserver}
  19. * This store observer object, when it is connecter and waiting for
  20. * changaes, or store null, when observer currently not working.
  21. */
  22. #observer;
  23. /**
  24. * This create new autotranslator. It require phrasebook, to loads
  25. * translations for phrases from. When null had been given, the it
  26. * use global translate function.
  27. *
  28. * @throws {Error} - When trying to use it in the NodeJS.
  29. *
  30. * @param {?phrasebook} phrasebook
  31. */
  32. constructor(phrasebook = null) {
  33. NODE: throw new Error("It is not avairable in the NodeJS.");
  34. this.#observer = null;
  35. this.#phrasebook = phrasebook;
  36. }
  37. /**
  38. * It return class name for elements, which would be translated by
  39. * autotranslator.
  40. *
  41. * @returns {string} - Class name for autotranslating elements.
  42. */
  43. static get_class_name() {
  44. return "translate";
  45. }
  46. /**
  47. * This return selector for choose elements which must be autotranslated.
  48. *
  49. * @returns {string} - Selector of the elements to translate.
  50. */
  51. get #class_selector() {
  52. return "." + autotranslate.get_class_name();
  53. }
  54. /**
  55. * This return name of attribute which store phrase to translate.
  56. *
  57. * @returns {string} - Name of attribute which store phrase.
  58. */
  59. static get_attribute_name() {
  60. return "phrase";
  61. }
  62. /**
  63. * This check that autotranslator is connected and waiting to changes.
  64. *
  65. * @returns {bool} - True when observer is connected, fakse when not.
  66. */
  67. get is_connected() {
  68. return this.#observer !== null;
  69. }
  70. /**
  71. * This search elements which could be translated in the element given
  72. * in the parameter. When null given, then it search elements in the
  73. * all document.
  74. *
  75. * @param {?HTMLElement} where - Item to load items from or null.
  76. * @returns {Array} - Array of elements to translate.
  77. */
  78. #get_all_items(where = null) {
  79. if (where === null) {
  80. where = document;
  81. }
  82. return Array.from(
  83. where.querySelectorAll(this.#class_selector)
  84. );
  85. }
  86. /**
  87. * It translate given phrase, baseed on loaded phrasebook, or when not
  88. * loaded any, then use global translate function. When it also not
  89. * exists, then throws error in debug mode, or return not translated
  90. * phrase on production.
  91. *
  92. * @throws {Error} - When any option to translate not exists.
  93. *
  94. * @param {string} content - Phrase to translate.
  95. * @returns {string} - Translated content.
  96. */
  97. #translate(content) {
  98. if (this.#phrasebook !== null) {
  99. return this.#phrasebook.translate(content);
  100. }
  101. if (_ === undefined) {
  102. DEBUG: throw new Error("All translate options are unavairable.");
  103. return content;
  104. }
  105. return _(content);
  106. }
  107. /**
  108. * This add mutable observer to the body. It wait for DOM modifications,
  109. * and when any new node had been adder, or any phrase attribute had
  110. * been changed, then it trying to translate it.
  111. *
  112. * @returns {autotranslate} - This object to chain load.
  113. */
  114. connect() {
  115. if (this.is_connected) {
  116. return this;
  117. }
  118. const body = document.querySelector("body");
  119. const callback = (targets) => { this.#process(targets); };
  120. const options = {
  121. childList: true,
  122. attributes: true,
  123. characterData: false,
  124. subtree: true,
  125. attributeFilter: [ autotranslate.get_attribute_name() ],
  126. attributeOldValue: false,
  127. characterDataOldValue: false
  128. };
  129. this.#observer = new MutationObserver(callback);
  130. this.#observer.observe(body, options);
  131. return this;
  132. }
  133. /**
  134. * This prcoess all given in the array mutable records.
  135. *
  136. * @param {Array} targets - Array with mutable records.
  137. */
  138. #process(targets) {
  139. targets.forEach(count => {
  140. if (count.type === "attributes") {
  141. this.#update_single(count.target);
  142. return;
  143. }
  144. this.#get_all_items(count.target).forEach(count => {
  145. this.#update_single(count);
  146. });
  147. });
  148. }
  149. /**
  150. * This disconnect observer, and remove it.
  151. *
  152. * @returns {autotranslate} - This object to chain loading.
  153. */
  154. disconnect() {
  155. if (!this.is_connected) {
  156. return this;
  157. }
  158. this.#observer.disconnect();
  159. this.#observer = null;
  160. return this;
  161. }
  162. /**
  163. * This update single element, based on phrase attribute. When element
  164. * is standard HTMLElement, then it place translated content into
  165. * innerText, but when element is input, like HTMLInputElement or
  166. * HTMLTextAreaElement, then it place result into placeholder. When
  167. * input is button, or submit, then it put content into value.
  168. *
  169. * @param {HTMLElement} target - Element to translate
  170. */
  171. #update_single(target) {
  172. const attrobute_name = autotranslate.get_attribute_name();
  173. const phrase = target.getAttribute(attrobute_name);
  174. const result = this.#translate(phrase);
  175. if (target instanceof HTMLInputElement) {
  176. if (target.type === "button" || target.type === "submit") {
  177. target.value = result;
  178. return;
  179. }
  180. target.placeholder = result;
  181. return;
  182. }
  183. if (target instanceof HTMLTextAreaElement) {
  184. target.placeholder = result;
  185. return;
  186. }
  187. target.innerText = result
  188. }
  189. /**
  190. * This update translation of all elements in the document. It is useable
  191. * when new autotranslator is created.
  192. *
  193. * @returns {autotranslate} - Instance of object to chain loading.
  194. */
  195. update() {
  196. this.#get_all_items().forEach(count => {
  197. this.#update_single(count);
  198. });
  199. return this;
  200. }
  201. }
  202. exports.autotranslate = autotranslate;