Pygments Lexer for Fluent

1.0 · active · verified Thu Apr 16

The `fluent-pygments` library provides a Pygments lexer for Fluent (FTL), a powerful localization system designed for natural language. It enables syntax highlighting of Fluent files, particularly useful within documentation generators like Sphinx that leverage Pygments for code rendering. It is a component of the broader `python-fluent` project, which also includes `fluent.syntax` and `fluent.runtime`.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `FluentLexer` with Pygments to highlight Fluent (FTL) code. It shows how to output the highlighted code both as a complete HTML document and as colored text for a 256-color terminal. The HTML output typically requires a separate CSS stylesheet for proper rendering, which can be generated using `pygmentize -S <style_name> -f html > style.css`.

from pygments import highlight
from pygments.formatters import HtmlFormatter, Terminal256Formatter
from fluent_pygments.lexer import FluentLexer

ftl_code = """
### A resource comment for the whole file
my-key = Localize { -brand-name }
-brand-name = Fluent
# $num is the number of strings to localize
plurals = { $num ->
    [one] One string
   *[other] {$num} strings
}
"""

# Highlight to HTML
html_formatter = HtmlFormatter(full=True, style='default')
highlighted_html = highlight(ftl_code, FluentLexer(), html_formatter)

# To see output, you might write to a file or print the raw HTML
# print(highlighted_html)

# Highlight to terminal with 256 colors
terminal_formatter = Terminal256Formatter(style='monokai')
highlighted_terminal = highlight(ftl_code, FluentLexer(), terminal_formatter)

print("\n--- HTML Output Snippet (not runnable directly) ---\n")
print(highlighted_html[:500] + "...") # Print a snippet for brevity
print("\n--- Terminal Output ---\n")
print(highlighted_terminal)

view raw JSON →