fluent-translation-parser

1.0.0 • Public • Published

Introduction

Travis Coveralls npm version David

This is a module to parse an i18next translation string into an AST and back to a string.

Getting started

Source can be loaded via npm or downloaded from this repo.

# npm package
$ npm install fluent-translation-parser

Sample

import { parse, stringify } from "fluent-translation-parser";
 
const AST = parse(`
    { $unreadEmails ->
        [one] You have one unread email.
       *[other] You have { $unreadEmails } unread emails.
    }
`);
// will return
/*
[
  {
    "type": "text",
    "content": "    { $unreadEmails ->\n        [one] You have one unread email.\n       *[other] You have { $unreadEmails } unread emails.\n    }",
    "children": [
      {
        "type": "text",
        "content": "    "
      },
      {
        "type": "selector",
        "raw": "{ $unreadEmails ->",
        "prefix": "{",
        "suffix": "->",
        "content": " $unreadEmails ",
        "variable": "unreadEmails"
      },
      {
        "type": "text",
        "content": "\n        "
      },
      {
        "type": "variant",
        "isDefault": false,
        "raw": "[one]",
        "prefix": "[",
        "suffix": "]",
        "content": "one",
        "variable": "one"
      },
      {
        "type": "text",
        "content": " You have one unread email.\n       "
      },
      {
        "type": "variant",
        "isDefault": true,
        "raw": "*[other]",
        "prefix": "*[",
        "suffix": "]",
        "content": "other",
        "variable": "other"
      },
      {
        "type": "text",
        "content": " You have "
      },
      {
        "type": "variable",
        "raw": "{ $unreadEmails }",
        "prefix": "{",
        "suffix": "}",
        "content": " $unreadEmails ",
        "variable": "unreadEmails"
      },
      {
        "type": "text",
        "content": " unread emails.\n    "
      },
      {
        "type": "closingBracket",
        "raw": "}",
        "prefix": "",
        "suffix": "}",
        "content": ""
      },
      {
        "type": "text",
        "content": ""
      }
    ]
  }
]
*/
stringify(AST);

Readme

Keywords

Package Sidebar

Install

npm i fluent-translation-parser

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

2.18 MB

Total Files

542

Last publish

Collaborators

  • jamuhl