I'm working on a simple translate option, but I'm stuck in case when I have duplicated titles. When titles are different it's working fine, but when I have duplicates it doesn't translate them.
Here is the function:
function Translate(language, availableLanguages) {
let titles = [...document.getElementsByClassName('translate')]
let titlesValues = []
titles.forEach((title) => {
titlesValues.push(title.textContent)
})
for (let i = 0; i < availableLanguages.length; i++) {
const languageObject = availableLanguages[i];
titlesValues.forEach((title) => {
if(Object.values(languageObject).includes(title)) {
titles[titlesValues.indexOf(title)].textContent = languageObject[language]
}
})
}
}
Here is the incoming languages json:
{
"Languages": [
"English",
"German"
]
}
Here is the availableLanguages json:
[
{
"Key": "Hello",
"English": "Hello",
"German": "Hallo"
}
]