A Study of Po
Why GNU Gettext and PO files matter for practical i18n, plus a quick primer on PO message syntax.
#language #learn #translation
We’ll talk about the GNU Gettext and the PO format here.
1. Why do we need to talk about this?
I have been working on the openATTIC project for almost two months, from building the environment of a small Ceph cluster (5 nodes), to writing the openATTIC RESTful API documentation (UPDATE: Ceph dashboard has replaced openATTIC in SUSE-SES-6 product, check this site for ceph dashboard API documentation ), and now merging two ‘branches’ of different features: one I call ‘enhanced’, and another ‘base’ (which is actually the master branch of the oA project).
One significant difference is the ‘enhanced’ branch is a hard-coded translated version, the translations appear directly in the source code file (almost in every html file under the /webui directory). Although these translations are good enough, well, much more better than the google translation result, but it’s not a good way for the i18n(internationalization), especially for this ‘international’ project.
So here comes the GNU Gettext and the PO format files, this is the way the master branch do, and you could find these files under the /webui/locale/ directory. They exist for this situation, where you would like to write a program in your native language, not only English, and the program should be ‘translatable’, which means any other translators can easily contribute for other versions.
In a word, the PO format file is a place where you put your translation in your language, and you can just pay a little attention to your source code file in order to internationlize a program. Easy, and elegant!
2. What are Gettext and PO file?
The GNU `gettext’ utilities are a set of tools that provides a framework to help other GNU packages produce multi-lingual messages.
So, Gettext is a system which helps us for the translation. And the translation file format it defines, is just the PO format.
3. Basic syntax of the PO format file
Example:
#: finddialog.cpp:39
msgid "Gaseous Nebulae"
msgstr "Gasne magline"`
Such a snippet is also called a message, a .po file is just a group of messages.
The first line is the source reference comment, which is a line staring with #: above the msgid “…” line. “It tells from which source file of the program code (or source document of any kind), and the line in that source file.”
The msgid string is follwed by the original string, which you want to translate into another language, and which are wrapped in double quotes.
The keyword msgstr denotes then the string which to become the translation, and you see, also double-quoted.
3.1 String wrapping
If a message to be translated is long, you can just wrap the original and translation strings like this example below:
#: indimenu.cpp:96
msgid ""
"No INDI devices (...)"
"(...) in the devices menu."
msgstr ""
"Nema INDI uređaja (...) u meniju uređaja."
To be continued…
Thoughts:
- When you modified a html file, if the position of the ‘translate’ tag changed, then you need to correct the .po file as well. This is inconvenient if we want to add more features in the future. -> Q: Is there any solution ?
- In Angular, the i18n support is well designed, you can use Angular CLI to ‘extract localizable text into a file that you can send out to be translated’. That means you can just collect all those strings to be translated with several commands (or just one?), this greatly free you from doing this manually. Check here for more details about how to make your app available in multiple languages.