META-INF.resources.bower_components.globalize.src.common.format-message.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jwebmp-globalize Show documentation
Show all versions of jwebmp-globalize Show documentation
The JWebSwing implementation for a full Globalization
The newest version!
define([
"../util/to-string"
], function (toString) {
/**
* formatMessage( message, data )
*
* @message [String] A message with optional {vars} to be replaced.
*
* @data [Array or JSON] Object with replacing-variables content.
*
* Return the formatted message. For example:
*
* - formatMessage( "{0} second", [ 1 ] ); // 1 second
*
* - formatMessage( "{0}/{1}", ["m", "s"] ); // m/s
*
* - formatMessage( "{name} <{email}>", {
* name: "Foo",
* email: "[email protected]"
* }); // Foo
*/
return function (message, data) {
// Replace {attribute}'s
message = message.replace(/{[0-9a-zA-Z-_. ]+}/g, function (name) {
name = name.replace(/^{([^}]*)}$/, "$1");
return toString(data[name]);
});
return message;
};
});