gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
10KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>54.2. For the Programmer</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="nls-translator.html" title="54.1. For the Translator" /><link rel="next" href="plhandler.html" title="Chapter 55. Writing a Procedural Language Handler" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">54.2. For the Programmer</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="nls-translator.html" title="54.1. For the Translator">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="nls.html" title="Chapter 54. Native Language Support">Up</a></td><th width="60%" align="center">Chapter 54. Native Language Support</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plhandler.html" title="Chapter 55. Writing a Procedural Language Handler">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="NLS-PROGRAMMER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">54.2. For the Programmer</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="nls-programmer.html#NLS-MECHANICS">54.2.1. Mechanics</a></span></dt><dt><span class="sect2"><a href="nls-programmer.html#NLS-GUIDELINES">54.2.2. Message-Writing Guidelines</a></span></dt></dl></div><div class="sect2" id="NLS-MECHANICS"><div class="titlepage"><div><div><h3 class="title">54.2.1. Mechanics</h3></div></div></div><p>
  3. This section describes how to implement native language support in a
  4. program or library that is part of the
  5. <span class="productname">PostgreSQL</span> distribution.
  6. Currently, it only applies to C programs.
  7. </p><div class="procedure" id="id-1.10.7.3.2.3"><p class="title"><strong>Adding NLS Support to a Program</strong></p><ol class="procedure" type="1"><li class="step"><p>
  8. Insert this code into the start-up sequence of the program:
  9. </p><pre class="programlisting">
  10. #ifdef ENABLE_NLS
  11. #include &lt;locale.h&gt;
  12. #endif
  13. ...
  14. #ifdef ENABLE_NLS
  15. setlocale(LC_ALL, "");
  16. bindtextdomain("<em class="replaceable"><code>progname</code></em>", LOCALEDIR);
  17. textdomain("<em class="replaceable"><code>progname</code></em>");
  18. #endif
  19. </pre><p>
  20. (The <em class="replaceable"><code>progname</code></em> can actually be chosen
  21. freely.)
  22. </p></li><li class="step"><p>
  23. Wherever a message that is a candidate for translation is found,
  24. a call to <code class="function">gettext()</code> needs to be inserted. E.g.:
  25. </p><pre class="programlisting">
  26. fprintf(stderr, "panic level %d\n", lvl);
  27. </pre><p>
  28. would be changed to:
  29. </p><pre class="programlisting">
  30. fprintf(stderr, gettext("panic level %d\n"), lvl);
  31. </pre><p>
  32. (<code class="symbol">gettext</code> is defined as a no-op if NLS support is
  33. not configured.)
  34. </p><p>
  35. This tends to add a lot of clutter. One common shortcut is to use:
  36. </p><pre class="programlisting">
  37. #define _(x) gettext(x)
  38. </pre><p>
  39. Another solution is feasible if the program does much of its
  40. communication through one or a few functions, such as
  41. <code class="function">ereport()</code> in the backend. Then you make this
  42. function call <code class="function">gettext</code> internally on all
  43. input strings.
  44. </p></li><li class="step"><p>
  45. Add a file <code class="filename">nls.mk</code> in the directory with the
  46. program sources. This file will be read as a makefile. The
  47. following variable assignments need to be made here:
  48. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="varname">CATALOG_NAME</code></span></dt><dd><p>
  49. The program name, as provided in the
  50. <code class="function">textdomain()</code> call.
  51. </p></dd><dt><span class="term"><code class="varname">AVAIL_LANGUAGES</code></span></dt><dd><p>
  52. List of provided translations — initially empty.
  53. </p></dd><dt><span class="term"><code class="varname">GETTEXT_FILES</code></span></dt><dd><p>
  54. List of files that contain translatable strings, i.e., those
  55. marked with <code class="function">gettext</code> or an alternative
  56. solution. Eventually, this will include nearly all source
  57. files of the program. If this list gets too long you can
  58. make the first <span class="quote">“<span class="quote">file</span>”</span> be a <code class="literal">+</code>
  59. and the second word be a file that contains one file name per
  60. line.
  61. </p></dd><dt><span class="term"><code class="varname">GETTEXT_TRIGGERS</code></span></dt><dd><p>
  62. The tools that generate message catalogs for the translators
  63. to work on need to know what function calls contain
  64. translatable strings. By default, only
  65. <code class="function">gettext()</code> calls are known. If you used
  66. <code class="function">_</code> or other identifiers you need to list
  67. them here. If the translatable string is not the first
  68. argument, the item needs to be of the form
  69. <code class="literal">func:2</code> (for the second argument).
  70. If you have a function that supports pluralized messages,
  71. the item should look like <code class="literal">func:1,2</code>
  72. (identifying the singular and plural message arguments).
  73. </p></dd></dl></div><p>
  74. </p></li></ol></div><p>
  75. The build system will automatically take care of building and
  76. installing the message catalogs.
  77. </p></div><div class="sect2" id="NLS-GUIDELINES"><div class="titlepage"><div><div><h3 class="title">54.2.2. Message-Writing Guidelines</h3></div></div></div><p>
  78. Here are some guidelines for writing messages that are easily
  79. translatable.
  80. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  81. Do not construct sentences at run-time, like:
  82. </p><pre class="programlisting">
  83. printf("Files were %s.\n", flag ? "copied" : "removed");
  84. </pre><p>
  85. The word order within the sentence might be different in other
  86. languages. Also, even if you remember to call <code class="function">gettext()</code> on
  87. each fragment, the fragments might not translate well separately. It's
  88. better to duplicate a little code so that each message to be
  89. translated is a coherent whole. Only numbers, file names, and
  90. such-like run-time variables should be inserted at run time into
  91. a message text.
  92. </p></li><li class="listitem"><p>
  93. For similar reasons, this won't work:
  94. </p><pre class="programlisting">
  95. printf("copied %d file%s", n, n!=1 ? "s" : "");
  96. </pre><p>
  97. because it assumes how the plural is formed. If you figured you
  98. could solve it like this:
  99. </p><pre class="programlisting">
  100. if (n==1)
  101. printf("copied 1 file");
  102. else
  103. printf("copied %d files", n):
  104. </pre><p>
  105. then be disappointed. Some languages have more than two forms,
  106. with some peculiar rules. It's often best to design the message
  107. to avoid the issue altogether, for instance like this:
  108. </p><pre class="programlisting">
  109. printf("number of copied files: %d", n);
  110. </pre><p>
  111. </p><p>
  112. If you really want to construct a properly pluralized message,
  113. there is support for this, but it's a bit awkward. When generating
  114. a primary or detail error message in <code class="function">ereport()</code>, you can
  115. write something like this:
  116. </p><pre class="programlisting">
  117. errmsg_plural("copied %d file",
  118. "copied %d files",
  119. n,
  120. n)
  121. </pre><p>
  122. The first argument is the format string appropriate for English
  123. singular form, the second is the format string appropriate for
  124. English plural form, and the third is the integer control value
  125. that determines which plural form to use. Subsequent arguments
  126. are formatted per the format string as usual. (Normally, the
  127. pluralization control value will also be one of the values to be
  128. formatted, so it has to be written twice.) In English it only
  129. matters whether <em class="replaceable"><code>n</code></em> is 1 or not 1, but in other
  130. languages there can be many different plural forms. The translator
  131. sees the two English forms as a group and has the opportunity to
  132. supply multiple substitute strings, with the appropriate one being
  133. selected based on the run-time value of <em class="replaceable"><code>n</code></em>.
  134. </p><p>
  135. If you need to pluralize a message that isn't going directly to an
  136. <code class="function">errmsg</code> or <code class="function">errdetail</code> report, you have to use
  137. the underlying function <code class="function">ngettext</code>. See the gettext
  138. documentation.
  139. </p></li><li class="listitem"><p>
  140. If you want to communicate something to the translator, such as
  141. about how a message is intended to line up with other output,
  142. precede the occurrence of the string with a comment that starts
  143. with <code class="literal">translator</code>, e.g.:
  144. </p><pre class="programlisting">
  145. /* translator: This message is not what it seems to be. */
  146. </pre><p>
  147. These comments are copied to the message catalog files so that
  148. the translators can see them.
  149. </p></li></ul></div><p>
  150. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="nls-translator.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="nls.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plhandler.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">54.1. For the Translator </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 55. Writing a Procedural Language Handler</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1