Hong-Phuc Bui
2024-10-16 f8613c9ce2bd4b74b11727d2eae204f49151bcba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Client code,
import {translation} from "./glossary-async.js";
 
 
const domCallbacks = {
    start() {
        console.log("TODO: show an indication that browser is loading translation from server");
    },
    finish() {
        console.log("TODO: remove the loading indication");
    },
    onSuccess(json) {
        const origin = json["origin"]["text"];
        const target = json["target"];
        if(target.length > 0) {
            console.log(target);
            const phrases = target[0]["phrases"];
            let html = `<dl><dt class="glossary-origin">${origin}</dt>` ;
            for(let p of phrases) {
                html += `<dd class="glossary-target">${p["text"]}</dd>`
            }
            html += "</dl>";
            const container = document.getElementById("translation");
            container.innerHTML = html;
        } else { // there is no translation for the origin
            let html = `Keine Übersetzung für <span class="glossary-origin">${origin}</span> in französisch :(`;
            const container = document.getElementById("translation");
            container.innerHTML = html;
        }
    },
    onError(response) {
        console.log("TODO: shows HTTP-Status and some explaination on browser");
    }
}
 
function onGlossaryWordClick(event) {
    const targetElement = event.target;
    const phrase = targetElement.dataset["phrase"]; // match value of attribute data-phrase
    const targetLanguage = "fr";
    translation(phrase, targetLanguage, domCallbacks);
}
 
document.addEventListener("DOMContentLoaded", () => {
    const glossaryWords = document.querySelectorAll(".glossary-online");
    glossaryWords.forEach(element => {
        element.addEventListener("click", onGlossaryWordClick);
    })
});