manpagez: man pages & more
info gawk
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.5 A Simple Internationalization Example

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-pot’ to create the ‘.pot’ file:

$ gawk --gen-pot -f guide.awk > guide.pot

This produces:

#: guide.awk:4
msgid "Don't Panic"
msgstr ""

#: guide.awk:5
msgid "The Answer Is"
msgstr ""

This original portable object template 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.pot’ file.

Next, the messages must be translated. Here is a translation to a hypothetical dialect of English, called “Mellow”:(61)

$ cp guide.pot 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 section awk Portability Issues) 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?

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 30, 2012 using texi2html 5.0.