Created
August 28, 2012 02:31
-
-
Save ralphholzmann/3494413 to your computer and use it in GitHub Desktop.
CanJS and messageformat.js example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
numMessages: "You have {numMessages, plural, one {one message} other {#{numMessages} messages}}." | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || {} ); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h3><%= message( "numMessages", { numMessages: 10 }); %></h3> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment