messageformat.js (GitHub: SlexAxton/messageformat.js, License: WTFPL) is a module by Alex Sexton (@SlexAxton) for handling both pluralization and gender in your applications. It can also lead to much better translations, as it was built by ICU to help solve those two problems for all known CLDR languages – likely all the ones you care about.
npm install messageformat
Features
MessageFormat in Java-land technically incorporates all other type formatting (and the older ChoiceFormat) directly into its messages, however, in the name of filesize, messageformat.js only strives to implement SelectFormat and PluralFormat. There are plans to pull in locale-aware NumberFormat parsing as a “plugin” to this library, but as of right now, it’s best to pass things in preformatted (as suggested in the ICU docs).
- Handles arbitrary nesting of pluralization and select rules.
- Works on the server and the client.
- No i18n necessary – you can use it for just well-formed english sentences.
UX++;
- Speed: Compiles messages to JavaScript code.
- This is great for speed. Message formatting is just string concatenation.
- Run a precompiler at build time and remove the need for most of the library.
filesize--;
- Compatible with other languages that support MessageFormat
- Very whitespace tolerant.
- Supports unicode characters
- Most translation companies are beginning to be exposed to translations like this, even if it’s not their core business.
Example
var mf = new MessageFormat('en');
var message = mf.compile('This is a message.'); // returns a function
message();
// "This is a message."
message = mf.compile('His name is {NAME}.');
message({ "NAME" : "Jed" });
// "His name is Jed."
Have you ever had to localize an application? What was your eperience like?