POMO — PO/MO Translation File Handling

WordPress gettext-based internationalization subsystem for reading, writing, and managing PO and MO translation files.

Since: 2.8.0 Source: wp-includes/pomo/

Components

ComponentDescription
class-translation-entry.mdEncapsulates a single translatable string
class-translations.mdBase class: a set of translation entries with headers
class-gettext-translations.mdGettext-aware translations with plural form parsing
class-noop-translations.mdNo-op implementation (passthrough, no actual translation)
class-mo.mdMO (Machine Object) binary file reader/writer
class-po.mdPO (Portable Object) text file reader/writer
class-plural-forms.mdPlural-Forms expression parser and evaluator
streams.mdStream reader classes for binary file I/O

Architecture

Translations (2.8.0)
    ├── entries[], headers[]
    ├── translate(), translate_plural()
    └── add_entry(), merge_with()

Gettext_Translations (2.8.0) extends Translations
    ├── Plural-Forms header parsing
    ├── make_plural_form_function() → Plural_Forms
    └── make_headers() from raw translation string

MO (2.8.0) extends Gettext_Translations
    ├── import_from_file() / import_from_reader()
    ├── export() / export_to_file()
    └── Binary MO format: magic, revision, string tables

PO (2.8.0) extends Gettext_Translations
    ├── import_from_file() — line-by-line PO parser
    ├── export() / export_to_file()
    └── poify() / unpoify() — PO string escaping

NOOP_Translations (2.8.0)
    └── Same interface as Translations, all methods are no-ops

Translation_Entry (2.8.0)
    ├── singular, plural, context, translations[]
    ├── key() — generates lookup key (contextx04singular)
    └── merge_with() — merges flags, references, comments

Plural_Forms (4.9.0)
    ├── Shunting-yard parser → Reverse Polish Notation
    ├── execute() — stack-based RPN evaluator
    └── get() — cached plural form lookup

POMO_Reader → POMO_FileReader (file handle I/O)
           → POMO_StringReader (in-memory string I/O)
               → POMO_CachedFileReader (file read into memory)
                   → POMO_CachedIntFileReader

Key Concepts

Translation Lookup

Translations are keyed by a combination of context and singular string. The key format is:

  • Without context: singular
  • With context: contextx04singular (EOT character as separator)

Line endings are normalized to n in keys.

MO File Format

MO files are binary, containing:

  1. Magic number (0x950412de) — also determines byte order
  2. Revision — only revision 0 is supported
  3. String count and table offsets
  4. Original strings table — length/offset pairs
  5. Translation strings table — length/offset pairs
  6. Hash table (optional, skipped on read)
  7. String data — null-terminated strings

Context is separated by x04, plural forms by x00.

PO File Format

PO files are text-based with entries like:

po
# translator comment
#. extracted comment
#: file.php:42
#, php-format
msgctxt "context"
msgid "singular"
msgid_plural "plural"
msgstr[0] "translation"
msgstr[1] "translations"

Plural Forms

The Plural-Forms header (e.g., nplurals=2; plural=n != 1;) is parsed by the Plural_Forms class using the shunting-yard algorithm to convert the C-like expression to RPN, then evaluated with a stack-based interpreter. Results are cached per-number.

Constants

ConstantValueSource
PO_MAX_LINE_LEN79po.php

File Map

FileClasses
entry.phpTranslation_Entry
translations.phpTranslations, Gettext_Translations, NOOP_Translations
mo.phpMO
po.phpPO
plural-forms.phpPlural_Forms
streams.phpPOMO_Reader, POMO_FileReader, POMO_StringReader, POMO_CachedFileReader, POMO_CachedIntFileReader