intl-messageformat-parser

WebJar for intl-messageformat-parser

License

License

BSD 3-Clause
Categories

Categories

ORM Data
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

intl-messageformat-parser
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

intl-messageformat-parser
WebJar for intl-messageformat-parser
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/yahoo/intl-messageformat-parser

Download intl-messageformat-parser

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/intl-messageformat-parser/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>intl-messageformat-parser</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/intl-messageformat-parser/
implementation 'org.webjars.npm:intl-messageformat-parser:1.2.0'
// https://jarcasting.com/artifacts/org.webjars.npm/intl-messageformat-parser/
implementation ("org.webjars.npm:intl-messageformat-parser:1.2.0")
'org.webjars.npm:intl-messageformat-parser:jar:1.2.0'
<dependency org="org.webjars.npm" name="intl-messageformat-parser" rev="1.2.0">
  <artifact name="intl-messageformat-parser" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='intl-messageformat-parser', version='1.2.0')
)
libraryDependencies += "org.webjars.npm" % "intl-messageformat-parser" % "1.2.0"
[org.webjars.npm/intl-messageformat-parser "1.2.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Code has been moved to the FormatJS monorepo.

Intl MessageFormat Parser

Parses ICU Message strings into an AST via JavaScript.

npm Version Build Status Dependency Status

Overview

This package implements a parser in JavaScript that parses the industry standard ICU Message strings — used for internationalization — into an AST. The produced AST can then be used by a compiler, like intl-messageformat, to produce localized formatted strings for display to users.

This parser is written in PEG.js, a parser generator for JavaScript. This parser's implementation was inspired by and derived from Alex Sexton's messageformat.js project. The differences from Alex's implementation are:

  • This project is standalone.
  • It's authored as ES6 modules compiled to CommonJS and the Bundle format for the browser.
  • The produced AST is more descriptive and uses recursive structures.
  • The keywords used in the AST match the ICU Message "spec".

Usage

Loading in the Browser

The dist/ folder contains the version of this package for use in the browser, and it can be loaded and used like this:

<script src="intl-messageformat-parser/dist/parser.min.js"></script>
<script>
    IntlMessageFormatParser.parse('...');
</script>

Loading in Node.js

This package can also be require()-ed in Node.js:

var parser = require('intl-messageformat-parser');
parser.parse('...');

Example

Given an ICU Message string like this:

On {takenDate, date, short} {name} took {numPhotos, plural,
    =0 {no photos.}
    =1 {one photo.}
    other {# photos.}
}
// Assume `msg` is the string above.
parser.parse(msg);

This parser will produce this AST:

{
    "type": "messageFormatPattern",
    "elements": [
        {
            "type": "messageTextElement",
            "value": "On "
        },
        {
            "type": "argumentElement",
            "id": "takenDate",
            "format": {
                "type": "dateFormat",
                "style": "short"
            }
        },
        {
            "type": "messageTextElement",
            "value": " "
        },
        {
            "type": "argumentElement",
            "id": "name",
            "format": null
        },
        {
            "type": "messageTextElement",
            "value": " took "
        },
        {
            "type": "argumentElement",
            "id": "numPhotos",
            "format": {
                "type": "pluralFormat",
                "offset": 0,
                "options": [
                    {
                        "type": "optionalFormatPattern",
                        "selector": "=0",
                        "value": {
                            "type": "messageFormatPattern",
                            "elements": [
                                {
                                    "type": "messageTextElement",
                                    "value": "no photos."
                                }
                            ]
                        }
                    },
                    {
                        "type": "optionalFormatPattern",
                        "selector": "=1",
                        "value": {
                            "type": "messageFormatPattern",
                            "elements": [
                                {
                                    "type": "messageTextElement",
                                    "value": "one photo."
                                }
                            ]
                        }
                    },
                    {
                        "type": "optionalFormatPattern",
                        "selector": "other",
                        "value": {
                            "type": "messageFormatPattern",
                            "elements": [
                                {
                                    "type": "messageTextElement",
                                    "value": "# photos."
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.

org.webjars.npm

Yahoo

Yahoo is a Verizon Media brand. This organization is the home to many of the active open source projects published by engineers at Yahoo and Verizon Media.

Versions

Version
1.2.0
1.1.0