Skip to content

Instantly share code, notes, and snippets.

@ralphholzmann
Created August 28, 2012 02:31
Show Gist options
  • Save ralphholzmann/3494413 to your computer and use it in GitHub Desktop.
Save ralphholzmann/3494413 to your computer and use it in GitHub Desktop.
CanJS and messageformat.js example
{
numMessages: "You have {numMessages, plural, one {one message} other {#{numMessages} messages}}."
}
steal("en.json", "messageformat.js", function( translation ) {
var mf = new MessageFormat("en"),
cache = {};
// Add EJS helper
can.extend( can.EJS.Helpers.prototype, {
message : function( key, data ) {
if ( ! cache[ key ] ) {
cache[ key ] = mf.compile( translation[ key ] || "ERROR: <" + key + "> NOT DEFINED" );
}
return cache[ key ].call( this, data || {} );
}
});
});
<h3><%= message( "numMessages", { numMessages: 10 }); %></h3>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment