@ember-intl/intl-messageformat-parser

中文文档
1.5.0发布于 2018/11/05BSD-3-ClauseCommonJS零依赖近 30 天下载 2.5万

Parses ICU Message strings into an AST via JavaScript.

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录6 节
  1. Overview
  2. Usage
  3. Loading in the Browser
  4. Loading in Node.js
  5. Example
  6. License

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.

版本与发布记录

@ember-intl/intl-messageformat-parser 共发布 4 个版本,首次发布于 2017/08/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.0latest2018/11/058 年前
1.4.02018/10/098 年前
2.0.12017/08/249 年前
2.0.02017/08/249 年前

关于 @ember-intl/intl-messageformat-parser 的常见问题

@ember-intl/intl-messageformat-parser 是什么?

Parses ICU Message strings into an AST via JavaScript.注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @ember-intl/intl-messageformat-parser?

在项目目录运行 npm install @ember-intl/intl-messageformat-parser,或使用 pnpm add / yarn add / bun add。

@ember-intl/intl-messageformat-parser 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ember-intl__intl-messageformat-parser 是否存在。

@ember-intl/intl-messageformat-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ember-intl/intl-messageformat-parser 可以商用吗?

@ember-intl/intl-messageformat-parser 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@ember-intl/intl-messageformat-parser 的下载量有多少?

根据 npm 官方下载统计,@ember-intl/intl-messageformat-parser 在 2026/10/14 – 2026/11/12 这 30 天内被下载 25,130 次,日均约 838 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ember-intl/intl-messageformat-parser 还在维护吗?

最新版本 1.5.0 发布于 2018/11/05(8 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/08/24。作者已将其标记为弃用,新项目建议寻找替代方案。