airbnb / polyglot.js

Give your JavaScript the ability to speak many languages.
http://airbnb.github.io/polyglot.js
BSD 2-Clause "Simplified" License
3.71k stars 208 forks source link

Example for html5 #54

Closed marcocastignoli closed 8 years ago

marcocastignoli commented 8 years ago

Is there an example for html5 code?

This is my implementation (it works!), is it correct?

file: translations/it.json

{
    "test":"Test",
    ...
}

file: index.html

<lang key="test"></lang>
<script>
function setLanguage(id){
    var url=null;
    switch (id) {
        case 'it':
            url='translations/it.json';
            break;
        default:
    }
    if (url) {
        $.getJSON(url,function(translation){
                    var polyglot = new Polyglot({phrases: translation});
                    $('lang').each(function(id,obj){
                        var key=$(obj).attr('key');
                        $(obj).html(polyglot.t(key));
                    });
                    localStorage.setItem("language", id);
        });
    } else {
        $('lang').each(function(id,obj){
            var key=$(obj).attr('key');
            $(obj).html(key);
        });
        console.error("Language: the selected translation (\""+id+"\") doesn't exists.");
    }
}
</script>
ljharb commented 8 years ago

I'm not sure I understand your question. As far as I know, there's no such thing as a lang element - can you link to some sort of documentation describing it?

marcocastignoli commented 8 years ago

I just created it, my question is, how to implement it in the right way?

ljharb commented 8 years ago

It's not valid HTML to create arbitrary elements. There's not currently a right/standard way to implement custom elements.

Either way, that question isn't related to polyglot, so I'm going to close this if that's alright.

marcocastignoli commented 8 years ago

I thank you for your suggestion, but my question was about polyglot. How to implement it in html5? that was my idea, do you have examples? Maybe better to put one in the readme

ljharb commented 8 years ago

@marcocastignoli "implement it in html5" doesn't make sense. can you elaborate on what you're trying to do? Whether you use "html5" or not is utterly irrelevant to using polyglot (or any other functional lib).