I'm making a translation function, and I want to easily add translations inside an object and then change each globally inside the $("body").html(), problem is, when I use new RegExp() inside my for loop it cuts the loop after first iteration.
if (window.location.href.indexOf("sv") > -1) {
//CUSTOM TRANSLATIONS
var translations = {
'All': 'alla',
'Filter Members': 'Filtrera medlemar',
}
for (var key in translations) {
if (translations.hasOwnProperty(key)) {
console.log(key + " -> " + translations[key]);
$body = jQuery("body");
replaceThis = new RegExp(key, "g");
alert(replaceThis);
$body.html($body.html().replace(replaceThis, translations[key]));
}
}
}
$.each( translations, function( key, value ) { //your code});if (translations.hasOwnProperty(key))is useful.<script>since you try to alter<body>'s html. SoquerySelectorAll=>querySelectoralla. (My comment is true only if your scripts are inside your<body>)