formatjs / handlebars-intl

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

Problem when setting locale #53

Closed bakura10 closed 9 years ago

bakura10 commented 9 years ago

Hi,

I'm trying to make this library works, but I have a few issue. As of today, I only have "en" locale, however everything is rendered according to my browser locale (French), excepting on Safari where the polyfill is loaded with only "en" locale. I'd like to set the default language application wide, but cannot find anyway to do it. The only way I've found is to surround any helper with the intl block helper, with locales to "en", but this is annoying as it basically requires me to wrap the whole page around this block helper.

I'd prefer to pass the language directly (if it's not possible to modify the used locale glboally) directly to helper, like this:

{{formatNumber 5 locales="en"}}

But this syntax does not seem to work. Any hint?

Thanks!

ericf commented 9 years ago

You'll need to determine the user's locale, and conditionally load the Intl.js polyfill and its data for that locale:

http://formatjs.io/guide/#runtime-environments http://formatjs.io/guide/#patch-runtime

bakura10 commented 9 years ago

The polyfill actually works as expected, because I load only "en". The problem comes when Intl is natively available, as it uses my locale (fr-FR) instead of en (that I want as a default as I didn't translate the app into French yet)

ericf commented 9 years ago

The details for how to set the locale when rendering a Handlebars template are specified here: http://formatjs.io/handlebars/

var intlData = {
    locales: 'en-US'
}

var context = {
    price: 1000
};

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

If you can't specify the options when rendering the template for whatever reason, you can always use the {{#intl}} block helper to set the locale for part or all of the template by wrapping it with this helper. Details here: http://formatjs.io/handlebars/#intl

bakura10 commented 9 years ago

Hi,

Sorry for pinging you again, but I'm actually using this in context of an Ember app (side question: is there any Ember-CLI addon planned?). I've been able to successfully make bound helpers out of the Handlebars-Intl helper, but I have no idea about where am I suppose to write the configuration intl (like translation strings, date formats...). Obviously, I cannot use the template method you are saying to me, as I'm using only the Handlebars helpers in Handlebars template.

What I was looking for is: where am I supposed to write globally, in a file, all the string translations, date formats, locales... and having the library consumes it whenever I call the {{formatDate}}, {{formatMessage}}... helpers.

Sorry if this seems obvious, I've read several times the doc but cannot find it. I've looked at the code and couldn't find a place where I could set this.

ericf commented 9 years ago

An Ember integration is something we'd like to add, so that using FormatJS in Ember works out of the box and is more idiomatic Ember.

bakura10 commented 9 years ago

Thank you !

After more test, it appears the main problem is that the package does not give access to internal methods like simpleFormat. If I had access to those, I could easily write my own handlebars helpers and retrieve the local configs from my Ember config and automatically pass it as an option.

Right now, the simplest way is to not use this bundle and copy paste all the needed code, unless you have a better idea ? :)

ericf commented 9 years ago

We are considering the possibility of proving access to an intermediate library which wraps and caches Intl* instances and can do formatting — this lib would then be shared by the high-level FormatJS integration libs. But we're on the fence about it since we already provide the low-level packages.