I am pulling a text from a JavaScript file but the HTML tags don't work.
I followed the best answer here and ran into that problem:
Build multiple language website using jQuery and JSON based methods
This is the part of the JavaScript file:
var arrLang = {
"en-gb": {
"HOME": "Home",
"ABOUT": "About <br> Us",
"CONTACT": "Contact Us",
},
"zh-tw": {
"HOME": "首頁",
"ABOUT": "關於我們",
"CONTACT": "聯絡我們",
}
};
// The default language is English
var lang = localStorage.getItem('lang') || navigator.language.slice(0, 2);
if (!Object.keys(arrLang).includes(lang)){
lang = 'en';
}
// Check for localStorage support
if('localStorage' in window){
var usrLang = localStorage.getItem('uiLang');
if(usrLang) {
lang = usrLang;
}
}
console.log(lang);
$(document).ready(function() {
"use strict";
$(".lang").each(function() {
$(this).text(arrLang[lang][$(this).attr("key")]);
});
});
// get/set the selected language
$(".language-button").click(function() {
"use strict";
var lang = $(this).attr("id");
localStorage.setItem('lang', $(this).attr("id"));
// update localStorage key
if('localStorage' in window){
localStorage.setItem('uiLang', lang);
console.log( localStorage.getItem('uiLang') );
}
$(".lang").each(function() {
$(this).text(arrLang[lang][$(this).attr("key")]);
});
});
HTML: This is how I pull the Text:
<div class="lang" key="ABOUT"></div>
HTML: Expected result:
About
Us
HTML: This is what I get:
About <br> Us
What is a very simple way to make the HTML tags work in this case?
<pre></pre>tag$(this).html(arrLang[lang][$(this).attr("key")]);