How to make i18n with Handlebars.js (mustache templates)?

后端 未结 7 2111
予麋鹿
予麋鹿 2021-01-30 00:30

I\'m currently using Handlebars.js (associated with Backbone and jQuery) to make a web app almost totally client side rendered, and I\'m having issues with the internationalisat

7条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 00:57

    With NodeJs / Express :

    • node-i18n ( detect the Accept-Language header )

        app.use(i18n.init); 
      
    • Sample translation file

      {   
       "hello": "hello",   
       "home-page": {
         "home": "Home",
          "signup": "Sign Up"  
       } 
      }
      
    • In Express controller

      ...
      data.tr = req.__('home-page');
      var template = Handlebars.compile(source);
      var result = template(data);
      
    • Handlebars Template

          
    • {{tr.home}}

提交回复
热议问题