GettextOps.Output (gettext_ops v0.1.1)

View Source

Output formatting functions for gettext messages.

This module provides consistent formatting for both plain text (.po format) and JSON output. JSON output uses line-delimited format (one JSON object per line) for easy piping to LLMs and other tools.

Summary

Functions

Formats a message as JSON.

Formats a message as plain text in .po file format.

Prints a message to stdout in the specified format.

Prints multiple messages with appropriate separator.

Converts a message to a map for JSON encoding.

Functions

format_json(message)

@spec format_json(Expo.Message.t()) :: String.t()

Formats a message as JSON.

Returns a JSON string (single line) containing msgid, msgstr, and optional references, comments, and flags.

Examples

iex> message = %Expo.Message.Singular{msgid: ["Sign In"], msgstr: [""]}
iex> GettextOps.Output.format_json(message) |> JSON.decode!()
%{"msgid" => "Sign In", "msgstr" => ""}

# With references (JSON key order may vary):
message = %Expo.Message.Singular{msgid: ["Welcome"], msgstr: ["Välkommen"], references: [[{"lib/home.ex", 5}]]}
GettextOps.Output.format_json(message)
# => JSON string containing: "msgid":"Welcome", "msgstr":"Välkommen", "references":["lib/home.ex:5"]

format_text(message)

@spec format_text(Expo.Message.t()) :: String.t()

Formats a message as plain text in .po file format.

Returns a string representation of the message that matches the .po file format, including msgid and msgstr lines.

Examples

iex> message = %Expo.Message.Singular{msgid: ["Sign In"], msgstr: [""]}
iex> GettextOps.Output.format_text(message)
"msgid \"Sign In\"\nmsgstr \"\"\n"

iex> message = %Expo.Message.Singular{msgid: ["Welcome"], msgstr: ["Välkommen"]}
iex> GettextOps.Output.format_text(message)
"msgid \"Welcome\"\nmsgstr \"Välkommen\"\n"

to_map(message)

@spec to_map(Expo.Message.t()) :: map()

Converts a message to a map for JSON encoding.

The map includes:

  • msgid: The message ID
  • msgstr: The message string (for plurals, the first form)
  • msgctxt: The message context (only if the entry has one)
  • references: List of "file:line" strings (if present)
  • comments: List of comment strings (if present)
  • flags: List of flag strings (if present)

Examples

iex> message = %Expo.Message.Singular{msgid: ["Hello"], msgstr: ["Hej"]}
iex> GettextOps.Output.to_map(message)
%{msgid: "Hello", msgstr: "Hej"}

iex> message = %Expo.Message.Singular{
...>   msgid: ["Sign In"],
...>   msgstr: [""],
...>   references: [[{"lib/auth.ex", 12}]]
...> }
iex> GettextOps.Output.to_map(message)
%{msgid: "Sign In", msgstr: "", references: ["lib/auth.ex:12"]}

iex> message = %Expo.Message.Singular{
...>   msgid: ["Active"],
...>   msgstr: ["Giltig"],
...>   msgctxt: ["token status"]
...> }
iex> GettextOps.Output.to_map(message)
%{msgid: "Active", msgstr: "Giltig", msgctxt: "token status"}