Загрузите мой HTML, пока мой JS-код не переведет его с помощью PHP.Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Загрузите мой HTML, пока мой JS-код не переведет его с помощью PHP.

Сообщение Anonymous »

Я пытаюсь загрузить свой HTML, пока мой JS-код не переведет его с помощью PHP. Затем я хотел бы получить содержимое этого переведенного HTML. Я провел много исследований, но не могу этого сделать.
Вот код моего HTML:

Код: Выделить всё




Translate Page



This example displays a simple translate button, with no text.
This example displays a simple translate button, with no text.

This is a paragraph.

A particle is kept at rest at the top of a sphere of diameter \(42 m\). When disturbed slightly,
it slides down. At what height \( h \) from the bottom, the particle will leave the sphere

(a) \( 14m \)

(b) \( 16m \)

(c) \( 35m \)

(d) \( 70m \)




function translateContent() {
const allTextNodes = getAllTextNodes(document.body);
const textContent = allTextNodes.map(node => node.textContent.trim()).join(' ');

// Translate content to French using Google Translate
fetch('https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=fr&dt=t&q=' + encodeURIComponent(textContent))
.then(response => response.json())
.then(data => {
const translatedText = data[0].map(sentence => sentence[0]);
replaceTextNodes(allTextNodes, translatedText.join(' '));
})
.catch(error => console.error('Error translating content:', error));
}

function getAllTextNodes(element) {
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
const textNodes = [];

while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}

return textNodes;
}

function replaceTextNodes(nodes, newText) {
let currentIndex = 0;
nodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
const originalText = node.textContent.trim();
const newTextSlice = newText.slice(currentIndex, currentIndex + originalText.length);
node.textContent = newTextSlice;
currentIndex += originalText.length;
}
});
}

// Trigger translation on page load
window.onload = translateContent;



Я пробовал это:

Код: Выделить всё


Подробнее здесь: [url]https://stackoverflow.com/questions/78470241/load-my-html-until-my-js-code-translates-it-using-php[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»