localization_utils¶
localization_utils
¶
Localization file parsing and serialization utilities.
Supports PO/POT (GNU Gettext) and XLIFF/XLF (OASIS, versions 1.2 and 2.0).
Each format has a parse/serialize pair. The unified parse_localization
/ serialize_localization dispatchers select the correct pair based on
file extension.
LocalizationEntry
dataclass
¶
A single translatable localization string.
| ATTRIBUTE | DESCRIPTION |
|---|---|
index |
Sequential position (0-based).
TYPE:
|
msgid |
Source text (the string to translate).
TYPE:
|
msgstr |
Current translation (may be empty).
TYPE:
|
context |
Optional disambiguation context.
TYPE:
|
metadata |
Format-specific extra data (comments, flags, plurals, etc.).
TYPE:
|
is_localization_format
¶
_unescape_po
¶
Unescapes PO/Gettext C-style escape sequences.
Source code in src/utils/localization_utils.py
_escape_po
¶
Escapes a string for PO/Gettext output.
Source code in src/utils/localization_utils.py
_parse_po_block
¶
Parses a single PO block into comments and keyword values.
| PARAMETER | DESCRIPTION |
|---|---|
lines
|
Raw text lines of a single PO block.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
Tuple of (comments, keywords) where comments is a list of |
dict[str, str]
|
|
tuple[list[str], dict[str, str]]
|
unescaped string values. |
Source code in src/utils/localization_utils.py
_extract_po_flags
¶
Extracts PO flags from #, comment lines.
Source code in src/utils/localization_utils.py
parse_po
¶
Parses a PO/POT file into localization entries.
The header entry (first entry with empty msgid) is preserved
verbatim in header_lines and excluded from the returned entries.
| PARAMETER | DESCRIPTION |
|---|---|
content
|
Raw PO/POT file content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[list[LocalizationEntry], list[str]]
|
Tuple of (entries, header_lines). |
Source code in src/utils/localization_utils.py
serialize_po
¶
Reconstructs a PO file from entries and the original header.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
Localization entries with translated text.
TYPE:
|
header_lines
|
Original header block (preserved verbatim).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Complete PO file content. |
Source code in src/utils/localization_utils.py
_detect_xliff_version
¶
Detects XLIFF version from the root element.
| PARAMETER | DESCRIPTION |
|---|---|
root
|
Parsed XML root element.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/utils/localization_utils.py
_parse_xliff_12
¶
Parses XLIFF 1.2 trans-units into entries.
Source code in src/utils/localization_utils.py
_inject_xliff_12
¶
Injects translations into XLIFF 1.2 tree in-place.
Source code in src/utils/localization_utils.py
_parse_xliff_20
¶
Parses XLIFF 2.0 units/segments into entries.
Source code in src/utils/localization_utils.py
_inject_xliff_20
¶
Injects translations into XLIFF 2.0 tree in-place.
Source code in src/utils/localization_utils.py
parse_xliff
¶
Parses an XLIFF file (1.2 or 2.0) into localization entries.
| PARAMETER | DESCRIPTION |
|---|---|
content
|
Raw XLIFF file content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[LocalizationEntry]
|
Tuple of (entries, root_element). The root element is the |
Element
|
parsed XML tree that will be modified in-place for serialization. |
Source code in src/utils/localization_utils.py
serialize_xliff
¶
Reconstructs an XLIFF file by injecting translations into the tree.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
Localization entries with translated text.
TYPE:
|
root
|
Parsed XML root element from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Complete XLIFF file content. |
Source code in src/utils/localization_utils.py
parse_localization
¶
Dispatches to the format-specific localization parser.
| PARAMETER | DESCRIPTION |
|---|---|
content
|
Raw file content.
TYPE:
|
suffix
|
Lowercase file extension (e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[LocalizationEntry]
|
Tuple of (entries, format_data) where format_data is |
object
|
whatever the format-specific serializer needs. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the extension is not a supported localization format. |
Source code in src/utils/localization_utils.py
serialize_localization
¶
Dispatches to the format-specific localization serializer.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
Localization entries with translated text.
TYPE:
|
format_data
|
Format-specific data from
TYPE:
|
suffix
|
Lowercase file extension.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Complete file content. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the extension is not a supported localization format. |