Skip to content

Commit

Permalink
Message: Provide Bidi structured text support
Browse files Browse the repository at this point in the history
  • Loading branch information
ashensis authored and nkovacs committed May 15, 2017
1 parent 06effea commit dd5770c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
23 changes: 22 additions & 1 deletion doc/api/message/message-formatter.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## .messageFormatter( path ) ➡ function([ variables ])
## .messageFormatter( path, options ) ➡ function([ variables ])

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
Expand All @@ -14,6 +14,18 @@ messages data.
String or Array containing the path of the message content, eg.
`"greetings/bye"`, or `[ "greetings", "bye" ]`.

**options** (optional)

Options should be an Objects, where each property can be referenced by name.
The possible property having name recognizable by messageFormatter is "setBiDiSupport".
It should have Boolean value indicating whether Bidi structuring is to be imposed on formatted
message (like {"setBiDiSupport": true}).
Special Unicode Bidi marks are inserted depending on locale in order to preserve the text
flow of structured message which corresponds give local (from right-to-left for Bidi scripts
like Arabic, Hebrew or Pharsi and left-to-right otherwise.
For more info on Bidi structured text see:
http://cldr.unicode.org/development/development-process/design-proposals/bidi-handling-of-structured-text

**variables** (optional)

Variables can be Objects, where each property can be referenced by name inside a
Expand Down Expand Up @@ -212,6 +224,15 @@ likeFormatter( 2 );
likeFormatter( 3 );
// > "You and 2 others liked this"
```
#### Bidi structured meessage
Globalize.loadMessages({
ar: { breadcrumb: "{0} >> {1} >> {2}" }
});
bidiFormatter = Globalize( "ar" ).messageFormatter( "breadcrumb", {"setBiDiSupport": true} );

bidiFormatter( "First", "Second", "Third" );
// > "Third << Second << First"


Read on [SlexAxton/messageFormatter.js][] for more information on regard of ICU
MessageFormat.
Expand Down
11 changes: 9 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ Globalize.loadMessages = function( json ) {
*
* @path [String or Array]
*
* @options [object]
*
* Format a message given its path.
*/
Globalize.messageFormatter =
Globalize.prototype.messageFormatter = function( path ) {
Globalize.prototype.messageFormatter = function( path, options ) {
var cldr, formatter, message, pluralGenerator, returnFn,
args = slice.call( arguments, 0 );

Expand All @@ -85,6 +87,9 @@ Globalize.prototype.messageFormatter = function( path ) {
validateMessageType( path, message );

var compiler = new messageCompiler( this, Globalize._messageFmts );
if ( options && ( options.setBiDiSupport === true ) ) {
compiler.setBiDiSupport( true );
}
var formatterSrc = compiler
.compile( message, cldr.locale );

Expand Down Expand Up @@ -149,7 +154,9 @@ Globalize.prototype.messageFormatter = function( path ) {
*/
Globalize.formatMessage =
Globalize.prototype.formatMessage = function( path /* , variables */ ) {
return this.messageFormatter( path ).apply( {}, slice.call( arguments, 1 ) );
return ( arguments[ 1 ] && arguments[ 1 ].setBiDiSupport === true ) ?
this.messageFormatter( path, arguments[ 1 ] ).apply( {}, slice.call( arguments, 2 ) ) :
this.messageFormatter( path ).apply( {}, slice.call( arguments, 1 ) );
};

return Globalize;
Expand Down
17 changes: 16 additions & 1 deletion test/functional/message/format-message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define([
define([
"globalize",
"json!cldr-data/supplemental/likelySubtags.json",
"json!cldr-data/supplemental/plurals.json",
Expand All @@ -19,6 +19,9 @@ QUnit.module( ".formatMessage( path [, variables] )", {
greetings: {
hello: "Hello, {name}"
}
},
he: {
breadcrumb: "{0} >> {1} >> {2}",
}
});
},
Expand Down Expand Up @@ -49,4 +52,16 @@ QUnit.test( "should format a message", function( assert ) {
}), "Hello, Beethoven" );
});

QUnit.test( "should format a message", function( assert ) {
assert.equal( Globalize( "en" ).formatMessage( "greetings/hello", {
name: "Beethoven"
}), "Hello, Beethoven" );
});

QUnit.test( "should support Bidi structured text", function( assert ) {
assert.equal( Globalize( "he" ).formatMessage( "breadcrumb", {"setBiDiSupport": true},
[ "Mozart", "Bethoven", "Dvorzak" ]
), "\u200FMozart\u200F >> \u200FBethoven\u200F >> \u200FDvorzak\u200F" );
});

});
10 changes: 10 additions & 0 deletions test/functional/message/message-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ QUnit.module( ".messageFormatter( path )", {
},
"en-GB": {},
fr: {},
he: {
helloArray: "Hello, {0} & {1}"
},
pt: {
amen: "Amém"
},
Expand Down Expand Up @@ -198,6 +201,13 @@ QUnit.test( "should support ICU message format", function( assert ) {
}, "4th category" );
});

QUnit.test( "should support Bidi structured text", function( assert ) {
assert.equal(
Globalize( "he" ).messageFormatter( "helloArray", {"setBiDiSupport": true} )( "Beethoven", "Mozart" ),
"Hello, \u200FBeethoven\u200F & \u200FMozart\u200F"
);
});

// Reference #473
QUnit.test( "should NOT merge array data", function( assert ) {
// Re-loading a message that uses array syntax.
Expand Down

0 comments on commit dd5770c

Please sign in to comment.