1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Localization</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="An Introduction to Programmable Completion"
HREF="tabexpansion.html"><LINK
REL="NEXT"
TITLE="History Commands"
HREF="histcommands.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="APPENDIX"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="tabexpansion.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="histcommands.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="APPENDIX"
><H1
><A
NAME="LOCALIZATION"
></A
>Appendix K. Localization</H1
><P
>Localization is an undocumented Bash feature.</P
><P
><A
NAME="LOCALEREF"
></A
>A localized shell script echoes
its text output in the language defined as the system's locale.
A Linux user in Berlin, Germany, would get script output in German,
whereas his cousin in Berlin, Maryland, would get output from
the same script in English.</P
><P
>To create a localized script, use the following template to
write all messages to the user (error messages, prompts,
etc.).</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # localized.sh
3 # Script by Stphane Chazelas,
4 #+ modified by Bruno Haible, bugfixed by Alfredo Pironti.
5
6 . gettext.sh
7
8 E_CDERROR=65
9
10 error()
11 {
12 printf "$@" >&2
13 exit $E_CDERROR
14 }
15
16 cd $var || error "`eval_gettext \"Can\'t cd to \\\$var.\"`"
17 # The triple backslashes (escapes) in front of $var needed
18 #+ "because eval_gettext expects a string
19 #+ where the variable values have not yet been substituted."
20 # -- per Bruno Haible
21 read -p "`gettext \"Enter the value: \"`" var
22 # ...
23
24
25 # ------------------------------------------------------------------
26 # Alfredo Pironti comments:
27
28 # This script has been modified to not use the $"..." syntax in
29 #+ favor of the "`gettext \"...\"`" syntax.
30 # This is ok, but with the new localized.sh program, the commands
31 #+ "bash -D filename" and "bash --dump-po-string filename"
32 #+ will produce no output
33 #+ (because those command are only searching for the $"..." strings)!
34 # The ONLY way to extract strings from the new file is to use the
35 # 'xgettext' program. However, the xgettext program is buggy.
36
37 # Note that 'xgettext' has another bug.
38 #
39 # The shell fragment:
40 # gettext -s "I like Bash"
41 # will be correctly extracted, but . . .
42 # xgettext -s "I like Bash"
43 # . . . fails!
44 # 'xgettext' will extract "-s" because
45 #+ the command only extracts the
46 #+ very first argument after the 'gettext' word.
47
48
49 # Escape characters:
50 #
51 # To localize a sentence like
52 # echo -e "Hello\tworld!"
53 #+ you must use
54 # echo -e "`gettext \"Hello\\tworld\"`"
55 # The "double escape character" before the `t' is needed because
56 #+ 'gettext' will search for a string like: 'Hello\tworld'
57 # This is because gettext will read one literal `\')
58 #+ and will output a string like "Bonjour\tmonde",
59 #+ so the 'echo' command will display the message correctly.
60 #
61 # You may not use
62 # echo "`gettext -e \"Hello\tworld\"`"
63 #+ due to the xgettext bug explained above.
64
65
66
67 # Let's localize the following shell fragment:
68 # echo "-h display help and exit"
69 #
70 # First, one could do this:
71 # echo "`gettext \"-h display help and exit\"`"
72 # This way 'xgettext' will work ok,
73 #+ but the 'gettext' program will read "-h" as an option!
74 #
75 # One solution could be
76 # echo "`gettext -- \"-h display help and exit\"`"
77 # This way 'gettext' will work,
78 #+ but 'xgettext' will extract "--", as referred to above.
79 #
80 # The workaround you may use to get this string localized is
81 # echo -e "`gettext \"\\0-h display help and exit\"`"
82 # We have added a \0 (NULL) at the beginning of the sentence.
83 # This way 'gettext' works correctly, as does 'xgettext.'
84 # Moreover, the NULL character won't change the behavior
85 #+ of the 'echo' command.
86 # ------------------------------------------------------------------</PRE
></TD
></TR
></TABLE
>
</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>bash -D localized.sh</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>"Can't cd to %s."
"Enter the value: "</TT
></PRE
></TD
></TR
></TABLE
>
This lists all the localized text. (The <TT
CLASS="OPTION"
>-D</TT
>
option lists double-quoted strings prefixed by a <SPAN
CLASS="TOKEN"
>$</SPAN
>,
without executing the script.)</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>bash --dump-po-strings localized.sh</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>#: a:6
msgid "Can't cd to %s."
msgstr ""
#: a:7
msgid "Enter the value: "
msgstr ""</TT
></PRE
></TD
></TR
></TABLE
>
The <TT
CLASS="OPTION"
>--dump-po-strings</TT
> option to Bash
resembles the <TT
CLASS="OPTION"
>-D</TT
> option, but uses <A
HREF="textproc.html#GETTEXTREF"
>gettext</A
> <SPAN
CLASS="QUOTE"
>"po"</SPAN
> format.
</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Bruno Haible points out:</P
><P
>Starting with gettext-0.12.2, <B
CLASS="COMMAND"
>xgettext -o - localized.sh</B
>
is recommended instead of <B
CLASS="COMMAND"
>bash --dump-po-strings
localized.sh</B
>, because <B
CLASS="COMMAND"
>xgettext</B
> . . .</P
><P
>1. understands the gettext and eval_gettext commands
(whereas bash --dump-po-strings understands only its deprecated
$"..." syntax)</P
><P
>2. can extract comments placed by the programmer, intended
to be read by the translator.</P
><P
>This shell code is then not specific to Bash any
more; it works the same way with Bash 1.x and other /bin/sh
implementations.</P
></TD
></TR
></TABLE
></DIV
><P
>Now, build a <TT
CLASS="FILENAME"
>language.po</TT
>
file for each language that the script will be translated
into, specifying the <TT
CLASS="REPLACEABLE"
><I
>msgstr</I
></TT
>. Alfredo
Pironti gives the following example:</P
><P
>fr.po:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #: a:6
2 msgid "Can't cd to $var."
3 msgstr "Impossible de se positionner dans le repertoire $var."
4 #: a:7
5 msgid "Enter the value: "
6 msgstr "Entrez la valeur : "
7
8 # The string are dumped with the variable names, not with the %s syntax,
9 #+ similar to C programs.
10 #+ This is a very cool feature if the programmer uses
11 #+ variable names that make sense!</PRE
></TD
></TR
></TABLE
>
</P
><P
>Then, run <A
HREF="textproc.html#MSGFMTREF"
>msgfmt</A
>.</P
><P
><TT
CLASS="USERINPUT"
><B
>msgfmt -o localized.sh.mo fr.po</B
></TT
></P
><P
>Place the resulting <TT
CLASS="FILENAME"
>localized.sh.mo</TT
> file in the
<TT
CLASS="FILENAME"
>/usr/local/share/locale/fr/LC_MESSAGES</TT
>
directory, and at the beginning of the script, insert the lines:
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 TEXTDOMAINDIR=/usr/local/share/locale
2 TEXTDOMAIN=localized.sh</PRE
></TD
></TR
></TABLE
>
</P
><P
>If a user on a French system runs the script, she will get
French messages.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>With older versions of Bash or other shells, localization requires
<A
HREF="textproc.html#GETTEXTREF"
>gettext</A
>, using the
<TT
CLASS="OPTION"
>-s</TT
> option. In this case, the script becomes:</P
><P
><A
NAME="GETTEXTEXAMPLE"
></A
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # localized.sh
3
4 E_CDERROR=65
5
6 error() {
7 local format=$1
8 shift
9 printf "$(gettext -s "$format")" "$@" >&2
10 exit $E_CDERROR
11 }
12 cd $var || error "Can't cd to %s." "$var"
13 read -p "$(gettext -s "Enter the value: ")" var
14 # ...</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><P
>The <TT
CLASS="VARNAME"
>TEXTDOMAIN</TT
> and
<TT
CLASS="VARNAME"
>TEXTDOMAINDIR</TT
> variables need to be set and
exported to the environment. This should be done within the
script itself.</P
><P
>---</P
><P
>This appendix written by Stphane Chazelas,
with modifications suggested by Alfredo Pironti,
and by Bruno Haible, maintainer of GNU <A
HREF="textproc.html#GETTEXTREF"
>gettext</A
>.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="tabexpansion.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="histcommands.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>An Introduction to Programmable Completion</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
> </TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>History Commands</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|