GettextOps.Output (gettext_ops v0.1.1)
View SourceOutput 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
@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"]
@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"
@spec print_message(Expo.Message.t(), :text | :json) :: :ok
Prints a message to stdout in the specified format.
Examples
message = %Expo.Message.Singular{msgid: ["Hello"], msgstr: ["Hej"]}
GettextOps.Output.print_message(message, :text)
# Outputs: msgid "Hello"\nmsgstr "Hej"\n
GettextOps.Output.print_message(message, :json)
# Outputs: {"msgid":"Hello","msgstr":"Hej"}
@spec print_messages([Expo.Message.t()], :text | :json) :: :ok
Prints multiple messages with appropriate separator.
For text format, adds a blank line between messages. For JSON format, outputs one JSON object per line (line-delimited JSON).
Examples
messages = [
%Expo.Message.Singular{msgid: ["Hello"], msgstr: ["Hej"]},
%Expo.Message.Singular{msgid: ["Goodbye"], msgstr: ["Hejdå"]}
]
GettextOps.Output.print_messages(messages, :text)
# Outputs: msgid "Hello"\nmsgstr "Hej"\n\nmsgid "Goodbye"\nmsgstr "Hejdå"\n
GettextOps.Output.print_messages(messages, :json)
# Outputs: {"msgid":"Hello","msgstr":"Hej"}\n{"msgid":"Goodbye","msgstr":"Hejdå"}
@spec to_map(Expo.Message.t()) :: map()
Converts a message to a map for JSON encoding.
The map includes:
msgid: The message IDmsgstr: 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"}