https://github.com/locize/fluent-translation-parser
parses fluent translations to AST
https://github.com/locize/fluent-translation-parser
Last synced: 11 months ago
JSON representation
parses fluent translations to AST
- Host: GitHub
- URL: https://github.com/locize/fluent-translation-parser
- Owner: locize
- License: mit
- Created: 2018-09-19T10:05:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-03T07:53:22.000Z (over 3 years ago)
- Last Synced: 2025-06-07T07:04:52.683Z (12 months ago)
- Language: JavaScript
- Size: 632 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Introduction
[](https://travis-ci.org/i18next-translation-parser)
[](https://coveralls.io/github/locize/fluent-translation-parser)
[](https://www.npmjs.com/package/i18next-translation-parser)
[](https://david-dm.org/locize/fluent-translation-parser)
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](https://www.npmjs.com/package/fluent-translation-parser) or [downloaded](https://github.com/locize/fluent-chained-backend/blob/master/fluentTranslationParser.min.js) from this repo.
```
# npm package
$ npm install fluent-translation-parser
```
# Sample
```js
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);
```