问题
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
回答1:
There are several reasons:
- 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. - Reading a binary MO file is usually faster and simpler than parsing a textual PO file.
- PO files often have translation entries that should not be used for production, for example fuzzy or obsolete entries.
- Many PO parsers are buggy or incomplete.
- 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.
来源:https://stackoverflow.com/questions/53743208/benefits-of-compiling-po-files-to-mo