Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function applyTranslations() {
- let elements = document.querySelectorAll("[data-translate]");
- let frameElements = frameElement.frame.document.querySelectorAll("[data-translate]");
- let allElements = elements;
- if (frameElements) {
- allElements = Array.from(elements).concat(Array.from(frameElements));
- }
- let updates = [];
- allElements.forEach(function(element) {
- if (element.childNodes && element.childNodes.length > 0) {
- Array.from(element.childNodes).forEach(function(child) {
- if (child.nodeType === Node.TEXT_NODE) {
- let textContent = child.nodeValue;
- let matches = textContent.match(/{([^}]+)}/g);
- if (matches) {
- matches.forEach(function(match) {
- let key = match.slice(1, -1);
- let translation = getNestedValue(globalTranslation, key);
- if (translation) {
- textContent = textContent.replace(match, translation);
- }
- });
- if (child.nodeValue !== textContent) {
- updates.push({ child, textContent });
- }
- }
- }
- });
- }
- });
- function processBatch(updates, batchSize) {
- if (updates.length === 0) {
- return;
- }
- let fragment = document.createDocumentFragment();
- let batch = updates.splice(0, batchSize);
- batch.forEach(function(update) {
- let newNode = document.createTextNode(update.textContent);
- update.child.parentNode.replaceChild(newNode, update.child);
- });
- document.body.appendChild(fragment);
- requestAnimationFrame(() => processBatch(updates, batchSize));
- }
- processBatch(updates, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement