Advertisement
Mabsitow

Untitled

May 30th, 2024
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function applyTranslations() {
  2.     let elements = document.querySelectorAll("[data-translate]");
  3.     let frameElements = frameElement.frame.document.querySelectorAll("[data-translate]");
  4.  
  5.     let allElements = elements;
  6.     if (frameElements) {
  7.         allElements = Array.from(elements).concat(Array.from(frameElements));
  8.     }
  9.  
  10.     let updates = [];
  11.  
  12.     allElements.forEach(function(element) {
  13.         if (element.childNodes && element.childNodes.length > 0) {
  14.             Array.from(element.childNodes).forEach(function(child) {
  15.                 if (child.nodeType === Node.TEXT_NODE) {
  16.                     let textContent = child.nodeValue;
  17.                     let matches = textContent.match(/{([^}]+)}/g);
  18.                     if (matches) {
  19.                         matches.forEach(function(match) {
  20.                             let key = match.slice(1, -1);
  21.                             let translation = getNestedValue(globalTranslation, key);
  22.                             if (translation) {
  23.                                 textContent = textContent.replace(match, translation);
  24.                             }
  25.                         });
  26.                         if (child.nodeValue !== textContent) {
  27.                             updates.push({ child, textContent });
  28.                         }
  29.                     }
  30.                 }
  31.             });
  32.         }
  33.     });
  34.  
  35.     function processBatch(updates, batchSize) {
  36.         if (updates.length === 0) {
  37.             return;
  38.         }
  39.  
  40.         let fragment = document.createDocumentFragment();
  41.         let batch = updates.splice(0, batchSize);
  42.  
  43.         batch.forEach(function(update) {
  44.             let newNode = document.createTextNode(update.textContent);
  45.             update.child.parentNode.replaceChild(newNode, update.child);
  46.         });
  47.  
  48.         document.body.appendChild(fragment);
  49.  
  50.         requestAnimationFrame(() => processBatch(updates, batchSize));
  51.     }
  52.  
  53.     processBatch(updates, 1);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement