Skip to content
Learning English Made Easy
ESL Foundation
ESL Foundation 2
ESL Foundation 3
To Be Lesson – Present, Past & Future
To Be Lesson – Present, Past & Future
(function () {
const extractTextLines = html => {
const temp = document.createElement('div');
temp.innerHTML = html;
return temp.innerHTML.split('
').map(line =>
line.replace(/<[^>]*>?/gm, '').trim()
);
};
const translateLine = async (line, lang) => {
if (lang === 'en') return line;
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${lang}&dt=t&q=${encodeURIComponent(line)}`;
const res = await fetch(url);
const data = await res.json();
return data[0].map(d => d[0]).join(' ');
};
const renderText = async (id, lang) => {
const template = document.getElementById('template-' + id);
const container = document.getElementById('original-text-' + id);
if (!template || !container) return;
const html = template.innerHTML;
const lines = extractTextLines(html);
const translatedLines = [];
for (let line of lines) {
if (!line.trim()) {
translatedLines.push('');
} else {
const translated = await translateLine(line, lang);
translatedLines.push(translated);
}
}
container.innerHTML = '';
const block = document.createElement('p');
block.innerHTML = translatedLines.join('
');
container.appendChild(block);
};
document.addEventListener('DOMContentLoaded', function () {
const blocks = document.querySelectorAll('.translator');
blocks.forEach(block => {
const id = block.dataset.id;
const select = document.getElementById('language-select-' + id);
const container = document.getElementById('original-text-' + id);
const template = document.getElementById('template-' + id);
if (!select || !container || !template) return;
// Load default (English) text
renderText(id, 'en');
// Update on language change
select.addEventListener('change', function () {
renderText(id, this.value);
});
});
});
})();