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

Rendr + Polyglot: How can I load the translations in base layout template? #31

Closed cristodcgomez closed 9 years ago

cristodcgomez commented 9 years ago

Hi! I've been tried to start a small app using Rendr and translate the menu options and some word with polyglot. I read the doc but when I try to use the .t() function I can't use it.

This is my __layout.hbs:

            <li><a href="/">{{appData.polyglot.t("home")}}</a></li>

but the server responses:

Error: Parse error on line 20:
...>{{appData.polyglot.t("home")}}</a></li>
-----------------------^
Expecting 'ID', got 'INVALID'

Am I wrong with the call? How can I load the values for the layout?

cristodcgomez commented 9 years ago

Nevermind, I fixed this using this in handlebarsHelpers.js:

module.exports = function(Handlebars) {
  return {
    copyright: function(year) {
      return new Handlebars.SafeString("&copy;" + year);
    },
    t: function (key) {
      return this.app.polyglot.t(key);
    }
  };
};

In __layout.hbs I use {{t "home"}} and it works.