# format-message-interpret

> Convert format-message-parse ast to a function

Latest version **6.2.4** (published 2022-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install format-message-interpret
pnpm add format-message-interpret
yarn add format-message-interpret
bun add format-message-interpret
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 6.2.4 |
| Published | 2022-02-17 |
| First published | 2015-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 29.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 204 |
| Author | Andy VanWagoner |
| Maintainers | thetalecrafter, bjohn465, aaronshaf, brentburgoyne |
| Keywords | format-message, message-format, internationalization, i18n, icu, messageformat, pluralformat, selectformat |

## Links

- npm: https://www.npmjs.com/package/format-message-interpret
- Repository: https://github.com/format-message/format-message
- Homepage: https://github.com/format-message/format-message/tree/master/packages/format-message-interpret
- Issues: https://github.com/format-message/format-message/issues
- npm.io page: https://npm.io/package/format-message-interpret

## Dependencies (2)

- [lookup-closest-locale](https://npm.io/package/lookup-closest-locale.md) ^6.2.0
- [format-message-formats](https://npm.io/package/format-message-formats.md) ^6.2.4

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 6.2.4 (latest) — 2022-02-17
- 6.0.0-alpha.2 (next) — 2018-04-18
- 6.2.3 — 2019-07-04
- 6.2.2 — 2019-07-04
- 6.2.0 — 2018-09-12
- 6.1.0 — 2018-08-30
- 6.0.4 — 2018-06-05
- 6.0.3 — 2018-04-27
- 6.0.0 — 2018-04-19
- 6.0.0-alpha.1 — 2018-04-17
- 6.0.0-alpha.0 — 2018-04-16
- 5.2.1 — 2017-11-03
- 5.1.2 — 2017-03-23
- 5.1.0 — 2016-12-28
- 5.0.0 — 2016-09-27
- … 5 more at https://npm.io/package/format-message-interpret/versions

## README

# ![format-message-interpret][logo]

> Convert format-message-parse ast to a function

[![npm Version][npm-image]][npm]
[![JS Standard Style][style-image]][style]
[![MIT License][license-image]][LICENSE]

Turns a compact format-message ast:
```js
[ "You have ", [ "numBananas", "plural", 0, {
     "=0": [ "no bananas" ],
    "one": [ "a banana" ],
  "other": [ [ '#' ], " bananas" ]
} ], " for sale." ]
```

into a function:
```js
format({ numBananas:0 })
//-> "You have no bananas for sale."
```

Quick Examples
--------------

`npm install format-message-interpret --save`

```js
import parse from 'format-message-parse'
import interpret from 'format-message-interpret'

interpret('en', parse('Hello, {name}!'))({ name: 'Jane' })
```

API
---

### `interpret(locales: string | string[], ast: AST, types?: Types)`

Generate a function from an `ast`, using the formatting rules of the `locales` that accepts an arguments object, and returns a string. You can optionally pass custom `types`. Any non-standard type found in `ast` without a corresponding formatter in `types` will be treated as a simple string type.

```js
type Types = {
  [type: string]: (placeholder: string[], locales: string | string[]) =>
    (value: any, args: Object) => string
}
```

`types` is an object with each key being the name of the type as it appears in the message pattern. Each value is a function that takes the `locales`, and the `node` from the ast (like `[ 'a', 'mytype', 'style' ]`), and it returns a function that will be called with the specific `value`, and the complete arguments object. If the custom type was defined with sub-messages, those will already be converted to functions meant to be called with `args`.

### `interpret.toParts(locales: string | string[], ast: AST, types?: Types)`

Like, `interpret`, `interpretToParts` will generate a function accepting the message arguments. However, it will return an array of message parts, instead of a string. This is intended to help generate rich messages.

```js
interpret.toParts('en', parse('a {b} c'))({ b: 1 }) // [ 'a ', 1, ' c' ]
interpret.toParts('en', parse('Click {a, element, children {here}}'), {
  element: (locales, [ id, type, props ]) =>
    (fn, args) => fn(props.children(args))
})({ a: children => <a>{children}</a> })
// [ 'Click ', <a>here</a> ]
```

License
-------

This software is free to use under the MIT license. See the [LICENSE-MIT file][LICENSE] for license text and copyright information.


[logo]: https://format-message.github.io/format-message/logo.svg
[npm]: https://www.npmjs.org/package/format-message-interpret
[npm-image]: https://img.shields.io/npm/v/format-message-interpret.svg
[style]: https://github.com/feross/standard
[style-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[license-image]: https://img.shields.io/npm/l/format-message.svg
[LICENSE]: https://github.com/format-message/format-message/blob/master/LICENSE-MIT

---
_Source: https://npm.io/package/format-message-interpret · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
