4

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

Konrad
  • 6,385
  • 12
  • 53
  • 96

1 Answers1

5

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 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.
Guido Flohr
  • 1,871
  • 15
  • 28
  • 1
    Many programs load translation only once on startup. I don't think performance is that important unless loading it every time. – Konrad Dec 17 '18 at 16:53
  • Compiling C sources on the fly before executing binaries would also just contribute to their startup time, and still it is a customary thing to do. – Guido Flohr Dec 20 '18 at 14:01
  • See also reason #5 that I have just added. – Guido Flohr Dec 20 '18 at 14:02
  • 1
    @Konrad I think you are only looking at desktop applications or long-running daemons/services. But for CLI programs, the startup performance *is* important because they are often invoked from shell scripts, maybe even in loops. – Guido Flohr Jul 08 '20 at 19:54