Fluent Syntax Parser

0.19.0 · active · verified Sat Apr 11

Fluent.syntax is the Python implementation for parsing, analyzing, processing, and serializing Fluent Translation List (FTL) files. It provides tools for working with the Abstract Syntax Tree (AST) of Fluent messages. As part of the broader Project Fluent, it helps unleash the expressive power of natural language in localization. The library is currently at version 0.19.0 and receives updates as part of the `python-fluent` project, with releases tied to Fluent specification updates and Python ecosystem changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing a Fluent Translation List (FTL) string into an Abstract Syntax Tree (AST) using `FluentParser` and then serializing the AST back into an FTL string using `FluentSerializer`. This showcases the core functionality of reading and writing Fluent syntax programmatically.

from fluent.syntax.parser import FluentParser
from fluent.syntax.serializer import FluentSerializer

ftl_source = """
# Welcome message
hello = Hello, { $username }!

# Plural example
num-emails = { $num ->
    [one] You have one email.
   *[other] You have { $num } emails.
}
"""

parser = FluentParser()
resource = parser.parse(ftl_source)

print("--- Parsed AST ---")
# For a more readable AST, you might want to use a custom visitor or ast.dump
# For simplicity, we'll just print the resource object's representation.
print(resource)

serializer = FluentSerializer()
serialized_ftl = serializer.serialize(resource)

print("\n--- Serialized FTL ---")
print(serialized_ftl)

view raw JSON →