Comments (7)
No, __addLocaleData
is only for internal CLDR data that the library needs to make decisions, it is not meant to be used to define/set messages per language. Instead, you should pass those messages as data when rendering the template, like this:
var context = {
foo: 1,
bar: 0.9
};
var intlData = {
"locales": "en-US",
"messages": {
"test": "it works"
}
};
var html = template(context, {
data: {intl: intlData}
});
then, your template could use messages.test
as a valid ICU message that can be resolved:
{{formatMessage (intlGet "messages.test")}}
You can read a lot more about all these here: http://formatjs.io/handlebars/
In the case of ember-intl, there is a lot more boilerplate that does some extra work to integrate it with ember data flow.
from handlebars-intl.
@caridy I have trouble understanding the concept as well and got fooled the same way. There are several parts in the documentation that were misleading me:
When you need to format data in another locale, include its data; e.g., for French: here
You can do this by having the server embed the chosen locale into the generate page. here
I'm confused now. Including another locale doesn't apply to custom messages?
Why would I define and set my locale messages in my application code? Isn't that what this library tries to avoid? Ideally, I would like my application code to be free of any language specific code.
Also, the API of that intlData
object is not clear to me. Is there a reference to what the properties of that object are and what they do?
Can you help me get the full picture?
Many thanks!
from handlebars-intl.
My question is related to client-side templating and seems that the same was discussed in #57 without satisfying resolution
from handlebars-intl.
@mediafreakch intlData
in the example above is not part of your application code, most likely, it will be form by your server per request, or even computed at build time and pushed to your CDN. In your code, you only reference to tokens that are used to lookup for messages in that data structure that was provisioned in your page ahead of time. I hope that clarifies that part.
As for the stuff set up via __addLocaleData()
, we provide that data, it is CLDR data needed by this engine to compute messages, dates, numbers, etc. The format we use should for that structure should not leak to user-land, because we can change it at any given time. Just make sure you include the right file that calls __addLocaleData()
to prepare your app to function. In the future, once browsers start implementing pluralization rules (Intl.PluralRules() is the proposal we are considering), that data is not longer needed because the browser will provide the low level api to figure pluralization, in which case, you will not need to load the data we provided today.
from handlebars-intl.
@caridy ok, I think I got the point. I would provision intlData
ahead of time by creating standalone files that contain the translated messages (eg. en-US.js
, de-DE.js
, etc.), then expose them as a global to client side scripts, eg. window.APP.translations
. The server would then choose and include the right file into the page. The application could then just lookup the keys in the defined global:
var template = Handlebars.compile(/* Template source above */);
var html = template(context, {
data: { intl: window.APP.translations }
});
One more question: What's the intlData
object's API? Why is there for example a locales
key inside? Are there other properties I need to be aware of?
from handlebars-intl.
Excellent, that will work.
As for intlData structure, it has formats, locales and messages. you can see examples of those in the docs http://formatjs.io/handlebars/ in the "render" panel in each example.
you need locales and optionally formats because your messages might contain number, and data tokens, e.g.: "You have reach {value, number, percent}", for that, locale information is needed.
from handlebars-intl.
Perfect, thanks for the clarification!
from handlebars-intl.
Related Issues (20)
- bower package HOT 4
- How to change value after load ? HOT 2
- Issue with a plural in 'ru' locale HOT 1
- Support for padding currency values HOT 1
- intlGet: Print message path if it is undefined HOT 1
- Currency formatting incorrect when no 'cents' in value. HOT 1
- Invalid date
- How to capitalize month
- Dates interval with format long: do non repeat year
- handlebarsIntl is undefined
- Deprecate old NPM package HOT 4
- Using `formatMessage` in a loop construct
- formatNumber: only a number is accepted
- Currency style does not ensure trailing decimal digits HOT 1
- Why am I getting (US$) and not ($) HOT 6
- TypeError: Cannot read property 'escapeExpression' of undefined
- Express Handlebars support HOT 1
- Setting the locale of Handlebars Intl using Express HOT 4
- Noot noot
- Missing Variables Raise an Error and Cause the Entire Template to Fail to Render
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from handlebars-intl.