This is a module to parse an i18next translation string into an AST and back to a string.
Source can be loaded via npm or downloaded from this repo.
# npm package
$ npm install fluent-translation-parser
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);