Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
messageformat
Advanced tools
PluralFormat and SelectFormat Message and i18n Tool - A JavaScript Implemenation of the ICU standards.
The messageformat npm package is a tool for handling internationalization (i18n) in JavaScript applications. It allows developers to create localized messages using ICU MessageFormat syntax, which supports pluralization, gender, and other complex message formatting needs.
Basic Message Formatting
This feature allows you to create and compile basic messages with placeholders that can be replaced with dynamic values.
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const message = mf.compile('Hello, {name}!');
console.log(message({ name: 'Alice' })); // Output: Hello, Alice!
Pluralization
This feature supports pluralization, allowing you to define different message formats based on the quantity of items.
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const message = mf.compile('{count, plural, one {# item} other {# items}}');
console.log(message({ count: 1 })); // Output: 1 item
console.log(message({ count: 5 })); // Output: 5 items
Gender Formatting
This feature allows you to format messages based on gender, providing different message templates for male, female, and other genders.
const MessageFormat = require('messageformat');
const mf = new MessageFormat('en');
const message = mf.compile('{gender, select, male {He} female {She} other {They}} will respond soon.');
console.log(message({ gender: 'male' })); // Output: He will respond soon.
console.log(message({ gender: 'female' })); // Output: She will respond soon.
i18next is a popular internationalization framework for JavaScript. It provides a comprehensive solution for handling translations, including support for pluralization, context, and interpolation. Compared to messageformat, i18next offers a more extensive ecosystem with plugins and integrations for various frameworks and libraries.
react-intl is a library specifically designed for internationalization in React applications. It uses the ICU MessageFormat syntax similar to messageformat but is tightly integrated with React components. It provides components and hooks for formatting dates, numbers, and messages within React applications.
Globalize is a library for internationalization and localization in JavaScript. It provides support for formatting dates, numbers, and messages, as well as parsing and validating input. Globalize uses CLDR data for localization and offers a more data-driven approach compared to messageformat.
The experience and subtlety of your program's text can be important. MessageFormat is a mechanism for handling both pluralization and gender in your applications. It can also lead to much better translations, as it's designed to support all the languages included in the Unicode CLDR.
The ICU has an official guide for the format. Messageformat.js supports and extends all parts of the standard, with the exception of the deprecated ChoiceFormat.
There is a good slide-deck on Plural and Gender in Translated Messages by Markus Scherer and Mark Davis. But, again, remember that many of these problems apply even if you're only outputting english.
Please see messageformat.github.io for a guide to MessageFormat, more information on on the build-time use of messageformat.js, and the code documentation.
Using messageformat.js, you can separate your code from your text formatting, while enabling much more humane expressions. In other words, you won't need to see this anymore in your output:
There are 1 results.
There are 1 result(s).
Number of results: 5.
With this message:
> var msg =
'{GENDER, select, male{He} female{She} other{They} }' +
' found ' +
'{RES, plural, =0{no results} one{1 result} other{# results} }' +
' in the ' +
'{CAT, selectordinal, one{#st} two{#nd} few{#rd} other{#th} }' +
' category.';
You'll get these results:
> var mfunc = new MessageFormat('en').compile(msg);
> mfunc({ GENDER: 'male', RES: 1, CAT: 2 })
'He found 1 result in the 2nd category.'
> mfunc({ GENDER: 'female', RES: 1, CAT: 2 })
'She found 1 result in the 2nd category.'
> mfunc({ GENDER: 'male', RES: 2, CAT: 1 })
'He found 2 results in the 1st category.'
> mfunc({ RES: 2, CAT: 2 })
'They found 2 results in the 2nd category.'
npm install messageformat
var MessageFormat = require('messageformat');
var mf = new MessageFormat('en');
bower install messageformat
<script src="path/to/bower_components/messageformat/messageformat.js"></script>
<script>
var mf = new MessageFormat('en');
</script>
You may use this software under the MIT License.
You may contribute to this software under the Dojo CLA.
Thanks to:
Jeff Hansen (@jeffijoe) has written an implementation for .NET - it's a Portable Class Library, making it possible to use on iOS, Android, Windows Phone, and pretty much any other .NET target.
icu-converter is a NodeJS tool for converting message files in the ICU Resource Bundle format into JSON or .property files.
FAQs
PluralFormat and SelectFormat Message and i18n Tool - A JavaScript Implemenation of the ICU standards.
The npm package messageformat receives a total of 229,869 weekly downloads. As such, messageformat popularity was classified as popular.
We found that messageformat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.