How is internationalization configured for Hogan.js?

后端 未结 2 1731
名媛妹妹
名媛妹妹 2021-01-20 15:31

I\'m looking to use hogan.js to create html form a template in the browser. I\'ve read that hogan supports i18n, but I can\'t find an example of how this works. How do you

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 16:07

    It would seem I was confusing an older fork of Mustache.js from Twitter, with Hogan a separate mustache compiler from also from twitter. The fork does support an {{_i}} tag for internationalization. This will then call a global function with the name _ in which you provide you're own method for looking up the translated value. E.g.

    translatedStrings = {
       name: "Nom";
    }
    
    function _(i18nKey) {
       return translatedStrings[i18nKey];
    }
    
    var template = "{{_i}}Name{{/i}}: {{username}}",
        context = {username: "Jean Luc"};
    
    Mustache.to_html(template, context);
    

    Would return "Nom: Jean Luc". Whereas with Hogan internationalization is achieved with normal mustache lambdas, e.g.:

    translatedStrings = {
       name: "Nom";
    }
    
    var template = "{{#i18n}}Name{{/i18n}}: {{username}}",
        context = {
           username: "Jean Luc",
           i18n: function (i18nKey) {return translatedStrings[i18nKey];}
        };
    
    Hogan.compile(template).render(context);
    

    See http://mustache.github.com/mustache.5.html for more on providing lambdas. So the main distinction is that the function to look up translations must always be provided on the context when rendering with Hogan, whereas the mustache fork will look up a global method.

提交回复
热议问题