📄 localization.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Localization</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Important System Directories"HREF="systemdirs.html"><LINKREL="NEXT"TITLE="History Commands"HREF="histcommands.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="APPENDIX"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="systemdirs.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="histcommands.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="LOCALIZATION"></A>Appendix I. Localization</H1><P>Localization is an undocumented Bash feature.</P><P>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> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # localized.sh 3 # Script by St閜hane 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> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>bash -D localized.sh</B></TT> <TTCLASS="COMPUTEROUTPUT">"Can't cd to %s." "Enter the value: "</TT></PRE></TD></TR></TABLE> This lists all the localized text. (The <TTCLASS="OPTION">-D</TT> option lists double-quoted strings prefixed by a <SPANCLASS="TOKEN">$</SPAN>, without executing the script.)</P><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>bash --dump-po-strings localized.sh</B></TT> <TTCLASS="COMPUTEROUTPUT">#: a:6 msgid "Can't cd to %s." msgstr "" #: a:7 msgid "Enter the value: " msgstr ""</TT></PRE></TD></TR></TABLE> The <TTCLASS="OPTION">--dump-po-strings</TT> option to Bash resembles the <TTCLASS="OPTION">-D</TT> option, but uses <AHREF="textproc.html#GETTEXTREF">gettext</A> <SPANCLASS="QUOTE">"po"</SPAN> format. </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Bruno Haible points out:</P><P>Starting with gettext-0.12.2, <BCLASS="COMMAND">xgettext -o - localized.sh</B> is recommended instead of <BCLASS="COMMAND">bash --dump-po-strings localized.sh</B>, because <BCLASS="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 <TTCLASS="FILENAME">language.po</TT> file for each language that the script will be translated into, specifying the <TTCLASS="REPLACEABLE"><I>msgstr</I></TT>. Alfredo Pironti gives the following example:</P><P>fr.po: <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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 <AHREF="textproc.html#MSGFMTREF">msgfmt</A>.</P><P><TTCLASS="USERINPUT"><B>msgfmt -o localized.sh.mo fr.po</B></TT></P><P>Place the resulting <TTCLASS="FILENAME">localized.sh.mo</TT> file in the <TTCLASS="FILENAME">/usr/local/share/locale/fr/LC_MESSAGES</TT> directory, and at the beginning of the script, insert the lines: <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>With older versions of Bash or other shells, localization requires <AHREF="textproc.html#GETTEXTREF">gettext</A>, using the <TTCLASS="OPTION">-s</TT> option. In this case, the script becomes:</P><P><ANAME="GETTEXTEXAMPLE"></A> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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 <TTCLASS="VARNAME">TEXTDOMAIN</TT> and <TTCLASS="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 St閜hane Chazelas, with modifications suggested by Alfredo Pironti, and by Bruno Haible, maintainer of GNU <AHREF="textproc.html#GETTEXTREF">gettext</A>.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="systemdirs.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="histcommands.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Important System Directories</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">History Commands</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -