21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

164 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>gawk’s ‘--gen-po’ command-line option extracts the messages and is discussed next.After that, printf’s ability to rearrange the order for printf arguments at runtime iscovered.9.4.1 Extracting Marked StringsOnce your awk program is working, and all the strings have been marked and you’ve set(and perhaps bound) the text domain, it is time to produce translations. First, use the‘--gen-po’ command-line option to create the initial ‘.po’ file:$ gawk --gen-po -f guide.awk > guide.poWhen run with ‘--gen-po’, gawk does not execute your program. Instead, it parses itas usual and prints all marked strings to standard output in the format of a GNU gettextPortable Object file. Also included in the output are any constant strings that appear asthe first argument to dcgettext or as the first and second argument to dcngettext. 3 SeeSection 9.5 [A Simple Internationalization Example], page 166, for the full list of steps togo through to create and test translations for guide.9.4.2 Rearranging printf ArgumentsFormat strings for printf and sprintf (see Section 4.5 [Using printf Statements forFancier Printing], page 61) present a special problem for translation. Consider the following:4printf(_"String ‘%s’ has %d characters\n",string, length(string)))A possible German translation for this might be:"%d Zeichen lang ist die Zeichenkette ‘%s’\n"The problem should be obvious: the order of the format specifications is different fromthe original! Even though gettext can return the translated string at runtime, it cannotchange the argument order in the call to printf.To solve this problem, printf format specifiers may have an additional optional element,which we call a positional specifier. For example:"%2$d Zeichen lang ist die Zeichenkette ‘%1$s’\n"Here, the positional specifier consists of an integer count, which indicates which argumentto use, and a ‘$’. Counts are one-based, and the format string itself is not included. Thus, inthe following example, ‘string’ is the first argument and ‘length(string)’ is the second:$ gawk ’BEGIN {> string = "Dont Panic"> printf _"%2$d characters live in \"%1$s\"\n",> string, length(string)> }’⊣ 10 characters live in "Dont Panic"If present, positional specifiers come first in the format specification, before the flags,the field width, and/or the precision.3 Starting with gettext version 0.11.5, the xgettext utility that comes with GNU gettext can handle‘.awk’ files.4 This example is borrowed from the GNU gettext manual.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!