Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ module.exports = function( grunt ) {
optimize: "none",
paths: {
cldr: "../external/cldrjs/dist/cldr",
"make-plural": "../external/make-plural/make-plural"
"make-plural": "../external/make-plural/make-plural",
messageformat: "../external/messageformat/messageformat"
},
skipSemiColonInsertion: true,
skipModuleInsertion: true,
Expand All @@ -121,16 +122,8 @@ module.exports = function( grunt ) {
onBuildWrite: function( id, path, contents ) {
var name = camelCase( id.replace( /util\/|common\//, "" ) );

// CLDRPluralRuleParser
if ( (/CLDRPluralRuleParser/).test( id ) ) {
return contents

// Replace UMD wrapper into var assignment.
.replace( /\(function\(root, factory\)[\s\S]*?}\(this, function\(\) {/, "var CLDRPluralRuleParser = (function() {" )
.replace( /}\)\);\s+$/, "}());" );

// MakePlural
} else if ( (/make-plural/).test( id ) ) {
if ( (/make-plural/).test( id ) ) {
return contents

// Replace its wrapper into var assignment.
Expand All @@ -156,6 +149,22 @@ module.exports = function( grunt ) {

// Remove MakePlural.load = function(.*) {...return MakePlural;.*};
.replace( /MakePlural.load = function\([\s\S]*?return MakePlural;\n};/, "" );

// messageformat
} else if ( (/messageformat/).test( id ) ) {
return contents

// Replace its wrapper into var assignment.
.replace( /\(function \( root \) {/, [
"var MessageFormat;",
"/* jshint ignore:start */",
"MessageFormat = (function() {"
].join( "\n" ) )
.replace( /if \(typeof exports !== 'undefined'[\s\S]*/, [
"return MessageFormat;",
"}());",
"/* jshint ignore:end */"
].join( "\n" ) );
}

// 1, and 2: Remove define() wrap.
Expand Down Expand Up @@ -358,7 +367,7 @@ module.exports = function( grunt ) {
"jscs:source",

// TODO fix issues, enable
// "jscs:test",
//"jscs:test",
"test:unit",
"clean",
"requirejs",
Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ information on its usage.
| globalize.js | 1.3KB | [Core library](#core) |
| globalize/currency.js | +2.6KB | [Currency module](#currency_module) provides currency formatting and parsing |
| globalize/date.js | +4.8KB | [Date module](#date_module) provides date formatting and parsing |
| globalize/message.js | +0.5KB | [Message module](#message_module) provides message translation |
| globalize/message.js | +5.5KB | [Message module](#message_module) provides ICU message format support |
| globalize/number.js | +2.9KB | [Number module](#number_module) provides number formatting and parsing |
| globalize/plural.js | +1.7KB | [Plural module](#plural_module) provides pluralization support |

Expand Down Expand Up @@ -273,17 +273,23 @@ to you in different flavors):
<a name="message_module"></a>
### Message module

- **`Globalize.loadTranslations( json )`**
- **`Globalize.loadMessages( json )`**

Load translation data.
Load messages data.

[Read more...](doc/api/message/load-translation.md)
[Read more...](doc/api/message/load-messages.md)

- **`.translate( path )`**
- **`.messageFormatter( path ) ➡ function([ variables ])`**

Translate item given its path.
Return a function that formats a message (using ICU message format pattern)
given its path and a set of variables into a user-readable string. It supports
pluralization and gender inflections.

[Read more...](doc/api/message/translate.md)
[Read more...](doc/api/message/message-formatter.md)

- **`.formatMessage( path [, variables ] )`**

Alias to `.messageFormatter( path )([ variables ])`.

<a name="number_module"></a>
### Number module
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"devDependencies": {
"make-plural": "eemeli/make-plural.js#2.1.2",
"es5-shim": "3.4.0",
"messageformat": "SlexAxton/messageformat.js#debeaf4",
"qunit": "1.12.0",
"requirejs": "2.1.9",
"requirejs-plugins": "1.0.2",
Expand Down
102 changes: 102 additions & 0 deletions doc/api/message/load-messages.md
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
Copy link
Contributor

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

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottgonzalez is there any?

Copy link
Contributor

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.

Copy link
Member Author

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

inflections. For more information see [`.messageFormatter( path ) ➡ function([
variables ])`](./message-formatter.md).
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

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 [aka cldr-traverse, maybe]. You can inspect this data using cldrjs.get("globalize-messages").

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

[aka cldr-traverse, maybe]

🙈 I'm skipping that, but I'm adding a comment here to keep track of.


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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devs will ask what the hack is message inheritance?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then format it in bold or make it a header.

Copy link
Member Author

Choose a reason for hiding this comment

The 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>
```
68 changes: 0 additions & 68 deletions doc/api/message/load-translation.md

This file was deleted.

Loading