formatjs / handlebars-intl

Handlebars helpers for internationalization.
http://formatjs.io/handlebars/
Other
265 stars 28 forks source link

Global setup #57

Closed yamsellem closed 9 years ago

yamsellem commented 9 years ago

This may sound stupid, but I cannot find a proper answer in the docs.

Looking at how translations are supposed to be given to templates, I've read this:

Setup one of FormatJS's integrations with the following data (the user's current locale, translated strings for that locale, optionally, any custom formats)

Looking to handlebars integration, I cannot figure out where I have to set the locale, translated strings and so on. In other words, where does intlGet find its lookup messages?

To be crystal clear: does intlData has a messages attribute? I don't find it in the source. And does the engine expects every template call to provide the locale, messages and so on? This seems tough.

Bonus question: when a message has no configuration — Welcome on board — is there a shortcut for the call {{formatMessage (intlGet "messages.welcomeOnBoard")}}? Thanks.

caridy commented 9 years ago

@yamsellem it is in the "render" tab on every example here: http://formatjs.io/handlebars/#formatMessage

you will see something like this:

var intlData = {
    "locales": "en-US",
    "messages": {
        "photos": "{name} took {numPhotos, plural,\n  =0 {no photos}\n  =1 {one photo}\n  other {# photos}\n} on {takenDate, date, long}.\n"
    }
};

var html = template(context, {
    data: {intl: intlData}
});

where context is:

var context = {
    name     : 'Annie',
    numPhotos: 1000,
    takenDate: Date.now()
};
yamsellem commented 9 years ago

Hi @caridy, thanks for your feedback.

I was talking about a global setup, something to avoid repeating the intl data all around the place. Something like: HandlebarsIntl.registerMessages(..).

To be honest, this is not realistic, in a web app, to require the messages everywhere it's needed. We need to isolate this responsibility in one single place.

ps. the intl.messagesattributes is not shown a single time on the docs but it can be found in the github readme ;-). Maybe merging those two docs will help.

juandopazo commented 9 years ago

@yamsellem I'm pretty sure intlGet looks for the message in the Handlebars context.

yamsellem commented 9 years ago

@juandopazo if it does, that still implies providing messages every time, which is not realistic, don't you think?

juandopazo commented 9 years ago

Right. Caridy had answered correctly. I see your point now. In the server it's easy to add the messages in a centralized fashion, but it's not so clear how to do it in the client.

yamsellem commented 9 years ago

@juandopazo Cool, we're on the same page ;-).

Maybe someone else has an idea of how to do this client side, maybe it needs a patch? Thanks again.

ps. by curiosity, how will you do this server side?

caridy commented 9 years ago

@yamsellem you call render once for the top level template, you pass the data object, which is propagated thru all partials, there is not need to pass that anywhere else, only when calling render on your top level template.

If you want to have more control on the message resolution process, you can simply ignore getIntl helper, and implement your own helper to locate your messages, there is nothing extraordinary about getIntl, it just returns a string.

caridy commented 9 years ago

as for the centralized messages, it is not recommended, especially if you're doing isomorphic apps since the resolution of messages happens per request on the server.

ericf commented 9 years ago

@yamsellem checkout the "RENDER" tab on this example: http://formatjs.io/handlebars/#formatMessage

yamsellem commented 9 years ago

@ericf my bad, it is there — well hidden, but there ;-). Still, the issue remains on client-side templating — a global configuration is a must have.