Konrad
Konrad

Reputation: 7237

Benefits of compiling po files to mo

What's the benefit and primary reason for compiling GNU gettext .po (Portable Object) files to .mo (Machine Object) ?

I saw many programs reading/parsing .po directly.

I'm not using wordpress but on their docs it says:

https://codex.wordpress.org/I18n_for_WordPress_Developers

PO files are compiled to binary MO files, which give faster access to the strings at run-time

Is faster access true? PO can be read only once and cached in some hash table, the same probably goes for MO

Upvotes: 4

Views: 2802

Answers (1)

Guido Flohr
Guido Flohr

Reputation: 2350

There are several reasons:

  1. You should always compile PO files with msgfmt --check which performs several important checks on the PO file, not only a syntax check. For example, if you you are using printf format strings, it will check that %-expansions in the translation match the original string. Failure to do so, may result in crashs at runtime. There are a lot more checks, depending on the (programming) language.
  2. Reading a binary MO file is usually faster and simpler than parsing a textual PO file.
  3. PO files often have translation entries that should not be used for production, for example fuzzy or obsolete entries.
  4. Many PO parsers are buggy or incomplete.
  5. It is part of the gettext API. Translations are expected to be located by default under /usr/share/locale/LOCALE/LC_MESSAGES/TEXTDOMAIN.mo and they are expected to be in MO format, not in PO format. That does, of course, not apply to the countless libraries that just implement a subset of the gettext API.

Upvotes: 5

Related Questions