-
Notifications
You must be signed in to change notification settings - Fork 594
Message: Support ICU MessageFormat using slexaxton/messageformat.js #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
## .loadMessages( json ) | ||
|
||
Load messages data. | ||
|
||
The first level of keys must be locales. For example: | ||
|
||
``` | ||
{ | ||
en: { | ||
hello: "Hello" | ||
}, | ||
pt: { | ||
hello: "Olá" | ||
} | ||
} | ||
``` | ||
|
||
ICU MessageFormat pattern is supported: variable replacement, gender and plural | ||
inflections. For more information see [`.messageFormatter( path ) ➡ function([ | ||
variables ])`](./message-formatter.md). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding explanation of how messages are stored, I'd add a paragraph below this one, something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
The provided messages are stored along side other cldr data, under the | ||
"globalize-messages" key. This allows Globalize to reuse the traversal methods | ||
provided by cldrjs. You can inspect this data using | ||
`cldrjs.get("globalize-messages")`. | ||
|
||
### Parameters | ||
|
||
**json** | ||
|
||
JSON object of messages data. Keys can use any character, except `/`, `{` and | ||
`}`. Values (i.e., the message content itself) can contain any character. | ||
|
||
### Example | ||
|
||
```javascript | ||
Globalize.loadMessages({ | ||
pt: { | ||
greetings: { | ||
hello: "Olá", | ||
bye: "Tchau" | ||
} | ||
} | ||
}); | ||
|
||
Globalize( "pt" ).formatMessage( "greetings/hello" ); | ||
➡ Olá | ||
``` | ||
|
||
#### Multiline strings | ||
|
||
Use Arrays as a convenience for multiline strings. The lines will be joined by a | ||
space. | ||
|
||
```javascript | ||
Globalize.loadMessages({ | ||
en: { | ||
longText: [ | ||
"Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi non", | ||
"quis exercitationem culpa nesciunt nihil aut nostrum explicabo", | ||
"reprehenderit optio amet ab temporibus asperiores quasi cupiditate.", | ||
"Voluptatum ducimus voluptates voluptas?" | ||
] | ||
} | ||
}); | ||
|
||
Globalize( "en" ).formatMessage( "longText" ); | ||
➡ "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eligendi non quis exercitationem culpa nesciunt nihil aut nostrum explicabo reprehenderit optio amet ab temporibus asperiores quasi cupiditate. Voluptatum ducimus voluptates voluptas?" | ||
``` | ||
|
||
#### Messages inheritance | ||
|
||
It's possible to inherit messages, for example: | ||
|
||
```javascript | ||
Globalize.loadMessages({ | ||
root: { | ||
amen: "Amen" | ||
}, | ||
pt: { | ||
amen: "Amém" | ||
} | ||
}); | ||
|
||
Globalize( "pt-PT" ).formatMessage( "amen" ); // "Amém" | ||
Globalize( "de" ).formatMessage( "amen" ); // "Amen" | ||
Globalize( "en" ).formatMessage( "amen" ); // "Amen" | ||
Globalize( "en-GB" ).formatMessage( "amen" ); // "Amen" | ||
Globalize( "fr" ).formatMessage( "amen" ); // "Amen" | ||
``` | ||
|
||
Note that `pt-PT`, `de`, `en`, `en-GB`, and `fr` have never been defined. | ||
`.formatMessage()` inherits `pt-PT` messages from `pt` (`pt-PT` ➡ `pt`), and | ||
it inherits the other messages from root, eg. `en-GB` ➡ `en` ➡ `root`. Yes, | ||
`root` is the last bundle of the parent lookup. | ||
|
||
Attention: On browsers, message inheritance only works if the optional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems like a rather subtle note for something that's pretty important. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideas on how this could be rewritten or replaced? This is very analogous to CLDR bundle inheritance. Not everyone may need it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull it up, higher into the document, or format it in bold or a header, something like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Devs will ask what the hack is message inheritance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then format it in bold or make it a header. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😳 Updated. See what you think. |
||
dependency `cldr/unresolved` is loaded. | ||
|
||
```html | ||
<script src="cldr/unresolved.js"></script> | ||
``` |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed the linebreaks here for the first time, probably applies to existing docs. In other places we don't add line breaks to prose in markdown or html (unlike commit messages and code). See for example this file: https://github.com/raw/jquery/jqueryui.com/v1.11.11/page/upgrade-guide/1.11.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the file you mentioned, I see linebreaks on Overview, but not otherwise.☺️
What's the style guide here? No linebreaks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Afaik there is no good reason to manually maintain linebreaks for prose, it just makes it more difficult to make edits. I don't know of any style guide for this. Our html styleguide doesn't mention it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scottgonzalez is there any?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The prose style guide is scattered. See jquery/contribute.jquery.org#75.
The rule is no hard breaks in prose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be tracked by #369