Irrelon / jquery-lang-js

i18n Allow instant language switching on HTML pages without reloading the page.
https://www.irrelon.com
362 stars 132 forks source link

Templates are not translated #115

Closed aoph closed 6 years ago

aoph commented 6 years ago

Hi

Is there a way to localize html templates? It seems they are unaffected by this library. I've tried both dynamic and non-dynamic loading.

<script>
    function showContent() {
        var temp = document.getElementsByTagName("template")[0];
        var clon = temp.content.cloneNode(true);
        document.body.appendChild(clon);
    }
</script>

<div lang=en>This page is currently in English</div>

<button onclick="showContent()">Show content</button>

<template>
    <div lang=en>This page is currently in English</div>
</template>

The first div is translated successfully, but the second div is not.

Thanks

Irrelon commented 6 years ago

Hey ya. The library scans the document when it starts up to determine which elements require modifying when a language is changed. If you alter the page dynamically without using jQuery the library will have no way to know about it and therefore won't translate any "new" elements.

You can solve it by either using jQuery to modify the DOM, or by using your templates themselves and removing this library entirely. Since you have templating capability it might be easier / better to use a data-binding library and handle language changes via binding?