jquery/globalize — pull request #72 by maenu (2011-10-17 19:42:42+00:00)
Title: Add printf-style support for messages

I recently ran into the situation, that I wanted to export some parametrized and localized messages to JavaScript. Imagine a text box that only allows a limited amount of characters. A hint like '23 characters left' would be shown, but this should be localized. So I'd add a message 'charactersLeft' to the culture's info like this:

```js
Globalize.addCultureInfo("en", {
messages: {
"characterLeft": "%0n characters left"
}
});
Globalize.addCultureInfo("de", {
messages: {
"characterLeft": "verbliebene Zeichen: %0n"
}
});
```

Note that the format is similar to the Globalize.format function, but the precision and conversion are switched to be closer to the printf style. I could then do this to obtain the localized message:

```js
var localized = Globalize.localize("charactersLeft", [23]);
// en: "23 character left"
// de: "verbliebene Zeichen: 23"
```

Nice and simple, isn't it?

I added the example 'formatter' witch contains a universal String formatter that behaves similar to C's printf or java.util.Formatter. The replacing happens in a concurrent manner, which means that replacing a specifier with another one won't do any harm. The specifiers support ordinary and argument indexing, precision and conversion (like Globalize.format), but no flags. Its localization is handled by pluging in a so-called Localizer. I added one that uses Globalize to format Dates, Numbers and Strings. The demo page shows the capabilities of the formatter. I've overwritten Globalize.localize in the example to directly integrate the formatter.

I've tested the formatter with jasmine (https://github.com/pivotal/jasmine), the localizer itself and the overwritten Globalize.localize are not tested yet.

Anybody interested in seeing this in Globalize? I'd give you the tests then (they kind of didn't fit into an example folder, so I left them away) or could do other stuff to get this thing merged.