PO files look like plain text you could fix in Notepad. That is exactly why they break.

A plugin ships its Spanish release and a third of the interface is still in English. The translator swears everything was translated, and they are right. Forty entries were marked as fuzzy, and the compiler skipped every one of them without saying a word.

That is the PO format in one story: simple enough for anyone to open, with a few rules that punish you quietly if you ignore them. I have worked with PO and POT resources since my years localizing desktop software, and the same handful of problems keeps coming back.

Where PO files come from

PO files belong to gettext, the localization system that grew out of the Unix world in the early 1990s. The GNU implementation, started in 1995, is the one that won. Today you find PO files behind Django, WordPress, GNOME, KDE, countless Python and C projects, and plenty of commercial software too.

The workflow uses three kinds of file:

  • POT (Portable Object Template): every translatable string extracted from the source code, with empty translations.
  • PO (Portable Object): one per language, a copy of the template with the translations filled in.
  • MO (Machine Object): the compiled binary the application actually loads at runtime.

Translators work on the PO. The program never reads it: it reads the MO. Keep that in mind, because it explains most of the surprises below.

Anatomy of an entry

pl.po
#. Shown in the cart summary. %(count)d is the number of files.
#: src/cart.py:42
#, python-format
msgid "%(count)d file"
msgid_plural "%(count)d files"
msgstr[0] "%(count)d plik"
msgstr[1] "%(count)d pliki"
msgstr[2] "%(count)d plików"

Every line has a job:

  • #. Extracted comment: written by the developer in the code, for the translator. The best context you will ever get, when developers bother to write it.
  • #: Reference: where the string lives in the source code.
  • #, Flags: python-format, c-format and friends tell tools which placeholder syntax to check. fuzzy lives here too.
  • # followed by a space: translator comment. Notes from the translator, preserved by the tools.
  • #| Previous source: what the msgid used to say before it changed. Gold when you review fuzzy entries.
  • msgctxt: context that tells apart two identical strings (more on that below).
  • msgid and msgid_plural: the source text, singular and plural.
  • msgstr or msgstr[n]: the translation, or one per plural form.

The header is not decoration

The first entry of every PO file has an empty msgid. Its msgstr is the header, and three of its lines matter more than anything else in the file:

pl.po
msgid ""
msgstr ""
"Language: pl\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"

charset tells every tool how to read the bytes. Templates often arrive with charset=CHARSET, a placeholder somebody is supposed to replace. When nobody does, you get encoding problems that look like translation errors.

Plural-Forms is a small program: how many forms the language has, and a C expression that picks the right one for any number.

Plural forms, the part everyone underestimates

English has two forms. Spanish has two. Japanese has one. Polish has three, and the rule is not “1, 2 to 4, 5 and up”. It is “1, numbers ending in 2 to 4 except 12 to 14, everything else”. So 22 files are “pliki” but 12 are “plików”. Arabic has six forms.

You do not have to memorize any of this, but you do have to test it. This compiles the Polish entry above to MO with polib and loads it with Python’s own gettext module:

plurals.py
import gettext
import polib

polib.pofile("pl.po").save_as_mofile("pl.mo")
with open("pl.mo", "rb") as f:
    t = gettext.GNUTranslations(f)

for n in (1, 2, 5, 12, 22, 25):
    print(t.ngettext("%(count)d file", "%(count)d files", n) % {"count": n})
vicparra@loc-engineer:~
1 plik
2 pliki
5 plików
12 plików
22 pliki
25 plików

Two things go wrong in practice. A PO file ends up with fewer msgstr[n] entries than its header announces, often after a round trip through a tool that only knows “singular” and “plural”. Or translators treat the plural slots as if they were the singular and plural of their own language. Both stay invisible until somebody orders 22 of something.

Context: the same word, two meanings

“Open” can be a verb on a button or an adjective in a status column. In many languages those are two different words. msgctxt lets developers tell them apart:

pl.po
msgctxt "verb"
msgid "Open"
msgstr "Otwórz"

msgctxt "adjective"
msgid "Open"
msgstr "Otwarte"

Without context, both collapse into one entry and somebody ships the adjective on a button. If you work with developers, asking for pgettext() instead of gettext() on short, ambiguous strings is one of the cheapest quality improvements you can get.

The gettext workflow, and where fuzzy entries come from

With the GNU tools, the cycle looks like this:

vicparra@loc-engineer:~
xgettext --language=Python --keyword=_ --output=messages.pot src/*.py   # extract strings
msginit --input=messages.pot --locale=pl --output=pl.po                 # start a new language
msgmerge --update --previous pl.po messages.pot                         # the source changed
msgfmt --check --statistics --output-file=pl.mo pl.po                   # compile

msgmerge is where fuzzy entries are born. When a source string changes slightly, gettext does not throw the old translation away: it attaches it to the new string and marks it #, fuzzy so a human can check it. With --previous it also keeps the old source in #| lines, so the reviewer can see exactly what changed.

The trap is that fuzzy entries are not compiled. msgfmt leaves them out unless you force it with --use-fuzzy, and the application falls back to the source text. The translation is in the file, the translator can see it, and the user never does:

vicparra@loc-engineer:~
>>> t.gettext("Delete %d files?")   # still fuzzy in pl.po
'Delete %d files?'

Review fuzzy entries, or treat them as untranslated. Never deliver them as if they were finished.

Escapes and long lines

PO strings are C strings. Inside them, a double quote is \", a backslash is \\ and a line break is \n. Long strings can be split across several lines, which are joined when the file is read:

es.po
msgid ""
"Your session has expired. "
"Please sign in again."
msgstr ""
"Tu sesión ha caducado. "
"Vuelve a iniciar sesión."

That first msgid "" is not empty: it is the start of a string that continues on the next lines. Two habits save a lot of trouble. Keep the same \n structure as the source, because msgfmt —check complains when a msgid and its msgstr do not begin and end the same way. And never type a straight double quote inside a translation without escaping it. Typographic quotes (“ ” or « ») need no escaping at all.

What I check before a PO file goes back

msgfmt --check is the first line of defence: it validates the header, the number of plural forms and, thanks to the *-format flags, the placeholders. When I cannot run it, or I want a report instead of a failed build, a few lines of Python with polib cover the rest:

check_po.py
import re
import polib

def check_plurals(path):
    po = polib.pofile(path)
    match = re.search(r"nplurals\s*=\s*(\d+)", po.metadata.get("Plural-Forms", ""))
    if not match:
        return ["Header has no Plural-Forms"]
    nplurals = int(match.group(1))
    problems = []
    for entry in po:
        if entry.msgid_plural and len(entry.msgstr_plural) != nplurals:
            problems.append(f"Line {entry.linenum}: {len(entry.msgstr_plural)} plural forms, header says {nplurals}")
    return problems

Combine it with the untranslated, fuzzy and placeholder checks from my article on localization file validation and you have a gate that catches almost everything. If you need a quick answer without installing anything, my free Master File Validator checks PO placeholders in your browser.

Tools for editing PO files

  • Poedit: the classic dedicated editor, cross-platform and actively maintained. It validates when you save and compiles the MO for you.
  • Lokalize: KDE’s translation tool, a natural fit if you work on Linux.
  • Weblate: an open-source web platform connected to Git, good for continuous localization of open-source projects.
  • Any CAT tool: Trados, memoQ, Phrase and OmegaT read PO files through their filters, with your translation memories and termbases.

I used to recommend Virtaal as well. It has not been updated in more than a decade, so I would not start a project on it today.

Poedit editing a PO file, with the source text, the translation and the list of entries

PO, JSON or XLIFF?

POJSONXLIFF
PluralsBuilt in, per-language rulesLibrary conventions (i18next, ICU)Not built in
Context and commentsBuilt in (msgctxt, #.)Not in the standardNotes and metadata
Typical homegettext projects: Django, WordPress, LinuxWeb and mobile appsExchange between localization tools
Easy to break by handModeratelyVeryVery

PO is the most translation-friendly of the three: plurals, context and comments are part of the format, not conventions bolted on later. JSON is everywhere in web development, and it has its own guide: JSON for localization.

The short version

  • Translators edit the PO, applications read the MO. If it is not in the MO, it does not exist.
  • Fuzzy means “not translated” until a human says otherwise.
  • Test plural forms with real numbers, including 12 and 22.
  • Ask developers for context (msgctxt) on short, ambiguous strings.
  • Run msgfmt --check before anything ships.