⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sec-i18n.html

📁 gtk_text program sample&eg
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>  <head>    <title>      Internationalization    </title>    <meta name="GENERATOR" content=    "Modular DocBook HTML Stylesheet Version 1.45">    <link rel="HOME" title="GTK+ / Gnome Application Development"    href="ggad.html">    <link rel="UP" title="Gnome Application Basics" href=     "cha-startup.html">    <link rel="PREVIOUS" title="Gnome Application Basics" href=     "cha-startup.html">    <link rel="NEXT" title="Argument Parsing with popt" href=     "z77.html">  </head>  <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=   "#840084" alink="#0000FF">    <div class="NAVHEADER">      <table width="100%" border="0" bgcolor="#ffffff" cellpadding=       "1" cellspacing="0">        <tr>          <th colspan="4" align="center">            <font color="#000000" size="2">GTK+ / Gnome Application            Development</font>          </th>        </tr>        <tr>          <td width="25%" bgcolor="#ffffff" align="left">            <a href="cha-startup.html"><font color="#0000ff" size=            "2"><b>&lt;&lt;&lt; Previous</b></font></a>          </td>          <td width="25%" colspan="2" bgcolor="#ffffff" align=           "center">            <font color="#0000ff" size="2"><b><a href="ggad.html">            <font color="#0000ff" size="2"><b>            Home</b></font></a></b></font>          </td>          <td width="25%" bgcolor="#ffffff" align="right">            <a href="z77.html"><font color="#0000ff" size="2"><b>            Next &gt;&gt;&gt;</b></font></a>          </td>        </tr>      </table>    </div>    <div class="SECT1">      <h1 class="SECT1">        <a name="SEC-I18N">Internationalization</a>      </h1>      <p>        All user-visible strings in a Gnome application should be        marked for translation. Translation is achieved using the        GNU <tt class="APPLICATION">gettext</tt> facility. <tt        class="APPLICATION">gettext</tt> is simply a message        catalog; it stores key-value pairs, where the key is the        string hard-coded into the program, and the value is a        translated string (if appropriate) or simply the key (if        there's no translation, or the key is already in the        correct language).      </p>      <p>        As a programmer, it's not your responsibility to provide        translations. However, you must make sure strings are        marked for translation---so that <tt class="APPLICATION">        gettext</tt>'s scripts can extract a list of strings to be        translated---and you must call a special function on each        string when the catalog lookup should take place.      </p>      <div class="FIGURE">        <a name="ML-I18N"></a>        <div class="FUNCSYNOPSIS">          <a name="ML-I18N.SYNOPSIS"></a>          <table border="0" bgcolor="#E0E0E0" width="100%">            <tr>              <td><pre class="FUNCSYNOPSISINFO">#include &lt;libgnome/gnome-i18n.h&gt;</pre>              </td>            </tr>          </table>          <p>            <code><code class="FUNCDEF"><tt class="FUNCTION">            _</tt></code>(<tt class=            "PARAMETER"><i>string</i></tt>);</code>          </p>          <p>            <code><code class="FUNCDEF"><tt class="FUNCTION">            N_</tt></code>(<tt class=            "PARAMETER"><i>string</i></tt>);</code>          </p>        </div>        <p>          <b>Figure 2. Translation Macros</b>        </p>      </div>      <p>        Gnome makes this easy, by defining two macros shown in <a        href="sec-i18n.html#ML-I18N">Figure 2</a>. The macro <tt        class="FUNCTION">_()</tt> both marks the string for        translation and performs the message-catalog lookup. You        should use it in any context C permits a function call. The        <tt class="FUNCTION">N_()</tt> macro is a no-op, but marks        the string for translation. You can use it when C does not        permit a function call; for example in static array        initializers. If you mark a string for translation with <tt        class="FUNCTION">N_()</tt>, you must eventually call <tt        class="FUNCTION">_()</tt> on it to actually perform the        lookup.      </p>      <p>        Here's a simple example:      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;#include &lt;gnome.h&gt;static char* a[] = {   N_("Translate Me"),  N_("Me Too")};int main(int argc, char** argv){  bindtextdomain(PACKAGE, GNOMELOCALEDIR);  textdomain(PACKAGE);  printf(_("Translated String\n"));  printf(_(a[0]));  printf(_(a[1]));  return 0;}&#13;</pre>          </td>        </tr>      </table>      <p>        Notice that the string literals <span class="STRUCTNAME">        "Translate Me"</span> and <span class="STRUCTNAME">"Me        Too"</span> are marked so that <tt class="APPLICATION">        gettext</tt> can find them and produce a list of strings to        be translated. Translators will use this list to create the        actual translations. Later, <tt class="FUNCTION">_()</tt>        includes a function call to perform the tranlation lookup        on each member of the array. Since a function call is        allowed when the string literal <span class="STRUCTNAME">        "Translated String"</span> is introduced, everything can        happen in a single step.      </p>      <p>        At the beginning of your program, you have to call <tt        class="FUNCTION">bindtextdomain()</tt> and <tt class=         "FUNCTION">textdomain()</tt> as shown in the above example.        In the above code, <tt class="FUNCTION">PACKAGE</tt> is a        string representing the package the program is found in,        typically defined in <tt class="FILENAME">config.h</tt>        (see <a href="cha-source.html">the chapter called <i>        Creating Your Source Tree</i></a>). You must arrange to        define <tt class="FUNCTION">GNOMELOCALEDIR</tt>, typically        in your <tt class="FILENAME">Makefile.am</tt> (<tt class=         "APPLICATION">$(prefix)/share/locale</tt>, or <tt class=         "APPLICATION">$(datadir)/locale</tt>, is the standard        value). Translations are stored in <tt class="FUNCTION">        GNOMELOCALEDIR</tt>.      </p>      <p>        When marking strings for translation, you must make sure        your strings are translatable. Avoid constructing a string        at runtime via concatenation. For example, do not do this:      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;  gchar* message = g_strconcat(_("There is an error on device "),                                device, NULL);&#13;</pre>          </td>        </tr>      </table>      <p>        The problem is that in some languages it may be correct to        put the name of the device first (or in the middle). If you        use <tt class="FUNCTION">g_snprintf()</tt> or <tt class=         "FUNCTION">g_strdup_printf()</tt> instead of concatenation,        the translator can change the word order. Here's the right        way to do it:      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;  gchar* message = g_strdup_printf(_("There is an error on device %s"),                                   device);&#13;</pre>          </td>        </tr>      </table>      <p>        Now the translator can move <span class="STRUCTNAME">        %s</span> as needed.      </p>      <p>        Complicated syntax-on-the-fly should be avoided whenever        possible. For example, translating this is a major problem:      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;  printf(_("There %s %d dog%s\n"),          n_dogs &gt; 1 ? _("were") : _("was"),         n_dogs,          n_dogs &gt; 1 ? _("s") : "");&#13;</pre>          </td>        </tr>      </table>      <p>        It is better to move the conditional out of the <tt class=         "FUNCTION">printf()</tt>:      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;  if (n_dogs &gt; 0)    printf(_("There were %d dogs\n"), n_dogs);  else     printf(_("There was 1 dog\n"));&#13;</pre>          </td>        </tr>      </table>      <p>        However, as the <tt class="APPLICATION">gettext</tt> manual        points out, even this will not always work; some languages        will distinguish more categories than "exactly one" and        "more than one" (that is, they might have a word form for        "exactly two" in addition to English's singular and plural        forms). That manual suggests that a lookup table indexed by        the number you plan to use might work in some cases:      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;static const char* ndogs_phrases[] = {  N_("There were no dogs.\n"),  N_("There was one dog.\n"),  N_("There were two dogs.\n"),  N_("There were three dogs.\n")};&#13;</pre>          </td>        </tr>      </table>      <p>        As you can see, this rapidly becomes unpleasant to deal        with. Avoid it if you can. The <tt class="APPLICATION">        gettext</tt> documentation has more examples, if you find        yourself in a hairy situation.      </p>      <p>        Internationalization must also be considered when parsing        or displaying certain kinds of data, including dates and        decimal numbers. In general, the C library provides        sufficient facilities to deal with this; use <tt class=         "FUNCTION">strftime()</tt>, <tt class="FUNCTION">        strcoll()</tt>, and so on to handle these cases; a good C        or POSIX book will explain them. The glib <span class=         "STRUCTNAME">GDate</span> facility handles dates using <tt        class="FUNCTION">strftime()</tt> internally.      </p>      <p>        One common mistake to avoid: don't use locale-dependent        functions when reading and writing files. For example, <tt        class="FUNCTION">printf()</tt> and <tt class="FUNCTION">        scanf()</tt> adjust their decimal number format for the        locale, so you can't use this format in files. Users in        Europe won't be able to read files created in the United        States.      </p>    </div>    <div class="NAVFOOTER">      <br>      <br>      <table width="100%" border="0" bgcolor="#ffffff" cellpadding=       "1" cellspacing="0">        <tr>          <td width="25%" bgcolor="#ffffff" align="left">            <a href="cha-startup.html"><font color="#0000ff" size=            "2"><b>&lt;&lt;&lt; Previous</b></font></a>          </td>          <td width="25%" colspan="2" bgcolor="#ffffff" align=           "center">            <font color="#0000ff" size="2"><b><a href="ggad.html">            <font color="#0000ff" size="2"><b>            Home</b></font></a></b></font>          </td>          <td width="25%" bgcolor="#ffffff" align="right">            <a href="z77.html"><font color="#0000ff" size="2"><b>            Next &gt;&gt;&gt;</b></font></a>          </td>        </tr>        <tr>          <td colspan="2" align="left">            <font color="#000000" size="2"><b>Gnome Application            Basics</b></font>          </td>          <td colspan="2" align="right">            <font color="#000000" size="2"><b>Argument Parsing with            <tt class="APPLICATION">popt</tt></b></font>          </td>        </tr>      </table>    </div>  </body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -