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

icu-to-json

v0.0.20

Published

Compile ICU MessageFormat strings to JSON at build time and render them at runtime

Downloads

5

Readme

icu-to-json

Precompile i18n icu syntax translations to JSON at build time and render them at runtime

Build Time: compile("Hello {name}")["Hello ",["name"]]
Run Time: run( ["Hello ",["name"]], { name: "World"} )"Hello World"

icu-to-json logo

Goal

The main goal is to boot up javascript apps with ICU translations as fast as possible for the end user.

Therefore this library provides a way to compile ICU MessageFormat strings to compressed JSON at build time and render them at runtime with a minimal runtime footprint.

overhead size comparison

size of icu-to-json

Features

  1. Smaller Runtime Footprint
    The runtime footprint is only 1kb (minified and gzipped)
  2. No parsing at runtime
    The precompiled JSON can be rendered without any string parsing at runtime
  3. Flexible
    The runtime is able to not only return strings but also any object (aka rich text elements like JSX)
  4. Types
    The compiler has an optional feature to generate typescript types for the ICU messages and their arguments

animation showing type autocomplete
⚠️ the typed t() function is not part of this library. It is only an example how the generated types could be used.

Installation

npm install icu-to-json

Usage

Runtime

Pure Interpolations

import { run } from 'icu-to-json';

// e.g. precompiled icu messsage:
// "Hello {name}!"
run(precompiledMessage, "en", { name: 'World' })) // Hello, World!

Plurals and Selectordinal

import { run } from 'icu-to-json';

// e.g. precompiled icu messsage:
// "You have {count, plural, one {# unread message} other {# unread messages}}."
run(precompiledMessage, "en", { count: 1 })) // You have 1 unread message.

Tags

Tags can be used to wrap parts of the message.

import { run } from 'icu-to-json';

// e.g. precompiled icu messsage:
// "You have <b>{count}</b> messages."
run(precompiledMessage, "en", { count: 2, b: (content: number) => `**${number}**`})) // You have **2** messages.

Rich Text

The runtime is able to not only return strings but also richtext elements (e.g. JSX).
JSX is only used as an example here - it works with any other object as well.

import { evaluateAst } from 'icu-to-json';

// e.g. precompiled icu messsage:
// "You have <link><b>{count, plural, one {# unread message} other {# unread messages}}.</b></link>"
evaluateAst(precompiledMessage, "en", { 
    count: 1, 
    link: (content: string) => <a href="/messages">{content}</a>,
    b: (content: string) => <b>{content}</b>
}) // [ 'You have ', <a href="/messages"><b>1 unread message.</b></a> ]

Compile

CLI

The CLI can be used to compile ICU MessageFormat strings to JSON at build time.

# compile icu messages to json
icu-to-json src/messages.json dist/messages.json
# generate typescript types, locales and formatters from the icu messages
icu-to-json --types src/messages.json dist/messages.json

API

import { compile } from 'icu-to-json/compiler';

const precompiledMessage = compile('Hello {name}!');

Status

This library is still in early development and not yet ready for production use.

How does it work?

The main work is done by the @formatjs/icu-messageformat-parser, which parses the ICU MessageFormat strings and returns an AST. The AST is then traversed and compiled to JSON by icu-to-json.

License

MIT