When you write an awk script to print a report, you might specify the report header and footer information using the print command. You might define the header and footer static values in English. What if you want to execute the report output for some other language? You might end-up copying this awk script to another awk script and modify all the print statements to have the static values displayed in appropriate values.
Probably an easier way is to use internationalization where you can use the same awk script, but change the static values of the output during run time.
This technique is also helpful when you have a huge program, but you end-up changing the printed static output frequently for some reason. Or you might want the users to customize the awk output by changing the static displayed text to something of their own.
This simple example shows the 4 high level steps to implement internalization in awk.
创建一个文本域并将其绑定到awk程序应该寻找到文本域的目录。在本例中,设定为当前目录
$ awk -f iteminfo.awk items.txt
START_TIME:Mon Aug 31 14:37:04 CST 2015
Num Description Type Price Qty
--------------------------------------------
101 HD Camcorder Video $210.00 010
102 Refrigerator Appliance $850.00 002
103 MP3 Player Audio $270.00 015
104 Tennis Racket Sports $190.00 020
105 Laser Printer Office $475.00 005
[luckyzhou@www ~]$ cat iteminfo.awk
BEGIN {
FS=","
TEXTDOMAIN="item"
bindtextdomain(".")
print _"START_TIME:" strftime("%a %b %d %H:%M:%S %Z %Y", systime());
printf "%-3s\t", _"Num";
printf "%-10s\t", _"Description"
printf "%-10s\t", _"Type"
printf "%-5s\t", _"Price"
printf "%-3s\n", _"Qty"
printf _"--------------------------------------------\n"
}
{
printf "%-3d\t%-10s\t%-10s\t$%-.2f\t%03d\n",$1,$2,$3,$4,$5
}
注意:在上面的例子有一个在所有字符串的前边。有 (下划线)在一字符串前面并不能改变它如何输出。它的输出没有任何问题,显示如下:
$ awk -f iteminfo.awk items.txt
START_TIME:Mon Aug 31 15:42:34 CST 2015
Num Description Type Price Qty
----------------------------------------------------
101 HD Camcorder Video $210.00 010
102 Refrigerator Appliance $850.00 002
103 MP3 Player Audio $270.00 015
104 Tennis Racket Sports $190.00 020
105 Laser Printer Office $475.00 005
生成可移植的目标文件(扩展名的.po ) ,如下图所示。请注意,使用--gen-po ,您还可以使用“ -W gen-po”。
$ gawk --gen-po -f iteminfo.awk > iteminfo.po
$ cat iteminfo.po
#: iteminfo.awk:5
msgid "START_TIME:"
msgstr ""
#: iteminfo.awk:6
msgid "Num"
msgstr ""
#: iteminfo.awk:7
msgid "Description"
msgstr ""
#: iteminfo.awk:8
msgid "Type"
msgstr ""
#: iteminfo.awk:9
msgid "Price"
msgstr ""
#: iteminfo.awk:10
msgid "Qty"
msgstr ""
#: iteminfo.awk:11
msgid "----------------------------------------------------\n"
""
msgstr ""
现在,修改该移动的目标文件并更改相应的信息字符串。如果 你想叫"Report Generated on:"(取代"START_TIME:"),编辑iteminfo.po文件,更改在msgid "START_TIME:"下面的msgstr
$ cat iteminfo.po
#: iteminfo.awk:5
msgid "START_TIME:"
msgstr "Report Generated On:"
注意:在这个例子中, msgstr串的其余部分被留下空的。
创建信息对象文件(从可移植对象文件)使用msgfmt命令。
如果iteminfo.po具有所有msgstr空的,它不会产生任何消息目标文件,如下图所示。
$ msgfmt -v iteminfo.po
0 translated messages, 7 untranslated messages.
因为我们创建了一个译本,它会创建messages.mo文件。
$ msgfmt -v iteminfo.po
1 translated message, 6 untranslated messages.
$ ls -1 messages.mo
messages.mo
拷贝这个message对像文件到message目录,你应该在当前目录创建。
[luckyzhou@www ~]$ mkdir -p en_US/LC_MESSAGES
[luckyzhou@www ~]$ mv messages.mo en_US/LC_MESSAGES/item.mo
注意:目标文件名应该与我们给的原awk文件里TEXTDOMAIN变量名匹配。TEXTDOMAIN = "item"。
现在你看到它不显示"START TIME:"了,在输出里,它变成了"Report Generated On:"
$ gawk -f iteminfo.awk items.txt
Report Generated On:Tue Sep 01 15:15:12 CST 2015
Num Description Type Price Qty
----------------------------------------------------
101 HD Camcorder Video $210.00 010
102 Refrigerator Appliance $850.00 002
103 MP3 Player Audio $270.00 015
104 Tennis Racket Sports $190.00 020
105 Laser Printer Office $475.00 005