Now let's look at a step-by-step example of how to internationalize and localize a simple awk program, using guide.awk as our original source:
BEGIN { TEXTDOMAIN = "guide" bindtextdomain(".") # for testing print _"Don't Panic" print _"The Answer Is", 42 print "Pardon me, Zaphod who?" }
Run `gawk --gen-po' to create the .po file:
$ gawk --gen-po -f guide.awk > guide.po
This produces:
#: guide.awk:4 msgid "Don't Panic" msgstr "" #: guide.awk:5 msgid "The Answer Is" msgstr ""
This original portable object file is saved and reused for each language
into which the application is translated. The msgid
is the original string and the msgstr
is the translation.
NOTE: Strings not marked with a leading underscore do not appear in the guide.po file.
Next, the messages must be translated. Here is a translation to a hypothetical dialect of English, called “Mellow”:1
$ cp guide.po guide-mellow.po Add translations to guide-mellow.po ...
Following are the translations:
#: guide.awk:4 msgid "Don't Panic" msgstr "Hey man, relax!" #: guide.awk:5 msgid "The Answer Is" msgstr "Like, the scoop is"
The next step is to make the directory to hold the binary message object
file and then to create the guide.mo file.
The directory layout shown here is standard for GNU gettext
on
GNU/Linux systems. Other versions of gettext
may use a different
layout:
$ mkdir en_US en_US/LC_MESSAGES
The msgfmt utility does the conversion from human-readable .po file to machine-readable .mo file. By default, msgfmt creates a file named messages. This file must be renamed and placed in the proper directory so that gawk can find it:
$ msgfmt guide-mellow.po $ mv messages en_US/LC_MESSAGES/guide.mo
Finally, we run the program to test it:
$ gawk -f guide.awk -| Hey man, relax! -| Like, the scoop is 42 -| Pardon me, Zaphod who?
If the three replacement functions for dcgettext
, dcngettext
and bindtextdomain
(see I18N Portability)
are in a file named libintl.awk,
then we can run guide.awk unchanged as follows:
$ gawk --posix -f guide.awk -f libintl.awk -| Don't Panic -| The Answer Is 42 -| Pardon me, Zaphod who?