npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@messageformat/convert

v1.0.0

Published

Convert input (e.g. from Rails i18n) into messageformat-compatible JSON

Downloads

2,585

Readme

@messageformat/convert

Converts hiearachical objects into messageformat-compatible JSON. More specifically, it:

  • Detects two-letter locale keys and parses their contents according to that locale's pluralisation rules
  • Converts #{var} and %{var} variable replacements into their messageformat equivalents, while properly escaping other {} characters.
  • Detects objects encoding pluralisation choices (using the locale-specific set of CLDR categories, i.e. zero|one|two|few|many|aother) and converts them to their messageformat equivalents.

With these conversions, messages stored according to the Rails i18n spec may be used together with messageformat.

This package was previously named messageformat-convert.

Installation

npm install @messageformat/convert

If using in an environment that does not natively support ES6 features such as object destructuring and arrow functions, you'll want to use a transpiler for this.

Usage

const convert = require('@messageformat/convert');
const { locales, translations } = convert({
  en: {
    format: '%{attribute} %{message}',
    errors: {
      confirmation: "doesn't match %{attribute}",
      accepted: 'must be accepted',
      wrong_length: {
        one: 'is the wrong length (should be 1 character)',
        other: 'is the wrong length (should be %{count} characters)'
      },
      equal_to: 'must be equal to %{count}'
    }
  }
});

const MessageFormat = require('@messageformat/core');
const mf = new MessageFormat(locales);
const messages = mf.compile(translations);

messages.en.errors.accepted();
// 'must be accepted'

messages.en.format({
  attribute: 'Problem',
  message: messages.en.errors.confirmation({ attribute: 'your style' })
});
// 'Problem doesn\'t match your style'

messages.en.errors.wrong_length({ count: 42 });
// 'is the wrong length (should be 42 characters)'

API: convert(data, options)

data should be a hierarchical object of strings; options is an optional set of configuration:

  • defaultLocale (string, default 'en') – Sets the initial locale.

  • includeLocales (array of strings, default null) – By default any key in the input data that matches the two-letter code of a CLDR pluralisation language switches the language used for determining the pluralisation rules. Set this to some limited set of languages (or even an empty array) to limit that.

  • verbose (boolean, default false) – If set to true, some logging and warnings will be printed to the console.

For more options, take a look at the source.

The object returned by the function contains the following fields:

  • locales (array of strings) – The actual locales encountered in the data

  • translations (object) – An object containing the MessageFormat strings, matching the shape of the input data


Messageformat is an OpenJS Foundation project, and we follow its Code of Conduct.