Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/share/doc/gettext/gettext_15.html @ 68:5028fdace37b
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 16:23:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 68:5028fdace37b |
---|---|
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> | |
2 <html> | |
3 <!-- Created on February, 21 2024 by texi2html 1.78a --> | |
4 <!-- | |
5 Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) | |
6 Karl Berry <karl@freefriends.org> | |
7 Olaf Bachmann <obachman@mathematik.uni-kl.de> | |
8 and many others. | |
9 Maintained by: Many creative people. | |
10 Send bugs and suggestions to <texi2html-bug@nongnu.org> | |
11 | |
12 --> | |
13 <head> | |
14 <title>GNU gettext utilities: 15. Other Programming Languages</title> | |
15 | |
16 <meta name="description" content="GNU gettext utilities: 15. Other Programming Languages"> | |
17 <meta name="keywords" content="GNU gettext utilities: 15. Other Programming Languages"> | |
18 <meta name="resource-type" content="document"> | |
19 <meta name="distribution" content="global"> | |
20 <meta name="Generator" content="texi2html 1.78a"> | |
21 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
22 <style type="text/css"> | |
23 <!-- | |
24 a.summary-letter {text-decoration: none} | |
25 pre.display {font-family: serif} | |
26 pre.format {font-family: serif} | |
27 pre.menu-comment {font-family: serif} | |
28 pre.menu-preformatted {font-family: serif} | |
29 pre.smalldisplay {font-family: serif; font-size: smaller} | |
30 pre.smallexample {font-size: smaller} | |
31 pre.smallformat {font-family: serif; font-size: smaller} | |
32 pre.smalllisp {font-size: smaller} | |
33 span.roman {font-family:serif; font-weight:normal;} | |
34 span.sansserif {font-family:sans-serif; font-weight:normal;} | |
35 ul.toc {list-style: none} | |
36 --> | |
37 </style> | |
38 | |
39 | |
40 </head> | |
41 | |
42 <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> | |
43 | |
44 <table cellpadding="1" cellspacing="1" border="0"> | |
45 <tr><td valign="middle" align="left">[<a href="gettext_14.html#SEC262" title="Beginning of this chapter or previous chapter"> << </a>]</td> | |
46 <td valign="middle" align="left">[<a href="gettext_16.html#SEC340" title="Next chapter"> >> </a>]</td> | |
47 <td valign="middle" align="left"> </td> | |
48 <td valign="middle" align="left"> </td> | |
49 <td valign="middle" align="left"> </td> | |
50 <td valign="middle" align="left"> </td> | |
51 <td valign="middle" align="left"> </td> | |
52 <td valign="middle" align="left">[<a href="gettext_toc.html#SEC_Top" title="Cover (top) of document">Top</a>]</td> | |
53 <td valign="middle" align="left">[<a href="gettext_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> | |
54 <td valign="middle" align="left">[<a href="gettext_21.html#SEC389" title="Index">Index</a>]</td> | |
55 <td valign="middle" align="left">[<a href="gettext_abt.html#SEC_About" title="About (help)"> ? </a>]</td> | |
56 </tr></table> | |
57 | |
58 <hr size="2"> | |
59 <a name="Programming-Languages"></a> | |
60 <a name="SEC263"></a> | |
61 <h1 class="chapter"> <a href="gettext_toc.html#TOC256">15. Other Programming Languages</a> </h1> | |
62 | |
63 <p>While the presentation of <code>gettext</code> focuses mostly on C and | |
64 implicitly applies to C++ as well, its scope is far broader than that: | |
65 Many programming languages, scripting languages and other textual data | |
66 like GUI resources or package descriptions can make use of the gettext | |
67 approach. | |
68 </p> | |
69 | |
70 | |
71 <a name="Language-Implementors"></a> | |
72 <a name="SEC264"></a> | |
73 <h2 class="section"> <a href="gettext_toc.html#TOC257">15.1 The Language Implementor's View</a> </h2> | |
74 | |
75 <p>All programming and scripting languages that have the notion of strings | |
76 are eligible to supporting <code>gettext</code>. Supporting <code>gettext</code> | |
77 means the following: | |
78 </p> | |
79 <ol> | |
80 <li> | |
81 You should add to the language a syntax for translatable strings. In | |
82 principle, a function call of <code>gettext</code> would do, but a shorthand | |
83 syntax helps keeping the legibility of internationalized programs. For | |
84 example, in C we use the syntax <code>_("string")</code>, and in GNU awk we use | |
85 the shorthand <code>_"string"</code>. | |
86 | |
87 </li><li> | |
88 You should arrange that evaluation of such a translatable string at | |
89 runtime calls the <code>gettext</code> function, or performs equivalent | |
90 processing. | |
91 | |
92 </li><li> | |
93 Similarly, you should make the functions <code>ngettext</code>, | |
94 <code>dcgettext</code>, <code>dcngettext</code> available from within the language. | |
95 These functions are less often used, but are nevertheless necessary for | |
96 particular purposes: <code>ngettext</code> for correct plural handling, and | |
97 <code>dcgettext</code> and <code>dcngettext</code> for obeying other locale-related | |
98 environment variables than <code>LC_MESSAGES</code>, such as <code>LC_TIME</code> or | |
99 <code>LC_MONETARY</code>. For these latter functions, you need to make the | |
100 <code>LC_*</code> constants, available in the C header <code><locale.h></code>, | |
101 referenceable from within the language, usually either as enumeration | |
102 values or as strings. | |
103 | |
104 </li><li> | |
105 You should allow the programmer to designate a message domain, either by | |
106 making the <code>textdomain</code> function available from within the | |
107 language, or by introducing a magic variable called <code>TEXTDOMAIN</code>. | |
108 Similarly, you should allow the programmer to designate where to search | |
109 for message catalogs, by providing access to the <code>bindtextdomain</code> | |
110 function or — on native Windows platforms — to the <code>wbindtextdomain</code> | |
111 function. | |
112 | |
113 </li><li> | |
114 You should either perform a <code>setlocale (LC_ALL, "")</code> call during | |
115 the startup of your language runtime, or allow the programmer to do so. | |
116 Remember that gettext will act as a no-op if the <code>LC_MESSAGES</code> and | |
117 <code>LC_CTYPE</code> locale categories are not both set. | |
118 | |
119 </li><li> | |
120 A programmer should have a way to extract translatable strings from a | |
121 program into a PO file. The GNU <code>xgettext</code> program is being | |
122 extended to support very different programming languages. Please | |
123 contact the GNU <code>gettext</code> maintainers to help them doing this. | |
124 The GNU <code>gettext</code> maintainers will need from you a formal | |
125 description of the lexical structure of source files. It should | |
126 answer the questions: | |
127 <ul> | |
128 <li> | |
129 What does a token look like? | |
130 </li><li> | |
131 What does a string literal look like? What escape characters exist | |
132 inside a string? | |
133 </li><li> | |
134 What escape characters exist outside of strings? If Unicode escapes | |
135 are supported, are they applied before or after tokenization? | |
136 </li><li> | |
137 What is the syntax for function calls? How are consecutive arguments | |
138 in the same function call separated? | |
139 </li><li> | |
140 What is the syntax for comments? | |
141 </li></ul> | |
142 <p>Based on this description, the GNU <code>gettext</code> maintainers | |
143 can add support to <code>xgettext</code>. | |
144 </p> | |
145 <p>If the string extractor is best integrated into your language's parser, | |
146 GNU <code>xgettext</code> can function as a front end to your string extractor. | |
147 </p> | |
148 </li><li> | |
149 The language's library should have a string formatting facility. | |
150 Additionally: | |
151 <ol> | |
152 <li> | |
153 There must be a way, in the format string, to denote the arguments by a | |
154 positional number or a name. This is needed because for some languages | |
155 and some messages with more than one substitutable argument, the | |
156 translation will need to output the substituted arguments in different | |
157 order. See section <a href="gettext_4.html#SEC30">Special Comments preceding Keywords</a>. | |
158 </li><li> | |
159 The syntax of format strings must be documented in a way that translators | |
160 can understand. The GNU <code>gettext</code> manual will be extended to | |
161 include a pointer to this documentation. | |
162 </li></ol> | |
163 <p>Based on this, the GNU <code>gettext</code> maintainers can add a format string | |
164 equivalence checker to <code>msgfmt</code>, so that translators get told | |
165 immediately when they have made a mistake during the translation of a | |
166 format string. | |
167 </p> | |
168 </li><li> | |
169 If the language has more than one implementation, and not all of the | |
170 implementations use <code>gettext</code>, but the programs should be portable | |
171 across implementations, you should provide a no-i18n emulation, that | |
172 makes the other implementations accept programs written for yours, | |
173 without actually translating the strings. | |
174 | |
175 </li><li> | |
176 To help the programmer in the task of marking translatable strings, | |
177 which is sometimes performed using the Emacs PO mode (see section <a href="gettext_4.html#SEC29">Marking Translatable Strings</a>), | |
178 you are welcome to | |
179 contact the GNU <code>gettext</code> maintainers, so they can add support for | |
180 your language to ‘<tt>po-mode.el</tt>’. | |
181 </li></ol> | |
182 | |
183 <p>On the implementation side, two approaches are possible, with | |
184 different effects on portability and copyright: | |
185 </p> | |
186 <ul> | |
187 <li> | |
188 You may link against GNU <code>gettext</code> functions if they are found in | |
189 the C library. For example, an autoconf test for <code>gettext()</code> and | |
190 <code>ngettext()</code> will detect this situation. For the moment, this test | |
191 will succeed on GNU systems and on Solaris 11 platforms. No severe | |
192 copyright restrictions apply, except if you want to distribute statically | |
193 linked binaries. | |
194 | |
195 </li><li> | |
196 You may emulate or reimplement the GNU <code>gettext</code> functionality. | |
197 This has the advantage of full portability and no copyright | |
198 restrictions, but also the drawback that you have to reimplement the GNU | |
199 <code>gettext</code> features (such as the <code>LANGUAGE</code> environment | |
200 variable, the locale aliases database, the automatic charset conversion, | |
201 and plural handling). | |
202 </li></ul> | |
203 | |
204 | |
205 <a name="Programmers-for-other-Languages"></a> | |
206 <a name="SEC265"></a> | |
207 <h2 class="section"> <a href="gettext_toc.html#TOC258">15.2 The Programmer's View</a> </h2> | |
208 | |
209 <p>For the programmer, the general procedure is the same as for the C | |
210 language. The Emacs PO mode marking supports other languages, and the GNU | |
211 <code>xgettext</code> string extractor recognizes other languages based on the | |
212 file extension or a command-line option. In some languages, | |
213 <code>setlocale</code> is not needed because it is already performed by the | |
214 underlying language runtime. | |
215 </p> | |
216 | |
217 <a name="Translators-for-other-Languages"></a> | |
218 <a name="SEC266"></a> | |
219 <h2 class="section"> <a href="gettext_toc.html#TOC259">15.3 The Translator's View</a> </h2> | |
220 | |
221 <p>The translator works exactly as in the C language case. The only | |
222 difference is that when translating format strings, she has to be aware | |
223 of the language's particular syntax for positional arguments in format | |
224 strings. | |
225 </p> | |
226 | |
227 | |
228 <a name="c_002dformat"></a> | |
229 <a name="SEC267"></a> | |
230 <h3 class="subsection"> <a href="gettext_toc.html#TOC260">15.3.1 C Format Strings</a> </h3> | |
231 | |
232 <p>C format strings are described in POSIX (IEEE P1003.1 2001), section | |
233 XSH 3 fprintf(), | |
234 <a href="http://www.opengroup.org/onlinepubs/007904975/functions/fprintf.html">http://www.opengroup.org/onlinepubs/007904975/functions/fprintf.html</a>. | |
235 See also the fprintf() manual page, | |
236 <a href="http://www.linuxvalley.it/encyclopedia/ldp/manpage/man3/printf.3.php">http://www.linuxvalley.it/encyclopedia/ldp/manpage/man3/printf.3.php</a>, | |
237 <a href="http://informatik.fh-wuerzburg.de/student/i510/man/printf.html">http://informatik.fh-wuerzburg.de/student/i510/man/printf.html</a>. | |
238 </p> | |
239 <p>Although format strings with positions that reorder arguments, such as | |
240 </p> | |
241 <table><tr><td> </td><td><pre class="example">"Only %2$d bytes free on '%1$s'." | |
242 </pre></td></tr></table> | |
243 | |
244 <p>which is semantically equivalent to | |
245 </p> | |
246 <table><tr><td> </td><td><pre class="example">"'%s' has only %d bytes free." | |
247 </pre></td></tr></table> | |
248 | |
249 <p>are a POSIX/XSI feature and not specified by ISO C 99, translators can rely | |
250 on this reordering ability: On the few platforms where <code>printf()</code>, | |
251 <code>fprintf()</code> etc. don't support this feature natively, ‘<tt>libintl.a</tt>’ | |
252 or ‘<tt>libintl.so</tt>’ provides replacement functions, and GNU <code><libintl.h></code> | |
253 activates these replacement functions automatically. | |
254 </p> | |
255 <a name="IDX1114"></a> | |
256 <a name="IDX1115"></a> | |
257 <p>As a special feature for Farsi (Persian) and maybe Arabic, translators can | |
258 insert an ‘<samp>I</samp>’ flag into numeric format directives. For example, the | |
259 translation of <code>"%d"</code> can be <code>"%Id"</code>. The effect of this flag, | |
260 on systems with GNU <code>libc</code>, is that in the output, the ASCII digits are | |
261 replaced with the ‘<samp>outdigits</samp>’ defined in the <code>LC_CTYPE</code> locale | |
262 category. On other systems, the <code>gettext</code> function removes this flag, | |
263 so that it has no effect. | |
264 </p> | |
265 <p>Note that the programmer should <em>not</em> put this flag into the | |
266 untranslated string. (Putting the ‘<samp>I</samp>’ format directive flag into an | |
267 <var>msgid</var> string would lead to undefined behaviour on platforms without | |
268 glibc when NLS is disabled.) | |
269 </p> | |
270 | |
271 <a name="objc_002dformat"></a> | |
272 <a name="SEC268"></a> | |
273 <h3 class="subsection"> <a href="gettext_toc.html#TOC261">15.3.2 Objective C Format Strings</a> </h3> | |
274 | |
275 <p>Objective C format strings are like C format strings. They support an | |
276 additional format directive: "%@", which when executed consumes an argument | |
277 of type <code>Object *</code>. | |
278 </p> | |
279 | |
280 <a name="c_002b_002b_002dformat"></a> | |
281 <a name="SEC269"></a> | |
282 <h3 class="subsection"> <a href="gettext_toc.html#TOC262">15.3.3 C++ Format Strings</a> </h3> | |
283 | |
284 <p>C++ format strings are described in ISO C++ 20, namely in | |
285 <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4861.pdf">https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4861.pdf</a>, | |
286 section 20.20.2 Format string [format.string]. | |
287 </p> | |
288 <p>An easier-to-read description is found at | |
289 <a href="https://en.cppreference.com/w/cpp/utility/format/format#Parameters">https://en.cppreference.com/w/cpp/utility/format/format#Parameters</a> and | |
290 <a href="https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification">https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification</a>. | |
291 </p> | |
292 | |
293 <a name="python_002dformat"></a> | |
294 <a name="SEC270"></a> | |
295 <h3 class="subsection"> <a href="gettext_toc.html#TOC263">15.3.4 Python Format Strings</a> </h3> | |
296 | |
297 <p>There are two kinds of format strings in Python: those acceptable to | |
298 the Python built-in format operator <code>%</code>, labelled as | |
299 ‘<samp>python-format</samp>’, and those acceptable to the <code>format</code> method | |
300 of the ‘<samp>str</samp>’ object. | |
301 </p> | |
302 <p>Python <code>%</code> format strings are described in | |
303 Python Library reference / | |
304 5. Built-in Types / | |
305 5.6. Sequence Types / | |
306 5.6.2. String Formatting Operations. | |
307 <a href="https://docs.python.org/2/library/stdtypes.html#string-formatting-operations">https://docs.python.org/2/library/stdtypes.html#string-formatting-operations</a>. | |
308 </p> | |
309 <p>Python brace format strings are described in PEP 3101 – Advanced | |
310 String Formatting, <a href="https://www.python.org/dev/peps/pep-3101/">https://www.python.org/dev/peps/pep-3101/</a>. | |
311 </p> | |
312 | |
313 <a name="java_002dformat"></a> | |
314 <a name="SEC271"></a> | |
315 <h3 class="subsection"> <a href="gettext_toc.html#TOC264">15.3.5 Java Format Strings</a> </h3> | |
316 | |
317 <p>There are two kinds of format strings in Java: those acceptable to the | |
318 <code>MessageFormat.format</code> function, labelled as ‘<samp>java-format</samp>’, | |
319 and those acceptable to the <code>String.format</code> and | |
320 <code>PrintStream.printf</code> functions, labelled as ‘<samp>java-printf-format</samp>’. | |
321 </p> | |
322 <p>Java format strings are described in the JDK documentation for class | |
323 <code>java.text.MessageFormat</code>, | |
324 <a href="https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html">https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html</a>. | |
325 See also the ICU documentation | |
326 <a href="http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html">http://icu-project.org/apiref/icu4j/com/ibm/icu/text/MessageFormat.html</a>. | |
327 </p> | |
328 <p>Java <code>printf</code> format strings are described in the JDK documentation | |
329 for class <code>java.util.Formatter</code>, | |
330 <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html">https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html</a>. | |
331 </p> | |
332 | |
333 <a name="csharp_002dformat"></a> | |
334 <a name="SEC272"></a> | |
335 <h3 class="subsection"> <a href="gettext_toc.html#TOC265">15.3.6 C# Format Strings</a> </h3> | |
336 | |
337 <p>C# format strings are described in the .NET documentation for class | |
338 <code>System.String</code> and in | |
339 <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpConFormattingOverview.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpConFormattingOverview.asp</a>. | |
340 </p> | |
341 | |
342 <a name="javascript_002dformat"></a> | |
343 <a name="SEC273"></a> | |
344 <h3 class="subsection"> <a href="gettext_toc.html#TOC266">15.3.7 JavaScript Format Strings</a> </h3> | |
345 | |
346 <p>Although JavaScript specification itself does not define any format | |
347 strings, many JavaScript implementations provide printf-like | |
348 functions. <code>xgettext</code> understands a set of common format strings | |
349 used in popular JavaScript implementations including Gjs, Seed, and | |
350 Node.JS. In such a format string, a directive starts with ‘<samp>%</samp>’ | |
351 and is finished by a specifier: ‘<samp>%</samp>’ denotes a literal percent | |
352 sign, ‘<samp>c</samp>’ denotes a character, ‘<samp>s</samp>’ denotes a string, | |
353 ‘<samp>b</samp>’, ‘<samp>d</samp>’, ‘<samp>o</samp>’, ‘<samp>x</samp>’, ‘<samp>X</samp>’ denote an integer, | |
354 ‘<samp>f</samp>’ denotes floating-point number, ‘<samp>j</samp>’ denotes a JSON | |
355 object. | |
356 </p> | |
357 | |
358 <a name="scheme_002dformat"></a> | |
359 <a name="SEC274"></a> | |
360 <h3 class="subsection"> <a href="gettext_toc.html#TOC267">15.3.8 Scheme Format Strings</a> </h3> | |
361 | |
362 <p>Scheme format strings are documented in the SLIB manual, section | |
363 Format Specification. | |
364 </p> | |
365 | |
366 <a name="lisp_002dformat"></a> | |
367 <a name="SEC275"></a> | |
368 <h3 class="subsection"> <a href="gettext_toc.html#TOC268">15.3.9 Lisp Format Strings</a> </h3> | |
369 | |
370 <p>Lisp format strings are described in the Common Lisp HyperSpec, | |
371 chapter 22.3 Formatted Output, | |
372 <a href="http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_22-3.html">http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_22-3.html</a>. | |
373 </p> | |
374 | |
375 <a name="elisp_002dformat"></a> | |
376 <a name="SEC276"></a> | |
377 <h3 class="subsection"> <a href="gettext_toc.html#TOC269">15.3.10 Emacs Lisp Format Strings</a> </h3> | |
378 | |
379 <p>Emacs Lisp format strings are documented in the Emacs Lisp reference, | |
380 section Formatting Strings, | |
381 <a href="https://www.gnu.org/manual/elisp-manual-21-2.8/html_chapter/elisp_4.html#SEC75">https://www.gnu.org/manual/elisp-manual-21-2.8/html_chapter/elisp_4.html#SEC75</a>. | |
382 Note that as of version 21, XEmacs supports numbered argument specifications | |
383 in format strings while FSF Emacs doesn't. | |
384 </p> | |
385 | |
386 <a name="librep_002dformat"></a> | |
387 <a name="SEC277"></a> | |
388 <h3 class="subsection"> <a href="gettext_toc.html#TOC270">15.3.11 librep Format Strings</a> </h3> | |
389 | |
390 <p>librep format strings are documented in the librep manual, section | |
391 Formatted Output, | |
392 <a href="http://librep.sourceforge.net/librep-manual.html#Formatted%20Output">http://librep.sourceforge.net/librep-manual.html#Formatted%20Output</a>, | |
393 <a href="http://www.gwinnup.org/research/docs/librep.html#SEC122">http://www.gwinnup.org/research/docs/librep.html#SEC122</a>. | |
394 </p> | |
395 | |
396 <a name="ruby_002dformat"></a> | |
397 <a name="SEC278"></a> | |
398 <h3 class="subsection"> <a href="gettext_toc.html#TOC271">15.3.12 Ruby Format Strings</a> </h3> | |
399 | |
400 <p>Ruby format strings are described in the documentation of the Ruby | |
401 functions <code>format</code> and <code>sprintf</code>, in | |
402 <a href="https://ruby-doc.org/core-2.7.1/Kernel.html#method-i-sprintf">https://ruby-doc.org/core-2.7.1/Kernel.html#method-i-sprintf</a>. | |
403 </p> | |
404 <p>There are two kinds of format strings in Ruby: | |
405 </p><ul> | |
406 <li> | |
407 Those that take a list of arguments without names. They support | |
408 argument reordering by use of the <code>%<var>n</var>$</code> syntax. Note | |
409 that if one argument uses this syntax, all must use this syntax. | |
410 </li><li> | |
411 Those that take a hash table, containing named arguments. The | |
412 syntax is <code>%<<var>name</var>></code>. Note that <code>%{<var>name</var>}</code> is | |
413 equivalent to <code>%<<var>name</var>>s</code>. | |
414 </li></ul> | |
415 | |
416 | |
417 <a name="sh_002dformat"></a> | |
418 <a name="SEC279"></a> | |
419 <h3 class="subsection"> <a href="gettext_toc.html#TOC272">15.3.13 Shell Format Strings</a> </h3> | |
420 | |
421 <p>Shell format strings, as supported by GNU gettext and the ‘<samp>envsubst</samp>’ | |
422 program, are strings with references to shell variables in the form | |
423 <code>$<var>variable</var></code> or <code>${<var>variable</var>}</code>. References of the form | |
424 <code>${<var>variable</var>-<var>default</var>}</code>, | |
425 <code>${<var>variable</var>:-<var>default</var>}</code>, | |
426 <code>${<var>variable</var>=<var>default</var>}</code>, | |
427 <code>${<var>variable</var>:=<var>default</var>}</code>, | |
428 <code>${<var>variable</var>+<var>replacement</var>}</code>, | |
429 <code>${<var>variable</var>:+<var>replacement</var>}</code>, | |
430 <code>${<var>variable</var>?<var>ignored</var>}</code>, | |
431 <code>${<var>variable</var>:?<var>ignored</var>}</code>, | |
432 that would be valid inside shell scripts, are not supported. The | |
433 <var>variable</var> names must consist solely of alphanumeric or underscore | |
434 ASCII characters, not start with a digit and be nonempty; otherwise such | |
435 a variable reference is ignored. | |
436 </p> | |
437 | |
438 <a name="awk_002dformat"></a> | |
439 <a name="SEC280"></a> | |
440 <h3 class="subsection"> <a href="gettext_toc.html#TOC273">15.3.14 awk Format Strings</a> </h3> | |
441 | |
442 <p>awk format strings are described in the gawk documentation, section | |
443 Printf, | |
444 <a href="https://www.gnu.org/manual/gawk/html_node/Printf.html#Printf">https://www.gnu.org/manual/gawk/html_node/Printf.html#Printf</a>. | |
445 </p> | |
446 | |
447 <a name="lua_002dformat"></a> | |
448 <a name="SEC281"></a> | |
449 <h3 class="subsection"> <a href="gettext_toc.html#TOC274">15.3.15 Lua Format Strings</a> </h3> | |
450 | |
451 <p>Lua format strings are described in the Lua reference manual, section String Manipulation, | |
452 <a href="https://www.lua.org/manual/5.1/manual.html#pdf-string.format">https://www.lua.org/manual/5.1/manual.html#pdf-string.format</a>. | |
453 </p> | |
454 | |
455 <a name="object_002dpascal_002dformat"></a> | |
456 <a name="SEC282"></a> | |
457 <h3 class="subsection"> <a href="gettext_toc.html#TOC275">15.3.16 Object Pascal Format Strings</a> </h3> | |
458 | |
459 <p>Object Pascal format strings are described in the documentation of the | |
460 Free Pascal runtime library, section Format, | |
461 <a href="https://www.freepascal.org/docs-html/rtl/sysutils/format.html">https://www.freepascal.org/docs-html/rtl/sysutils/format.html</a>. | |
462 </p> | |
463 | |
464 <a name="smalltalk_002dformat"></a> | |
465 <a name="SEC283"></a> | |
466 <h3 class="subsection"> <a href="gettext_toc.html#TOC276">15.3.17 Smalltalk Format Strings</a> </h3> | |
467 | |
468 <p>Smalltalk format strings are described in the GNU Smalltalk documentation, | |
469 class <code>CharArray</code>, methods ‘<samp>bindWith:</samp>’ and | |
470 ‘<samp>bindWithArguments:</samp>’. | |
471 <a href="https://www.gnu.org/software/smalltalk/gst-manual/gst_68.html#SEC238">https://www.gnu.org/software/smalltalk/gst-manual/gst_68.html#SEC238</a>. | |
472 In summary, a directive starts with ‘<samp>%</samp>’ and is followed by ‘<samp>%</samp>’ | |
473 or a nonzero digit (‘<samp>1</samp>’ to ‘<samp>9</samp>’). | |
474 </p> | |
475 | |
476 <a name="qt_002dformat"></a> | |
477 <a name="SEC284"></a> | |
478 <h3 class="subsection"> <a href="gettext_toc.html#TOC277">15.3.18 Qt Format Strings</a> </h3> | |
479 | |
480 <p>Qt format strings are described in the documentation of the QString class | |
481 <a href="file:/usr/lib/qt-4.3.0/doc/html/qstring.html">file:/usr/lib/qt-4.3.0/doc/html/qstring.html</a>. | |
482 In summary, a directive consists of a ‘<samp>%</samp>’ followed by a digit. The same | |
483 directive cannot occur more than once in a format string. | |
484 </p> | |
485 | |
486 <a name="qt_002dplural_002dformat"></a> | |
487 <a name="SEC285"></a> | |
488 <h3 class="subsection"> <a href="gettext_toc.html#TOC278">15.3.19 Qt Format Strings</a> </h3> | |
489 | |
490 <p>Qt format strings are described in the documentation of the QObject::tr method | |
491 <a href="file:/usr/lib/qt-4.3.0/doc/html/qobject.html">file:/usr/lib/qt-4.3.0/doc/html/qobject.html</a>. | |
492 In summary, the only allowed directive is ‘<samp>%n</samp>’. | |
493 </p> | |
494 | |
495 <a name="kde_002dformat"></a> | |
496 <a name="SEC286"></a> | |
497 <h3 class="subsection"> <a href="gettext_toc.html#TOC279">15.3.20 KDE Format Strings</a> </h3> | |
498 | |
499 <p>KDE 4 format strings are defined as follows: | |
500 A directive consists of a ‘<samp>%</samp>’ followed by a non-zero decimal number. | |
501 If a ‘<samp>%n</samp>’ occurs in a format strings, all of ‘<samp>%1</samp>’, ..., ‘<samp>%(n-1)</samp>’ | |
502 must occur as well, except possibly one of them. | |
503 </p> | |
504 | |
505 <a name="kde_002dkuit_002dformat"></a> | |
506 <a name="SEC287"></a> | |
507 <h3 class="subsection"> <a href="gettext_toc.html#TOC280">15.3.21 KUIT Format Strings</a> </h3> | |
508 | |
509 <p>KUIT (KDE User Interface Text) is compatible with KDE 4 format strings, | |
510 while it also allows programmers to add semantic information to a format | |
511 string, through XML markup tags. For example, if the first format | |
512 directive in a string is a filename, programmers could indicate that | |
513 with a ‘<samp>filename</samp>’ tag, like ‘<samp><filename>%1</filename></samp>’. | |
514 </p> | |
515 <p>KUIT format strings are described in | |
516 <a href="https://api.kde.org/frameworks/ki18n/html/prg_guide.html#kuit_markup">https://api.kde.org/frameworks/ki18n/html/prg_guide.html#kuit_markup</a>. | |
517 </p> | |
518 | |
519 <a name="boost_002dformat"></a> | |
520 <a name="SEC288"></a> | |
521 <h3 class="subsection"> <a href="gettext_toc.html#TOC281">15.3.22 Boost Format Strings</a> </h3> | |
522 | |
523 <p>Boost format strings are described in the documentation of the | |
524 <code>boost::format</code> class, at | |
525 <a href="https://www.boost.org/libs/format/doc/format.html">https://www.boost.org/libs/format/doc/format.html</a>. | |
526 In summary, a directive has either the same syntax as in a C format string, | |
527 such as ‘<samp>%1$+5d</samp>’, or may be surrounded by vertical bars, such as | |
528 ‘<samp>%|1$+5d|</samp>’ or ‘<samp>%|1$+5|</samp>’, or consists of just an argument number | |
529 between percent signs, such as ‘<samp>%1%</samp>’. | |
530 </p> | |
531 | |
532 <a name="tcl_002dformat"></a> | |
533 <a name="SEC289"></a> | |
534 <h3 class="subsection"> <a href="gettext_toc.html#TOC282">15.3.23 Tcl Format Strings</a> </h3> | |
535 | |
536 <p>Tcl format strings are described in the ‘<tt>format.n</tt>’ manual page, | |
537 <a href="http://www.scriptics.com/man/tcl8.3/TclCmd/format.htm">http://www.scriptics.com/man/tcl8.3/TclCmd/format.htm</a>. | |
538 </p> | |
539 | |
540 <a name="perl_002dformat"></a> | |
541 <a name="SEC290"></a> | |
542 <h3 class="subsection"> <a href="gettext_toc.html#TOC283">15.3.24 Perl Format Strings</a> </h3> | |
543 | |
544 <p>There are two kinds of format strings in Perl: those acceptable to the | |
545 Perl built-in function <code>printf</code>, labelled as ‘<samp>perl-format</samp>’, | |
546 and those acceptable to the <code>libintl-perl</code> function <code>__x</code>, | |
547 labelled as ‘<samp>perl-brace-format</samp>’. | |
548 </p> | |
549 <p>Perl <code>printf</code> format strings are described in the <code>sprintf</code> | |
550 section of ‘<samp>man perlfunc</samp>’. | |
551 </p> | |
552 <p>Perl brace format strings are described in the | |
553 ‘<tt>Locale::TextDomain(3pm)</tt>’ manual page of the CPAN package | |
554 libintl-perl. In brief, Perl format uses placeholders put between | |
555 braces (‘<samp>{</samp>’ and ‘<samp>}</samp>’). The placeholder must have the syntax | |
556 of simple identifiers. | |
557 </p> | |
558 | |
559 <a name="php_002dformat"></a> | |
560 <a name="SEC291"></a> | |
561 <h3 class="subsection"> <a href="gettext_toc.html#TOC284">15.3.25 PHP Format Strings</a> </h3> | |
562 | |
563 <p>PHP format strings are described in the documentation of the PHP function | |
564 <code>sprintf</code>, in ‘<tt>phpdoc/manual/function.sprintf.html</tt>’ or | |
565 <a href="http://www.php.net/manual/en/function.sprintf.php">http://www.php.net/manual/en/function.sprintf.php</a>. | |
566 </p> | |
567 | |
568 <a name="gcc_002dinternal_002dformat"></a> | |
569 <a name="SEC292"></a> | |
570 <h3 class="subsection"> <a href="gettext_toc.html#TOC285">15.3.26 GCC internal Format Strings</a> </h3> | |
571 | |
572 <p>These format strings are used inside the GCC sources. In such a format | |
573 string, a directive starts with ‘<samp>%</samp>’, is optionally followed by a | |
574 size specifier ‘<samp>l</samp>’, an optional flag ‘<samp>+</samp>’, another optional flag | |
575 ‘<samp>#</samp>’, and is finished by a specifier: ‘<samp>%</samp>’ denotes a literal | |
576 percent sign, ‘<samp>c</samp>’ denotes a character, ‘<samp>s</samp>’ denotes a string, | |
577 ‘<samp>i</samp>’ and ‘<samp>d</samp>’ denote an integer, ‘<samp>o</samp>’, ‘<samp>u</samp>’, ‘<samp>x</samp>’ | |
578 denote an unsigned integer, ‘<samp>.*s</samp>’ denotes a string preceded by a | |
579 width specification, ‘<samp>H</samp>’ denotes a ‘<samp>location_t *</samp>’ pointer, | |
580 ‘<samp>D</samp>’ denotes a general declaration, ‘<samp>F</samp>’ denotes a function | |
581 declaration, ‘<samp>T</samp>’ denotes a type, ‘<samp>A</samp>’ denotes a function argument, | |
582 ‘<samp>C</samp>’ denotes a tree code, ‘<samp>E</samp>’ denotes an expression, ‘<samp>L</samp>’ | |
583 denotes a programming language, ‘<samp>O</samp>’ denotes a binary operator, | |
584 ‘<samp>P</samp>’ denotes a function parameter, ‘<samp>Q</samp>’ denotes an assignment | |
585 operator, ‘<samp>V</samp>’ denotes a const/volatile qualifier. | |
586 </p> | |
587 | |
588 <a name="gfc_002dinternal_002dformat"></a> | |
589 <a name="SEC293"></a> | |
590 <h3 class="subsection"> <a href="gettext_toc.html#TOC286">15.3.27 GFC internal Format Strings</a> </h3> | |
591 | |
592 <p>These format strings are used inside the GNU Fortran Compiler sources, | |
593 that is, the Fortran frontend in the GCC sources. In such a format | |
594 string, a directive starts with ‘<samp>%</samp>’ and is finished by a | |
595 specifier: ‘<samp>%</samp>’ denotes a literal percent sign, ‘<samp>C</samp>’ denotes the | |
596 current source location, ‘<samp>L</samp>’ denotes a source location, ‘<samp>c</samp>’ | |
597 denotes a character, ‘<samp>s</samp>’ denotes a string, ‘<samp>i</samp>’ and ‘<samp>d</samp>’ | |
598 denote an integer, ‘<samp>u</samp>’ denotes an unsigned integer. ‘<samp>i</samp>’, | |
599 ‘<samp>d</samp>’, and ‘<samp>u</samp>’ may be preceded by a size specifier ‘<samp>l</samp>’. | |
600 </p> | |
601 | |
602 <a name="ycp_002dformat"></a> | |
603 <a name="SEC294"></a> | |
604 <h3 class="subsection"> <a href="gettext_toc.html#TOC287">15.3.28 YCP Format Strings</a> </h3> | |
605 | |
606 <p>YCP sformat strings are described in the libycp documentation | |
607 <a href="file:/usr/share/doc/packages/libycp/YCP-builtins.html">file:/usr/share/doc/packages/libycp/YCP-builtins.html</a>. | |
608 In summary, a directive starts with ‘<samp>%</samp>’ and is followed by ‘<samp>%</samp>’ | |
609 or a nonzero digit (‘<samp>1</samp>’ to ‘<samp>9</samp>’). | |
610 </p> | |
611 | |
612 | |
613 <a name="Maintainers-for-other-Languages"></a> | |
614 <a name="SEC295"></a> | |
615 <h2 class="section"> <a href="gettext_toc.html#TOC288">15.4 The Maintainer's View</a> </h2> | |
616 | |
617 <p>For the maintainer, the general procedure differs from the C language | |
618 case: | |
619 </p> | |
620 <ul> | |
621 <li> | |
622 If only a single programming language is used, the <code>XGETTEXT_OPTIONS</code> | |
623 variable in ‘<tt>po/Makevars</tt>’ (see section <a href="gettext_13.html#SEC237">‘<tt>Makevars</tt>’ in ‘<tt>po/</tt>’</a>) should be adjusted to | |
624 match the <code>xgettext</code> options for that particular programming language. | |
625 If the package uses more than one programming language with <code>gettext</code> | |
626 support, it becomes necessary to change the POT file construction rule | |
627 in ‘<tt>po/Makefile.in.in</tt>’. It is recommended to make one <code>xgettext</code> | |
628 invocation per programming language, each with the options appropriate for | |
629 that language, and to combine the resulting files using <code>msgcat</code>. | |
630 </li></ul> | |
631 | |
632 | |
633 <a name="List-of-Programming-Languages"></a> | |
634 <a name="SEC296"></a> | |
635 <h2 class="section"> <a href="gettext_toc.html#TOC289">15.5 Individual Programming Languages</a> </h2> | |
636 | |
637 | |
638 | |
639 | |
640 | |
641 <a name="C"></a> | |
642 <a name="SEC297"></a> | |
643 <h3 class="subsection"> <a href="gettext_toc.html#TOC290">15.5.1 C, C++, Objective C</a> </h3> | |
644 | |
645 <dl compact="compact"> | |
646 <dt> RPMs</dt> | |
647 <dd><p>gcc, gpp, gobjc, glibc, gettext | |
648 </p> | |
649 </dd> | |
650 <dt> Ubuntu packages</dt> | |
651 <dd><p>gcc, g++, gobjc, libc6-dev, libasprintf-dev | |
652 </p> | |
653 </dd> | |
654 <dt> File extension</dt> | |
655 <dd><p>For C: <code>c</code>, <code>h</code>. | |
656 <br>For C++: <code>C</code>, <code>c++</code>, <code>cc</code>, <code>cxx</code>, <code>cpp</code>, <code>hpp</code>. | |
657 <br>For Objective C: <code>m</code>. | |
658 </p> | |
659 </dd> | |
660 <dt> String syntax</dt> | |
661 <dd><p><code>"abc"</code> | |
662 </p> | |
663 </dd> | |
664 <dt> gettext shorthand</dt> | |
665 <dd><p><code>_("abc")</code> | |
666 </p> | |
667 </dd> | |
668 <dt> gettext/ngettext functions</dt> | |
669 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code>, <code>ngettext</code>, | |
670 <code>dngettext</code>, <code>dcngettext</code> | |
671 </p> | |
672 </dd> | |
673 <dt> textdomain</dt> | |
674 <dd><p><code>textdomain</code> function | |
675 </p> | |
676 </dd> | |
677 <dt> bindtextdomain</dt> | |
678 <dd><p><code>bindtextdomain</code> and <code>wbindtextdomain</code> functions | |
679 </p> | |
680 </dd> | |
681 <dt> setlocale</dt> | |
682 <dd><p>Programmer must call <code>setlocale (LC_ALL, "")</code> | |
683 </p> | |
684 </dd> | |
685 <dt> Prerequisite</dt> | |
686 <dd><p><code>#include <libintl.h></code> | |
687 <br><code>#include <locale.h></code> | |
688 <br><code>#define _(string) gettext (string)</code> | |
689 </p> | |
690 </dd> | |
691 <dt> Use or emulate GNU gettext</dt> | |
692 <dd><p>Use | |
693 </p> | |
694 </dd> | |
695 <dt> Extractor</dt> | |
696 <dd><p><code>xgettext -k_</code> | |
697 </p> | |
698 </dd> | |
699 <dt> Formatting with positions</dt> | |
700 <dd><p><code>fprintf "%2$d %1$d"</code> | |
701 <br>In C++: <code>autosprintf "%2$d %1$d"</code> | |
702 (see <a href="../autosprintf/index.html#Top">(autosprintf)Top</a> section `Introduction' in <cite>GNU autosprintf</cite>) | |
703 <br>In C++ 20 or newer: <code>std::vformat "{1} {0}"</code> | |
704 </p> | |
705 </dd> | |
706 <dt> Portability</dt> | |
707 <dd><p>autoconf (gettext.m4) and #if ENABLE_NLS | |
708 </p> | |
709 </dd> | |
710 <dt> po-mode marking</dt> | |
711 <dd><p>yes | |
712 </p></dd> | |
713 </dl> | |
714 | |
715 <p>The following examples are available in the ‘<tt>examples</tt>’ directory: | |
716 <code>hello-c</code>, <code>hello-c-gnome</code>, <code>hello-c++</code>, <code>hello-c++-qt</code>, | |
717 <code>hello-c++-kde</code>, <code>hello-c++-gnome</code>, <code>hello-c++-wxwidgets</code>, | |
718 <code>hello-objc</code>, <code>hello-objc-gnustep</code>, <code>hello-objc-gnome</code>. | |
719 </p> | |
720 | |
721 <a name="Python"></a> | |
722 <a name="SEC298"></a> | |
723 <h3 class="subsection"> <a href="gettext_toc.html#TOC291">15.5.2 Python</a> </h3> | |
724 | |
725 <dl compact="compact"> | |
726 <dt> RPMs</dt> | |
727 <dd><p>python | |
728 </p> | |
729 </dd> | |
730 <dt> Ubuntu packages</dt> | |
731 <dd><p>python | |
732 </p> | |
733 </dd> | |
734 <dt> File extension</dt> | |
735 <dd><p><code>py</code> | |
736 </p> | |
737 </dd> | |
738 <dt> String syntax</dt> | |
739 <dd><p><code>'abc'</code>, <code>u'abc'</code>, <code>r'abc'</code>, <code>ur'abc'</code>, | |
740 <br><code>"abc"</code>, <code>u"abc"</code>, <code>r"abc"</code>, <code>ur"abc"</code>, | |
741 <br><code>'''abc'''</code>, <code>u'''abc'''</code>, <code>r'''abc'''</code>, <code>ur'''abc'''</code>, | |
742 <br><code>"""abc"""</code>, <code>u"""abc"""</code>, <code>r"""abc"""</code>, <code>ur"""abc"""</code> | |
743 </p> | |
744 </dd> | |
745 <dt> gettext shorthand</dt> | |
746 <dd><p><code>_('abc')</code> etc. | |
747 </p> | |
748 </dd> | |
749 <dt> gettext/ngettext functions</dt> | |
750 <dd><p><code>gettext.gettext</code>, <code>gettext.dgettext</code>, | |
751 <code>gettext.ngettext</code>, <code>gettext.dngettext</code>, | |
752 also <code>ugettext</code>, <code>ungettext</code> | |
753 </p> | |
754 </dd> | |
755 <dt> textdomain</dt> | |
756 <dd><p><code>gettext.textdomain</code> function, or | |
757 <code>gettext.install(<var>domain</var>)</code> function | |
758 </p> | |
759 </dd> | |
760 <dt> bindtextdomain</dt> | |
761 <dd><p><code>gettext.bindtextdomain</code> function, or | |
762 <code>gettext.install(<var>domain</var>,<var>localedir</var>)</code> function | |
763 </p> | |
764 </dd> | |
765 <dt> setlocale</dt> | |
766 <dd><p>not used by the gettext emulation | |
767 </p> | |
768 </dd> | |
769 <dt> Prerequisite</dt> | |
770 <dd><p><code>import gettext</code> | |
771 </p> | |
772 </dd> | |
773 <dt> Use or emulate GNU gettext</dt> | |
774 <dd><p>emulate | |
775 </p> | |
776 </dd> | |
777 <dt> Extractor</dt> | |
778 <dd><p><code>xgettext</code> | |
779 </p> | |
780 </dd> | |
781 <dt> Formatting with positions</dt> | |
782 <dd><p><code>'...%(ident)d...' % { 'ident': value }</code> | |
783 <br><code>'...{ident}...'.format(ident=value)</code> (see PEP 3101) | |
784 </p> | |
785 </dd> | |
786 <dt> Portability</dt> | |
787 <dd><p>fully portable | |
788 </p> | |
789 </dd> | |
790 <dt> po-mode marking</dt> | |
791 <dd><p>— | |
792 </p></dd> | |
793 </dl> | |
794 | |
795 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-python</code>. | |
796 </p> | |
797 <p>A note about format strings: Python supports format strings with unnamed | |
798 arguments, such as <code>'...%d...'</code>, and format strings with named arguments, | |
799 such as <code>'...%(ident)d...'</code>. The latter are preferable for | |
800 internationalized programs, for two reasons: | |
801 </p> | |
802 <ul> | |
803 <li> | |
804 When a format string takes more than one argument, the translator can provide | |
805 a translation that uses the arguments in a different order, if the format | |
806 string uses named arguments. For example, the translator can reformulate | |
807 <table><tr><td> </td><td><pre class="smallexample">"'%(volume)s' has only %(freespace)d bytes free." | |
808 </pre></td></tr></table> | |
809 <p>to | |
810 </p><table><tr><td> </td><td><pre class="smallexample">"Only %(freespace)d bytes free on '%(volume)s'." | |
811 </pre></td></tr></table> | |
812 <p>Additionally, the identifiers also provide some context to the translator. | |
813 </p> | |
814 </li><li> | |
815 In the context of plural forms, the format string used for the singular form | |
816 does not use the numeric argument in many languages. Even in English, one | |
817 prefers to write <code>"one hour"</code> instead of <code>"1 hour"</code>. Omitting | |
818 individual arguments from format strings like this is only possible with | |
819 the named argument syntax. (With unnamed arguments, Python – unlike C – | |
820 verifies that the format string uses all supplied arguments.) | |
821 </li></ul> | |
822 | |
823 | |
824 <a name="Java"></a> | |
825 <a name="SEC299"></a> | |
826 <h3 class="subsection"> <a href="gettext_toc.html#TOC292">15.5.3 Java</a> </h3> | |
827 | |
828 <dl compact="compact"> | |
829 <dt> RPMs</dt> | |
830 <dd><p>java, java2 | |
831 </p> | |
832 </dd> | |
833 <dt> Ubuntu packages</dt> | |
834 <dd><p>default-jdk | |
835 </p> | |
836 </dd> | |
837 <dt> File extension</dt> | |
838 <dd><p><code>java</code> | |
839 </p> | |
840 </dd> | |
841 <dt> String syntax</dt> | |
842 <dd><p>"abc", """text block""" | |
843 </p> | |
844 </dd> | |
845 <dt> gettext shorthand</dt> | |
846 <dd><p>i18n("abc") | |
847 </p> | |
848 </dd> | |
849 <dt> gettext/ngettext functions</dt> | |
850 <dd><p><code>GettextResource.gettext</code>, <code>GettextResource.ngettext</code>, | |
851 <code>GettextResource.pgettext</code>, <code>GettextResource.npgettext</code> | |
852 </p> | |
853 </dd> | |
854 <dt> textdomain</dt> | |
855 <dd><p>—, use <code>ResourceBundle.getResource</code> instead | |
856 </p> | |
857 </dd> | |
858 <dt> bindtextdomain</dt> | |
859 <dd><p>—, use CLASSPATH instead | |
860 </p> | |
861 </dd> | |
862 <dt> setlocale</dt> | |
863 <dd><p>automatic | |
864 </p> | |
865 </dd> | |
866 <dt> Prerequisite</dt> | |
867 <dd><p>— | |
868 </p> | |
869 </dd> | |
870 <dt> Use or emulate GNU gettext</dt> | |
871 <dd><p>—, uses a Java specific message catalog format | |
872 </p> | |
873 </dd> | |
874 <dt> Extractor</dt> | |
875 <dd><p><code>xgettext -ki18n</code> | |
876 </p> | |
877 </dd> | |
878 <dt> Formatting with positions</dt> | |
879 <dd><p><code>MessageFormat.format "{1,number} {0,number}"</code> | |
880 or <code>String.format "%2$d %1$d"</code> | |
881 </p> | |
882 </dd> | |
883 <dt> Portability</dt> | |
884 <dd><p>fully portable | |
885 </p> | |
886 </dd> | |
887 <dt> po-mode marking</dt> | |
888 <dd><p>— | |
889 </p></dd> | |
890 </dl> | |
891 | |
892 <p>Before marking strings as internationalizable, uses of the string | |
893 concatenation operator need to be converted to <code>MessageFormat</code> | |
894 applications. For example, <code>"file "+filename+" not found"</code> becomes | |
895 <code>MessageFormat.format("file {0} not found", new Object[] { filename })</code>. | |
896 Only after this is done, can the strings be marked and extracted. | |
897 </p> | |
898 <p>GNU gettext uses the native Java internationalization mechanism, namely | |
899 <code>ResourceBundle</code>s. There are two formats of <code>ResourceBundle</code>s: | |
900 <code>.properties</code> files and <code>.class</code> files. The <code>.properties</code> | |
901 format is a text file which the translators can directly edit, like PO | |
902 files, but which doesn't support plural forms. Whereas the <code>.class</code> | |
903 format is compiled from <code>.java</code> source code and can support plural | |
904 forms (provided it is accessed through an appropriate API, see below). | |
905 </p> | |
906 <p>To convert a PO file to a <code>.properties</code> file, the <code>msgcat</code> | |
907 program can be used with the option <code>--properties-output</code>. To convert | |
908 a <code>.properties</code> file back to a PO file, the <code>msgcat</code> program | |
909 can be used with the option <code>--properties-input</code>. All the tools | |
910 that manipulate PO files can work with <code>.properties</code> files as well, | |
911 if given the <code>--properties-input</code> and/or <code>--properties-output</code> | |
912 option. | |
913 </p> | |
914 <p>To convert a PO file to a ResourceBundle class, the <code>msgfmt</code> program | |
915 can be used with the option <code>--java</code> or <code>--java2</code>. To convert a | |
916 ResourceBundle back to a PO file, the <code>msgunfmt</code> program can be used | |
917 with the option <code>--java</code>. | |
918 </p> | |
919 <p>Two different programmatic APIs can be used to access ResourceBundles. | |
920 Note that both APIs work with all kinds of ResourceBundles, whether | |
921 GNU gettext generated classes, or other <code>.class</code> or <code>.properties</code> | |
922 files. | |
923 </p> | |
924 <ol> | |
925 <li> | |
926 The <code>java.util.ResourceBundle</code> API. | |
927 | |
928 <p>In particular, its <code>getString</code> function returns a string translation. | |
929 Note that a missing translation yields a <code>MissingResourceException</code>. | |
930 </p> | |
931 <p>This has the advantage of being the standard API. And it does not require | |
932 any additional libraries, only the <code>msgcat</code> generated <code>.properties</code> | |
933 files or the <code>msgfmt</code> generated <code>.class</code> files. But it cannot do | |
934 plural handling, even if the resource was generated by <code>msgfmt</code> from | |
935 a PO file with plural handling. | |
936 </p> | |
937 </li><li> | |
938 The <code>gnu.gettext.GettextResource</code> API. | |
939 | |
940 <p>Reference documentation in Javadoc 1.1 style format is in the | |
941 <a href="javadoc2/index.html">javadoc2 directory</a>. | |
942 </p> | |
943 <p>Its <code>gettext</code> function returns a string translation. Note that when | |
944 a translation is missing, the <var>msgid</var> argument is returned unchanged. | |
945 </p> | |
946 <p>This has the advantage of having the <code>ngettext</code> function for plural | |
947 handling and the <code>pgettext</code> and <code>npgettext</code> for strings constraint | |
948 to a particular context. | |
949 </p> | |
950 <a name="IDX1116"></a> | |
951 <p>To use this API, one needs the <code>libintl.jar</code> file which is part of | |
952 the GNU gettext package and distributed under the LGPL. | |
953 </p></li></ol> | |
954 | |
955 <p>Four examples, using the second API, are available in the ‘<tt>examples</tt>’ | |
956 directory: <code>hello-java</code>, <code>hello-java-awt</code>, <code>hello-java-swing</code>, | |
957 <code>hello-java-qtjambi</code>. | |
958 </p> | |
959 <p>Now, to make use of the API and define a shorthand for ‘<samp>getString</samp>’, | |
960 there are three idioms that you can choose from: | |
961 </p> | |
962 <ul> | |
963 <li> | |
964 (This one assumes Java 1.5 or newer.) | |
965 In a unique class of your project, say ‘<samp>Util</samp>’, define a static variable | |
966 holding the <code>ResourceBundle</code> instance and the shorthand: | |
967 | |
968 <table><tr><td> </td><td><pre class="smallexample">private static ResourceBundle myResources = | |
969 ResourceBundle.getBundle("domain-name"); | |
970 public static String i18n(String s) { | |
971 return myResources.getString(s); | |
972 } | |
973 </pre></td></tr></table> | |
974 | |
975 <p>All classes containing internationalized strings then contain | |
976 </p> | |
977 <table><tr><td> </td><td><pre class="smallexample">import static Util.i18n; | |
978 </pre></td></tr></table> | |
979 | |
980 <p>and the shorthand is used like this: | |
981 </p> | |
982 <table><tr><td> </td><td><pre class="smallexample">System.out.println(i18n("Operation completed.")); | |
983 </pre></td></tr></table> | |
984 | |
985 </li><li> | |
986 In a unique class of your project, say ‘<samp>Util</samp>’, define a static variable | |
987 holding the <code>ResourceBundle</code> instance: | |
988 | |
989 <table><tr><td> </td><td><pre class="smallexample">public static ResourceBundle myResources = | |
990 ResourceBundle.getBundle("domain-name"); | |
991 </pre></td></tr></table> | |
992 | |
993 <p>All classes containing internationalized strings then contain | |
994 </p> | |
995 <table><tr><td> </td><td><pre class="smallexample">private static ResourceBundle res = Util.myResources; | |
996 private static String i18n(String s) { return res.getString(s); } | |
997 </pre></td></tr></table> | |
998 | |
999 <p>and the shorthand is used like this: | |
1000 </p> | |
1001 <table><tr><td> </td><td><pre class="smallexample">System.out.println(i18n("Operation completed.")); | |
1002 </pre></td></tr></table> | |
1003 | |
1004 </li><li> | |
1005 You add a class with a very short name, say ‘<samp>S</samp>’, containing just the | |
1006 definition of the resource bundle and of the shorthand: | |
1007 | |
1008 <table><tr><td> </td><td><pre class="smallexample">public class S { | |
1009 public static ResourceBundle myResources = | |
1010 ResourceBundle.getBundle("domain-name"); | |
1011 public static String i18n(String s) { | |
1012 return myResources.getString(s); | |
1013 } | |
1014 } | |
1015 </pre></td></tr></table> | |
1016 | |
1017 <p>and the shorthand is used like this: | |
1018 </p> | |
1019 <table><tr><td> </td><td><pre class="smallexample">System.out.println(S.i18n("Operation completed.")); | |
1020 </pre></td></tr></table> | |
1021 </li></ul> | |
1022 | |
1023 <p>Which of the three idioms you choose, will depend on whether your project | |
1024 requires portability to Java versions prior to Java 1.5 and, if so, whether | |
1025 copying two lines of codes into every class is more acceptable in your project | |
1026 than a class with a single-letter name. | |
1027 </p> | |
1028 | |
1029 <a name="C_0023"></a> | |
1030 <a name="SEC300"></a> | |
1031 <h3 class="subsection"> <a href="gettext_toc.html#TOC293">15.5.4 C#</a> </h3> | |
1032 | |
1033 <dl compact="compact"> | |
1034 <dt> RPMs</dt> | |
1035 <dd><p>mono | |
1036 </p> | |
1037 </dd> | |
1038 <dt> Ubuntu packages</dt> | |
1039 <dd><p>mono-mcs | |
1040 </p> | |
1041 </dd> | |
1042 <dt> File extension</dt> | |
1043 <dd><p><code>cs</code> | |
1044 </p> | |
1045 </dd> | |
1046 <dt> String syntax</dt> | |
1047 <dd><p><code>"abc"</code>, <code>@"abc"</code> | |
1048 </p> | |
1049 </dd> | |
1050 <dt> gettext shorthand</dt> | |
1051 <dd><p>_("abc") | |
1052 </p> | |
1053 </dd> | |
1054 <dt> gettext/ngettext functions</dt> | |
1055 <dd><p><code>GettextResourceManager.GetString</code>, | |
1056 <code>GettextResourceManager.GetPluralString</code> | |
1057 <code>GettextResourceManager.GetParticularString</code> | |
1058 <code>GettextResourceManager.GetParticularPluralString</code> | |
1059 </p> | |
1060 </dd> | |
1061 <dt> textdomain</dt> | |
1062 <dd><p><code>new GettextResourceManager(domain)</code> | |
1063 </p> | |
1064 </dd> | |
1065 <dt> bindtextdomain</dt> | |
1066 <dd><p>—, compiled message catalogs are located in subdirectories of the directory | |
1067 containing the executable | |
1068 </p> | |
1069 </dd> | |
1070 <dt> setlocale</dt> | |
1071 <dd><p>automatic | |
1072 </p> | |
1073 </dd> | |
1074 <dt> Prerequisite</dt> | |
1075 <dd><p>— | |
1076 </p> | |
1077 </dd> | |
1078 <dt> Use or emulate GNU gettext</dt> | |
1079 <dd><p>—, uses a C# specific message catalog format | |
1080 </p> | |
1081 </dd> | |
1082 <dt> Extractor</dt> | |
1083 <dd><p><code>xgettext -k_</code> | |
1084 </p> | |
1085 </dd> | |
1086 <dt> Formatting with positions</dt> | |
1087 <dd><p><code>String.Format "{1} {0}"</code> | |
1088 </p> | |
1089 </dd> | |
1090 <dt> Portability</dt> | |
1091 <dd><p>fully portable | |
1092 </p> | |
1093 </dd> | |
1094 <dt> po-mode marking</dt> | |
1095 <dd><p>— | |
1096 </p></dd> | |
1097 </dl> | |
1098 | |
1099 <p>Before marking strings as internationalizable, uses of the string | |
1100 concatenation operator need to be converted to <code>String.Format</code> | |
1101 invocations. For example, <code>"file "+filename+" not found"</code> becomes | |
1102 <code>String.Format("file {0} not found", filename)</code>. | |
1103 Only after this is done, can the strings be marked and extracted. | |
1104 </p> | |
1105 <p>GNU gettext uses the native C#/.NET internationalization mechanism, namely | |
1106 the classes <code>ResourceManager</code> and <code>ResourceSet</code>. Applications | |
1107 use the <code>ResourceManager</code> methods to retrieve the native language | |
1108 translation of strings. An instance of <code>ResourceSet</code> is the in-memory | |
1109 representation of a message catalog file. The <code>ResourceManager</code> loads | |
1110 and accesses <code>ResourceSet</code> instances as needed to look up the | |
1111 translations. | |
1112 </p> | |
1113 <p>There are two formats of <code>ResourceSet</code>s that can be directly loaded by | |
1114 the C# runtime: <code>.resources</code> files and <code>.dll</code> files. | |
1115 </p> | |
1116 <ul> | |
1117 <li> | |
1118 The <code>.resources</code> format is a binary file usually generated through the | |
1119 <code>resgen</code> or <code>monoresgen</code> utility, but which doesn't support plural | |
1120 forms. <code>.resources</code> files can also be embedded in .NET <code>.exe</code> files. | |
1121 This only affects whether a file system access is performed to load the message | |
1122 catalog; it doesn't affect the contents of the message catalog. | |
1123 | |
1124 </li><li> | |
1125 On the other hand, the <code>.dll</code> format is a binary file that is compiled | |
1126 from <code>.cs</code> source code and can support plural forms (provided it is | |
1127 accessed through the GNU gettext API, see below). | |
1128 </li></ul> | |
1129 | |
1130 <p>Note that these .NET <code>.dll</code> and <code>.exe</code> files are not tied to a | |
1131 particular platform; their file format and GNU gettext for C# can be used | |
1132 on any platform. | |
1133 </p> | |
1134 <p>To convert a PO file to a <code>.resources</code> file, the <code>msgfmt</code> program | |
1135 can be used with the option ‘<samp>--csharp-resources</samp>’. To convert a | |
1136 <code>.resources</code> file back to a PO file, the <code>msgunfmt</code> program can be | |
1137 used with the option ‘<samp>--csharp-resources</samp>’. You can also, in some cases, | |
1138 use the <code>monoresgen</code> program (from the <code>mono</code>/<code>mcs</code> package). | |
1139 This program can also convert a <code>.resources</code> file back to a PO file. But | |
1140 beware: as of this writing (January 2004), the <code>monoresgen</code> converter is | |
1141 quite buggy. | |
1142 </p> | |
1143 <p>To convert a PO file to a <code>.dll</code> file, the <code>msgfmt</code> program can be | |
1144 used with the option <code>--csharp</code>. The result will be a <code>.dll</code> file | |
1145 containing a subclass of <code>GettextResourceSet</code>, which itself is a subclass | |
1146 of <code>ResourceSet</code>. To convert a <code>.dll</code> file containing a | |
1147 <code>GettextResourceSet</code> subclass back to a PO file, the <code>msgunfmt</code> | |
1148 program can be used with the option <code>--csharp</code>. | |
1149 </p> | |
1150 <p>The advantages of the <code>.dll</code> format over the <code>.resources</code> format | |
1151 are: | |
1152 </p> | |
1153 <ol> | |
1154 <li> | |
1155 Freedom to localize: Users can add their own translations to an application | |
1156 after it has been built and distributed. Whereas when the programmer uses | |
1157 a <code>ResourceManager</code> constructor provided by the system, the set of | |
1158 <code>.resources</code> files for an application must be specified when the | |
1159 application is built and cannot be extended afterwards. | |
1160 | |
1161 </li><li> | |
1162 Plural handling: A message catalog in <code>.dll</code> format supports the plural | |
1163 handling function <code>GetPluralString</code>. Whereas <code>.resources</code> files can | |
1164 only contain data and only support lookups that depend on a single string. | |
1165 | |
1166 </li><li> | |
1167 Context handling: A message catalog in <code>.dll</code> format supports the | |
1168 query-with-context functions <code>GetParticularString</code> and | |
1169 <code>GetParticularPluralString</code>. Whereas <code>.resources</code> files can | |
1170 only contain data and only support lookups that depend on a single string. | |
1171 | |
1172 </li><li> | |
1173 The <code>GettextResourceManager</code> that loads the message catalogs in | |
1174 <code>.dll</code> format also provides for inheritance on a per-message basis. | |
1175 For example, in Austrian (<code>de_AT</code>) locale, translations from the German | |
1176 (<code>de</code>) message catalog will be used for messages not found in the | |
1177 Austrian message catalog. This has the consequence that the Austrian | |
1178 translators need only translate those few messages for which the translation | |
1179 into Austrian differs from the German one. Whereas when working with | |
1180 <code>.resources</code> files, each message catalog must provide the translations | |
1181 of all messages by itself. | |
1182 | |
1183 </li><li> | |
1184 The <code>GettextResourceManager</code> that loads the message catalogs in | |
1185 <code>.dll</code> format also provides for a fallback: The English <var>msgid</var> is | |
1186 returned when no translation can be found. Whereas when working with | |
1187 <code>.resources</code> files, a language-neutral <code>.resources</code> file must | |
1188 explicitly be provided as a fallback. | |
1189 </li></ol> | |
1190 | |
1191 <p>On the side of the programmatic APIs, the programmer can use either the | |
1192 standard <code>ResourceManager</code> API and the GNU <code>GettextResourceManager</code> | |
1193 API. The latter is an extension of the former, because | |
1194 <code>GettextResourceManager</code> is a subclass of <code>ResourceManager</code>. | |
1195 </p> | |
1196 <ol> | |
1197 <li> | |
1198 The <code>System.Resources.ResourceManager</code> API. | |
1199 | |
1200 <p>This API works with resources in <code>.resources</code> format. | |
1201 </p> | |
1202 <p>The creation of the <code>ResourceManager</code> is done through | |
1203 </p><table><tr><td> </td><td><pre class="smallexample"> new ResourceManager(domainname, Assembly.GetExecutingAssembly()) | |
1204 </pre></td></tr></table> | |
1205 | |
1206 <p>The <code>GetString</code> function returns a string's translation. Note that this | |
1207 function returns null when a translation is missing (i.e. not even found in | |
1208 the fallback resource file). | |
1209 </p> | |
1210 </li><li> | |
1211 The <code>GNU.Gettext.GettextResourceManager</code> API. | |
1212 | |
1213 <p>This API works with resources in <code>.dll</code> format. | |
1214 </p> | |
1215 <p>Reference documentation is in the | |
1216 <a href="csharpdoc/index.html">csharpdoc directory</a>. | |
1217 </p> | |
1218 <p>The creation of the <code>ResourceManager</code> is done through | |
1219 </p><table><tr><td> </td><td><pre class="smallexample"> new GettextResourceManager(domainname) | |
1220 </pre></td></tr></table> | |
1221 | |
1222 <p>The <code>GetString</code> function returns a string's translation. Note that when | |
1223 a translation is missing, the <var>msgid</var> argument is returned unchanged. | |
1224 </p> | |
1225 <p>The <code>GetPluralString</code> function returns a string translation with plural | |
1226 handling, like the <code>ngettext</code> function in C. | |
1227 </p> | |
1228 <p>The <code>GetParticularString</code> function returns a string's translation, | |
1229 specific to a particular context, like the <code>pgettext</code> function in C. | |
1230 Note that when a translation is missing, the <var>msgid</var> argument is returned | |
1231 unchanged. | |
1232 </p> | |
1233 <p>The <code>GetParticularPluralString</code> function returns a string translation, | |
1234 specific to a particular context, with plural handling, like the | |
1235 <code>npgettext</code> function in C. | |
1236 </p> | |
1237 <a name="IDX1117"></a> | |
1238 <p>To use this API, one needs the <code>GNU.Gettext.dll</code> file which is part of | |
1239 the GNU gettext package and distributed under the LGPL. | |
1240 </p></li></ol> | |
1241 | |
1242 <p>You can also mix both approaches: use the | |
1243 <code>GNU.Gettext.GettextResourceManager</code> constructor, but otherwise use | |
1244 only the <code>ResourceManager</code> type and only the <code>GetString</code> method. | |
1245 This is appropriate when you want to profit from the tools for PO files, | |
1246 but don't want to change an existing source code that uses | |
1247 <code>ResourceManager</code> and don't (yet) need the <code>GetPluralString</code> method. | |
1248 </p> | |
1249 <p>Two examples, using the second API, are available in the ‘<tt>examples</tt>’ | |
1250 directory: <code>hello-csharp</code>, <code>hello-csharp-forms</code>. | |
1251 </p> | |
1252 <p>Now, to make use of the API and define a shorthand for ‘<samp>GetString</samp>’, | |
1253 there are two idioms that you can choose from: | |
1254 </p> | |
1255 <ul> | |
1256 <li> | |
1257 In a unique class of your project, say ‘<samp>Util</samp>’, define a static variable | |
1258 holding the <code>ResourceManager</code> instance: | |
1259 | |
1260 <table><tr><td> </td><td><pre class="smallexample">public static GettextResourceManager MyResourceManager = | |
1261 new GettextResourceManager("domain-name"); | |
1262 </pre></td></tr></table> | |
1263 | |
1264 <p>All classes containing internationalized strings then contain | |
1265 </p> | |
1266 <table><tr><td> </td><td><pre class="smallexample">private static GettextResourceManager Res = Util.MyResourceManager; | |
1267 private static String _(String s) { return Res.GetString(s); } | |
1268 </pre></td></tr></table> | |
1269 | |
1270 <p>and the shorthand is used like this: | |
1271 </p> | |
1272 <table><tr><td> </td><td><pre class="smallexample">Console.WriteLine(_("Operation completed.")); | |
1273 </pre></td></tr></table> | |
1274 | |
1275 </li><li> | |
1276 You add a class with a very short name, say ‘<samp>S</samp>’, containing just the | |
1277 definition of the resource manager and of the shorthand: | |
1278 | |
1279 <table><tr><td> </td><td><pre class="smallexample">public class S { | |
1280 public static GettextResourceManager MyResourceManager = | |
1281 new GettextResourceManager("domain-name"); | |
1282 public static String _(String s) { | |
1283 return MyResourceManager.GetString(s); | |
1284 } | |
1285 } | |
1286 </pre></td></tr></table> | |
1287 | |
1288 <p>and the shorthand is used like this: | |
1289 </p> | |
1290 <table><tr><td> </td><td><pre class="smallexample">Console.WriteLine(S._("Operation completed.")); | |
1291 </pre></td></tr></table> | |
1292 </li></ul> | |
1293 | |
1294 <p>Which of the two idioms you choose, will depend on whether copying two lines | |
1295 of codes into every class is more acceptable in your project than a class | |
1296 with a single-letter name. | |
1297 </p> | |
1298 | |
1299 <a name="JavaScript"></a> | |
1300 <a name="SEC301"></a> | |
1301 <h3 class="subsection"> <a href="gettext_toc.html#TOC294">15.5.5 JavaScript</a> </h3> | |
1302 | |
1303 <dl compact="compact"> | |
1304 <dt> RPMs</dt> | |
1305 <dd><p>js | |
1306 </p> | |
1307 </dd> | |
1308 <dt> Ubuntu packages</dt> | |
1309 <dd><p>gjs | |
1310 </p> | |
1311 </dd> | |
1312 <dt> File extension</dt> | |
1313 <dd><p><code>js</code> | |
1314 </p> | |
1315 </dd> | |
1316 <dt> String syntax</dt> | |
1317 <dd><ul> | |
1318 <li> <code>"abc"</code> | |
1319 | |
1320 </li><li> <code>'abc'</code> | |
1321 | |
1322 </li><li> <code>`abc`</code> | |
1323 | |
1324 </li></ul> | |
1325 | |
1326 </dd> | |
1327 <dt> gettext shorthand</dt> | |
1328 <dd><p><code>_("abc")</code> | |
1329 </p> | |
1330 </dd> | |
1331 <dt> gettext/ngettext functions</dt> | |
1332 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code>, <code>ngettext</code>, | |
1333 <code>dngettext</code> | |
1334 </p> | |
1335 </dd> | |
1336 <dt> textdomain</dt> | |
1337 <dd><p><code>textdomain</code> function | |
1338 </p> | |
1339 </dd> | |
1340 <dt> bindtextdomain</dt> | |
1341 <dd><p><code>bindtextdomain</code> function | |
1342 </p> | |
1343 </dd> | |
1344 <dt> setlocale</dt> | |
1345 <dd><p>automatic | |
1346 </p> | |
1347 </dd> | |
1348 <dt> Prerequisite</dt> | |
1349 <dd><p>— | |
1350 </p> | |
1351 </dd> | |
1352 <dt> Use or emulate GNU gettext</dt> | |
1353 <dd><p>use, or emulate | |
1354 </p> | |
1355 </dd> | |
1356 <dt> Extractor</dt> | |
1357 <dd><p><code>xgettext</code> | |
1358 </p> | |
1359 </dd> | |
1360 <dt> Formatting with positions</dt> | |
1361 <dd><p>— | |
1362 </p> | |
1363 </dd> | |
1364 <dt> Portability</dt> | |
1365 <dd><p>On platforms without gettext, the functions are not available. | |
1366 </p> | |
1367 </dd> | |
1368 <dt> po-mode marking</dt> | |
1369 <dd><p>— | |
1370 </p></dd> | |
1371 </dl> | |
1372 | |
1373 | |
1374 <a name="Scheme"></a> | |
1375 <a name="SEC302"></a> | |
1376 <h3 class="subsection"> <a href="gettext_toc.html#TOC295">15.5.6 GNU guile - Scheme</a> </h3> | |
1377 | |
1378 <dl compact="compact"> | |
1379 <dt> RPMs</dt> | |
1380 <dd><p>guile | |
1381 </p> | |
1382 </dd> | |
1383 <dt> Ubuntu packages</dt> | |
1384 <dd><p>guile-2.0 | |
1385 </p> | |
1386 </dd> | |
1387 <dt> File extension</dt> | |
1388 <dd><p><code>scm</code> | |
1389 </p> | |
1390 </dd> | |
1391 <dt> String syntax</dt> | |
1392 <dd><p><code>"abc"</code> | |
1393 </p> | |
1394 </dd> | |
1395 <dt> gettext shorthand</dt> | |
1396 <dd><p><code>(_ "abc")</code>, <code>_"abc"</code> (GIMP script-fu extension) | |
1397 </p> | |
1398 </dd> | |
1399 <dt> gettext/ngettext functions</dt> | |
1400 <dd><p><code>gettext</code>, <code>ngettext</code> | |
1401 </p> | |
1402 </dd> | |
1403 <dt> textdomain</dt> | |
1404 <dd><p><code>textdomain</code> | |
1405 </p> | |
1406 </dd> | |
1407 <dt> bindtextdomain</dt> | |
1408 <dd><p><code>bindtextdomain</code> | |
1409 </p> | |
1410 </dd> | |
1411 <dt> setlocale</dt> | |
1412 <dd><p><code>(catch #t (lambda () (setlocale LC_ALL "")) (lambda args #f))</code> | |
1413 </p> | |
1414 </dd> | |
1415 <dt> Prerequisite</dt> | |
1416 <dd><p><code>(use-modules (ice-9 format))</code> | |
1417 </p> | |
1418 </dd> | |
1419 <dt> Use or emulate GNU gettext</dt> | |
1420 <dd><p>use | |
1421 </p> | |
1422 </dd> | |
1423 <dt> Extractor</dt> | |
1424 <dd><p><code>xgettext -k_</code> | |
1425 </p> | |
1426 </dd> | |
1427 <dt> Formatting with positions</dt> | |
1428 <dd><p>— | |
1429 </p> | |
1430 </dd> | |
1431 <dt> Portability</dt> | |
1432 <dd><p>On platforms without gettext, no translation. | |
1433 </p> | |
1434 </dd> | |
1435 <dt> po-mode marking</dt> | |
1436 <dd><p>— | |
1437 </p></dd> | |
1438 </dl> | |
1439 | |
1440 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-guile</code>. | |
1441 </p> | |
1442 | |
1443 <a name="Common-Lisp"></a> | |
1444 <a name="SEC303"></a> | |
1445 <h3 class="subsection"> <a href="gettext_toc.html#TOC296">15.5.7 GNU clisp - Common Lisp</a> </h3> | |
1446 | |
1447 <dl compact="compact"> | |
1448 <dt> RPMs</dt> | |
1449 <dd><p>clisp 2.28 or newer | |
1450 </p> | |
1451 </dd> | |
1452 <dt> Ubuntu packages</dt> | |
1453 <dd><p>clisp | |
1454 </p> | |
1455 </dd> | |
1456 <dt> File extension</dt> | |
1457 <dd><p><code>lisp</code> | |
1458 </p> | |
1459 </dd> | |
1460 <dt> String syntax</dt> | |
1461 <dd><p><code>"abc"</code> | |
1462 </p> | |
1463 </dd> | |
1464 <dt> gettext shorthand</dt> | |
1465 <dd><p><code>(_ "abc")</code>, <code>(ENGLISH "abc")</code> | |
1466 </p> | |
1467 </dd> | |
1468 <dt> gettext/ngettext functions</dt> | |
1469 <dd><p><code>i18n:gettext</code>, <code>i18n:ngettext</code> | |
1470 </p> | |
1471 </dd> | |
1472 <dt> textdomain</dt> | |
1473 <dd><p><code>i18n:textdomain</code> | |
1474 </p> | |
1475 </dd> | |
1476 <dt> bindtextdomain</dt> | |
1477 <dd><p><code>i18n:textdomaindir</code> | |
1478 </p> | |
1479 </dd> | |
1480 <dt> setlocale</dt> | |
1481 <dd><p>automatic | |
1482 </p> | |
1483 </dd> | |
1484 <dt> Prerequisite</dt> | |
1485 <dd><p>— | |
1486 </p> | |
1487 </dd> | |
1488 <dt> Use or emulate GNU gettext</dt> | |
1489 <dd><p>use | |
1490 </p> | |
1491 </dd> | |
1492 <dt> Extractor</dt> | |
1493 <dd><p><code>xgettext -k_ -kENGLISH</code> | |
1494 </p> | |
1495 </dd> | |
1496 <dt> Formatting with positions</dt> | |
1497 <dd><p><code>format "~1@*~D ~0@*~D"</code> | |
1498 </p> | |
1499 </dd> | |
1500 <dt> Portability</dt> | |
1501 <dd><p>On platforms without gettext, no translation. | |
1502 </p> | |
1503 </dd> | |
1504 <dt> po-mode marking</dt> | |
1505 <dd><p>— | |
1506 </p></dd> | |
1507 </dl> | |
1508 | |
1509 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-clisp</code>. | |
1510 </p> | |
1511 | |
1512 <a name="clisp-C"></a> | |
1513 <a name="SEC304"></a> | |
1514 <h3 class="subsection"> <a href="gettext_toc.html#TOC297">15.5.8 GNU clisp C sources</a> </h3> | |
1515 | |
1516 <dl compact="compact"> | |
1517 <dt> RPMs</dt> | |
1518 <dd><p>clisp | |
1519 </p> | |
1520 </dd> | |
1521 <dt> Ubuntu packages</dt> | |
1522 <dd><p>clisp | |
1523 </p> | |
1524 </dd> | |
1525 <dt> File extension</dt> | |
1526 <dd><p><code>d</code> | |
1527 </p> | |
1528 </dd> | |
1529 <dt> String syntax</dt> | |
1530 <dd><p><code>"abc"</code> | |
1531 </p> | |
1532 </dd> | |
1533 <dt> gettext shorthand</dt> | |
1534 <dd><p><code>ENGLISH ? "abc" : ""</code> | |
1535 <br><code>GETTEXT("abc")</code> | |
1536 <br><code>GETTEXTL("abc")</code> | |
1537 </p> | |
1538 </dd> | |
1539 <dt> gettext/ngettext functions</dt> | |
1540 <dd><p><code>clgettext</code>, <code>clgettextl</code> | |
1541 </p> | |
1542 </dd> | |
1543 <dt> textdomain</dt> | |
1544 <dd><p>— | |
1545 </p> | |
1546 </dd> | |
1547 <dt> bindtextdomain</dt> | |
1548 <dd><p>— | |
1549 </p> | |
1550 </dd> | |
1551 <dt> setlocale</dt> | |
1552 <dd><p>automatic | |
1553 </p> | |
1554 </dd> | |
1555 <dt> Prerequisite</dt> | |
1556 <dd><p><code>#include "lispbibl.c"</code> | |
1557 </p> | |
1558 </dd> | |
1559 <dt> Use or emulate GNU gettext</dt> | |
1560 <dd><p>use | |
1561 </p> | |
1562 </dd> | |
1563 <dt> Extractor</dt> | |
1564 <dd><p><code>clisp-xgettext</code> | |
1565 </p> | |
1566 </dd> | |
1567 <dt> Formatting with positions</dt> | |
1568 <dd><p><code>fprintf "%2$d %1$d"</code> | |
1569 </p> | |
1570 </dd> | |
1571 <dt> Portability</dt> | |
1572 <dd><p>On platforms without gettext, no translation. | |
1573 </p> | |
1574 </dd> | |
1575 <dt> po-mode marking</dt> | |
1576 <dd><p>— | |
1577 </p></dd> | |
1578 </dl> | |
1579 | |
1580 | |
1581 <a name="Emacs-Lisp"></a> | |
1582 <a name="SEC305"></a> | |
1583 <h3 class="subsection"> <a href="gettext_toc.html#TOC298">15.5.9 Emacs Lisp</a> </h3> | |
1584 | |
1585 <dl compact="compact"> | |
1586 <dt> RPMs</dt> | |
1587 <dd><p>emacs, xemacs | |
1588 </p> | |
1589 </dd> | |
1590 <dt> Ubuntu packages</dt> | |
1591 <dd><p>emacs, xemacs21 | |
1592 </p> | |
1593 </dd> | |
1594 <dt> File extension</dt> | |
1595 <dd><p><code>el</code> | |
1596 </p> | |
1597 </dd> | |
1598 <dt> String syntax</dt> | |
1599 <dd><p><code>"abc"</code> | |
1600 </p> | |
1601 </dd> | |
1602 <dt> gettext shorthand</dt> | |
1603 <dd><p><code>(_"abc")</code> | |
1604 </p> | |
1605 </dd> | |
1606 <dt> gettext/ngettext functions</dt> | |
1607 <dd><p><code>gettext</code>, <code>dgettext</code> (xemacs only) | |
1608 </p> | |
1609 </dd> | |
1610 <dt> textdomain</dt> | |
1611 <dd><p><code>domain</code> special form (xemacs only) | |
1612 </p> | |
1613 </dd> | |
1614 <dt> bindtextdomain</dt> | |
1615 <dd><p><code>bind-text-domain</code> function (xemacs only) | |
1616 </p> | |
1617 </dd> | |
1618 <dt> setlocale</dt> | |
1619 <dd><p>automatic | |
1620 </p> | |
1621 </dd> | |
1622 <dt> Prerequisite</dt> | |
1623 <dd><p>— | |
1624 </p> | |
1625 </dd> | |
1626 <dt> Use or emulate GNU gettext</dt> | |
1627 <dd><p>use | |
1628 </p> | |
1629 </dd> | |
1630 <dt> Extractor</dt> | |
1631 <dd><p><code>xgettext</code> | |
1632 </p> | |
1633 </dd> | |
1634 <dt> Formatting with positions</dt> | |
1635 <dd><p><code>format "%2$d %1$d"</code> | |
1636 </p> | |
1637 </dd> | |
1638 <dt> Portability</dt> | |
1639 <dd><p>Only XEmacs. Without <code>I18N3</code> defined at build time, no translation. | |
1640 </p> | |
1641 </dd> | |
1642 <dt> po-mode marking</dt> | |
1643 <dd><p>— | |
1644 </p></dd> | |
1645 </dl> | |
1646 | |
1647 | |
1648 <a name="librep"></a> | |
1649 <a name="SEC306"></a> | |
1650 <h3 class="subsection"> <a href="gettext_toc.html#TOC299">15.5.10 librep</a> </h3> | |
1651 | |
1652 <dl compact="compact"> | |
1653 <dt> RPMs</dt> | |
1654 <dd><p>librep 0.15.3 or newer | |
1655 </p> | |
1656 </dd> | |
1657 <dt> Ubuntu packages</dt> | |
1658 <dd><p>librep16 | |
1659 </p> | |
1660 </dd> | |
1661 <dt> File extension</dt> | |
1662 <dd><p><code>jl</code> | |
1663 </p> | |
1664 </dd> | |
1665 <dt> String syntax</dt> | |
1666 <dd><p><code>"abc"</code> | |
1667 </p> | |
1668 </dd> | |
1669 <dt> gettext shorthand</dt> | |
1670 <dd><p><code>(_"abc")</code> | |
1671 </p> | |
1672 </dd> | |
1673 <dt> gettext/ngettext functions</dt> | |
1674 <dd><p><code>gettext</code> | |
1675 </p> | |
1676 </dd> | |
1677 <dt> textdomain</dt> | |
1678 <dd><p><code>textdomain</code> function | |
1679 </p> | |
1680 </dd> | |
1681 <dt> bindtextdomain</dt> | |
1682 <dd><p><code>bindtextdomain</code> function | |
1683 </p> | |
1684 </dd> | |
1685 <dt> setlocale</dt> | |
1686 <dd><p>— | |
1687 </p> | |
1688 </dd> | |
1689 <dt> Prerequisite</dt> | |
1690 <dd><p><code>(require 'rep.i18n.gettext)</code> | |
1691 </p> | |
1692 </dd> | |
1693 <dt> Use or emulate GNU gettext</dt> | |
1694 <dd><p>use | |
1695 </p> | |
1696 </dd> | |
1697 <dt> Extractor</dt> | |
1698 <dd><p><code>xgettext</code> | |
1699 </p> | |
1700 </dd> | |
1701 <dt> Formatting with positions</dt> | |
1702 <dd><p><code>format "%2$d %1$d"</code> | |
1703 </p> | |
1704 </dd> | |
1705 <dt> Portability</dt> | |
1706 <dd><p>On platforms without gettext, no translation. | |
1707 </p> | |
1708 </dd> | |
1709 <dt> po-mode marking</dt> | |
1710 <dd><p>— | |
1711 </p></dd> | |
1712 </dl> | |
1713 | |
1714 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-librep</code>. | |
1715 </p> | |
1716 | |
1717 <a name="Ruby"></a> | |
1718 <a name="SEC307"></a> | |
1719 <h3 class="subsection"> <a href="gettext_toc.html#TOC300">15.5.11 Ruby</a> </h3> | |
1720 | |
1721 <dl compact="compact"> | |
1722 <dt> RPMs</dt> | |
1723 <dd><p>ruby, ruby-gettext | |
1724 </p> | |
1725 </dd> | |
1726 <dt> Ubuntu packages</dt> | |
1727 <dd><p>ruby, ruby-gettext | |
1728 </p> | |
1729 </dd> | |
1730 <dt> File extension</dt> | |
1731 <dd><p><code>rb</code> | |
1732 </p> | |
1733 </dd> | |
1734 <dt> String syntax</dt> | |
1735 <dd><p><code>"abc"</code>, <code>'abc'</code>, <code>%q/abc/</code> etc., | |
1736 <code>%q(abc)</code>, <code>%q[abc]</code>, <code>%q{abc}</code> | |
1737 </p> | |
1738 </dd> | |
1739 <dt> gettext shorthand</dt> | |
1740 <dd><p><code>_("abc")</code> | |
1741 </p> | |
1742 </dd> | |
1743 <dt> gettext/ngettext functions</dt> | |
1744 <dd><p><code>gettext</code>, <code>ngettext</code> | |
1745 </p> | |
1746 </dd> | |
1747 <dt> textdomain</dt> | |
1748 <dd><p>— | |
1749 </p> | |
1750 </dd> | |
1751 <dt> bindtextdomain</dt> | |
1752 <dd><p><code>bindtextdomain</code> function | |
1753 </p> | |
1754 </dd> | |
1755 <dt> setlocale</dt> | |
1756 <dd><p>— | |
1757 </p> | |
1758 </dd> | |
1759 <dt> Prerequisite</dt> | |
1760 <dd><p><code>require 'gettext'</code> | |
1761 <code>include GetText</code> | |
1762 </p> | |
1763 </dd> | |
1764 <dt> Use or emulate GNU gettext</dt> | |
1765 <dd><p>emulate | |
1766 </p> | |
1767 </dd> | |
1768 <dt> Extractor</dt> | |
1769 <dd><p><code>xgettext</code> | |
1770 </p> | |
1771 </dd> | |
1772 <dt> Formatting with positions</dt> | |
1773 <dd><p><code>sprintf("%2$d %1$d", x, y)</code> | |
1774 <br><code>"%{new} replaces %{old}" % {:old => oldvalue, :new => newvalue}</code> | |
1775 </p> | |
1776 </dd> | |
1777 <dt> Portability</dt> | |
1778 <dd><p>fully portable | |
1779 </p> | |
1780 </dd> | |
1781 <dt> po-mode marking</dt> | |
1782 <dd><p>— | |
1783 </p></dd> | |
1784 </dl> | |
1785 | |
1786 | |
1787 | |
1788 <a name="sh"></a> | |
1789 <a name="SEC308"></a> | |
1790 <h3 class="subsection"> <a href="gettext_toc.html#TOC301">15.5.12 sh - Shell Script</a> </h3> | |
1791 | |
1792 <dl compact="compact"> | |
1793 <dt> RPMs</dt> | |
1794 <dd><p>bash, gettext | |
1795 </p> | |
1796 </dd> | |
1797 <dt> Ubuntu packages</dt> | |
1798 <dd><p>bash, gettext-base | |
1799 </p> | |
1800 </dd> | |
1801 <dt> File extension</dt> | |
1802 <dd><p><code>sh</code> | |
1803 </p> | |
1804 </dd> | |
1805 <dt> String syntax</dt> | |
1806 <dd><p><code>"abc"</code>, <code>'abc'</code>, <code>abc</code> | |
1807 </p> | |
1808 </dd> | |
1809 <dt> gettext shorthand</dt> | |
1810 <dd><p><code>"`gettext \"abc\"`"</code> | |
1811 </p> | |
1812 </dd> | |
1813 <dt> gettext/ngettext functions</dt> | |
1814 <dd><a name="IDX1118"></a> | |
1815 <a name="IDX1119"></a> | |
1816 <p><code>gettext</code>, <code>ngettext</code> programs | |
1817 <br><code>eval_gettext</code>, <code>eval_ngettext</code>, <code>eval_pgettext</code>, | |
1818 <code>eval_npgettext</code> shell functions | |
1819 </p> | |
1820 </dd> | |
1821 <dt> textdomain</dt> | |
1822 <dd><a name="IDX1120"></a> | |
1823 <p>environment variable <code>TEXTDOMAIN</code> | |
1824 </p> | |
1825 </dd> | |
1826 <dt> bindtextdomain</dt> | |
1827 <dd><a name="IDX1121"></a> | |
1828 <p>environment variable <code>TEXTDOMAINDIR</code> | |
1829 </p> | |
1830 </dd> | |
1831 <dt> setlocale</dt> | |
1832 <dd><p>automatic | |
1833 </p> | |
1834 </dd> | |
1835 <dt> Prerequisite</dt> | |
1836 <dd><p><code>. gettext.sh</code> | |
1837 </p> | |
1838 </dd> | |
1839 <dt> Use or emulate GNU gettext</dt> | |
1840 <dd><p>use | |
1841 </p> | |
1842 </dd> | |
1843 <dt> Extractor</dt> | |
1844 <dd><p><code>xgettext</code> | |
1845 </p> | |
1846 </dd> | |
1847 <dt> Formatting with positions</dt> | |
1848 <dd><p>— | |
1849 </p> | |
1850 </dd> | |
1851 <dt> Portability</dt> | |
1852 <dd><p>fully portable | |
1853 </p> | |
1854 </dd> | |
1855 <dt> po-mode marking</dt> | |
1856 <dd><p>— | |
1857 </p></dd> | |
1858 </dl> | |
1859 | |
1860 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-sh</code>. | |
1861 </p> | |
1862 | |
1863 | |
1864 <a name="Preparing-Shell-Scripts"></a> | |
1865 <a name="SEC309"></a> | |
1866 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC302">15.5.12.1 Preparing Shell Scripts for Internationalization</a> </h4> | |
1867 | |
1868 <p>Preparing a shell script for internationalization is conceptually similar | |
1869 to the steps described in <a href="gettext_4.html#SEC17">Preparing Program Sources</a>. The concrete steps for shell | |
1870 scripts are as follows. | |
1871 </p> | |
1872 <ol> | |
1873 <li> | |
1874 Insert the line | |
1875 | |
1876 <table><tr><td> </td><td><pre class="smallexample">. gettext.sh | |
1877 </pre></td></tr></table> | |
1878 | |
1879 <p>near the top of the script. <code>gettext.sh</code> is a shell function library | |
1880 that provides the functions | |
1881 <code>eval_gettext</code> (see <a href="#SEC314">Invoking the <code>eval_gettext</code> function</a>), | |
1882 <code>eval_ngettext</code> (see <a href="#SEC315">Invoking the <code>eval_ngettext</code> function</a>), | |
1883 <code>eval_pgettext</code> (see <a href="#SEC316">Invoking the <code>eval_pgettext</code> function</a>), and | |
1884 <code>eval_npgettext</code> (see <a href="#SEC317">Invoking the <code>eval_npgettext</code> function</a>). | |
1885 You have to ensure that <code>gettext.sh</code> can be found in the <code>PATH</code>. | |
1886 </p> | |
1887 </li><li> | |
1888 Set and export the <code>TEXTDOMAIN</code> and <code>TEXTDOMAINDIR</code> environment | |
1889 variables. Usually <code>TEXTDOMAIN</code> is the package or program name, and | |
1890 <code>TEXTDOMAINDIR</code> is the absolute pathname corresponding to | |
1891 <code>$prefix/share/locale</code>, where <code>$prefix</code> is the installation location. | |
1892 | |
1893 <table><tr><td> </td><td><pre class="smallexample">TEXTDOMAIN=@PACKAGE@ | |
1894 export TEXTDOMAIN | |
1895 TEXTDOMAINDIR=@LOCALEDIR@ | |
1896 export TEXTDOMAINDIR | |
1897 </pre></td></tr></table> | |
1898 | |
1899 </li><li> | |
1900 Prepare the strings for translation, as described in <a href="gettext_4.html#SEC20">Preparing Translatable Strings</a>. | |
1901 | |
1902 </li><li> | |
1903 Simplify translatable strings so that they don't contain command substitution | |
1904 (<code>"`...`"</code> or <code>"$(...)"</code>), variable access with defaulting (like | |
1905 <code>${<var>variable</var>-<var>default</var>}</code>), access to positional arguments | |
1906 (like <code>$0</code>, <code>$1</code>, ...) or highly volatile shell variables (like | |
1907 <code>$?</code>). This can always be done through simple local code restructuring. | |
1908 For example, | |
1909 | |
1910 <table><tr><td> </td><td><pre class="smallexample">echo "Usage: $0 [OPTION] FILE..." | |
1911 </pre></td></tr></table> | |
1912 | |
1913 <p>becomes | |
1914 </p> | |
1915 <table><tr><td> </td><td><pre class="smallexample">program_name=$0 | |
1916 echo "Usage: $program_name [OPTION] FILE..." | |
1917 </pre></td></tr></table> | |
1918 | |
1919 <p>Similarly, | |
1920 </p> | |
1921 <table><tr><td> </td><td><pre class="smallexample">echo "Remaining files: `ls | wc -l`" | |
1922 </pre></td></tr></table> | |
1923 | |
1924 <p>becomes | |
1925 </p> | |
1926 <table><tr><td> </td><td><pre class="smallexample">filecount="`ls | wc -l`" | |
1927 echo "Remaining files: $filecount" | |
1928 </pre></td></tr></table> | |
1929 | |
1930 </li><li> | |
1931 For each translatable string, change the output command ‘<samp>echo</samp>’ or | |
1932 ‘<samp>$echo</samp>’ to ‘<samp>gettext</samp>’ (if the string contains no references to | |
1933 shell variables) or to ‘<samp>eval_gettext</samp>’ (if it refers to shell variables), | |
1934 followed by a no-argument ‘<samp>echo</samp>’ command (to account for the terminating | |
1935 newline). Similarly, for cases with plural handling, replace a conditional | |
1936 ‘<samp>echo</samp>’ command with an invocation of ‘<samp>ngettext</samp>’ or | |
1937 ‘<samp>eval_ngettext</samp>’, followed by a no-argument ‘<samp>echo</samp>’ command. | |
1938 | |
1939 <p>When doing this, you also need to add an extra backslash before the dollar | |
1940 sign in references to shell variables, so that the ‘<samp>eval_gettext</samp>’ | |
1941 function receives the translatable string before the variable values are | |
1942 substituted into it. For example, | |
1943 </p> | |
1944 <table><tr><td> </td><td><pre class="smallexample">echo "Remaining files: $filecount" | |
1945 </pre></td></tr></table> | |
1946 | |
1947 <p>becomes | |
1948 </p> | |
1949 <table><tr><td> </td><td><pre class="smallexample">eval_gettext "Remaining files: \$filecount"; echo | |
1950 </pre></td></tr></table> | |
1951 | |
1952 <p>If the output command is not ‘<samp>echo</samp>’, you can make it use ‘<samp>echo</samp>’ | |
1953 nevertheless, through the use of backquotes. However, note that inside | |
1954 backquotes, backslashes must be doubled to be effective (because the | |
1955 backquoting eats one level of backslashes). For example, assuming that | |
1956 ‘<samp>error</samp>’ is a shell function that signals an error, | |
1957 </p> | |
1958 <table><tr><td> </td><td><pre class="smallexample">error "file not found: $filename" | |
1959 </pre></td></tr></table> | |
1960 | |
1961 <p>is first transformed into | |
1962 </p> | |
1963 <table><tr><td> </td><td><pre class="smallexample">error "`echo \"file not found: \$filename\"`" | |
1964 </pre></td></tr></table> | |
1965 | |
1966 <p>which then becomes | |
1967 </p> | |
1968 <table><tr><td> </td><td><pre class="smallexample">error "`eval_gettext \"file not found: \\\$filename\"`" | |
1969 </pre></td></tr></table> | |
1970 </li></ol> | |
1971 | |
1972 | |
1973 <a name="gettext_002esh"></a> | |
1974 <a name="SEC310"></a> | |
1975 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC303">15.5.12.2 Contents of <code>gettext.sh</code></a> </h4> | |
1976 | |
1977 <p><code>gettext.sh</code>, contained in the run-time package of GNU gettext, provides | |
1978 the following: | |
1979 </p> | |
1980 <ul> | |
1981 <li> $echo | |
1982 The variable <code>echo</code> is set to a command that outputs its first argument | |
1983 and a newline, without interpreting backslashes in the argument string. | |
1984 | |
1985 </li><li> eval_gettext | |
1986 See <a href="#SEC314">Invoking the <code>eval_gettext</code> function</a>. | |
1987 | |
1988 </li><li> eval_ngettext | |
1989 See <a href="#SEC315">Invoking the <code>eval_ngettext</code> function</a>. | |
1990 | |
1991 </li><li> eval_pgettext | |
1992 See <a href="#SEC316">Invoking the <code>eval_pgettext</code> function</a>. | |
1993 | |
1994 </li><li> eval_npgettext | |
1995 See <a href="#SEC317">Invoking the <code>eval_npgettext</code> function</a>. | |
1996 </li></ul> | |
1997 | |
1998 | |
1999 <a name="gettext-Invocation"></a> | |
2000 <a name="SEC311"></a> | |
2001 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC304">15.5.12.3 Invoking the <code>gettext</code> program</a> </h4> | |
2002 | |
2003 | |
2004 <table><tr><td> </td><td><pre class="example">gettext [<var>option</var>] [[<var>textdomain</var>] <var>msgid</var>] | |
2005 gettext [<var>option</var>] -s [<var>msgid</var>]... | |
2006 </pre></td></tr></table> | |
2007 | |
2008 <a name="IDX1122"></a> | |
2009 <p>The <code>gettext</code> program displays the native language translation of a | |
2010 textual message. | |
2011 </p> | |
2012 <p><strong>Arguments</strong> | |
2013 </p> | |
2014 <dl compact="compact"> | |
2015 <dt> ‘<samp>-c <var>context</var></samp>’</dt> | |
2016 <dt> ‘<samp>--context=<var>context</var></samp>’</dt> | |
2017 <dd><a name="IDX1123"></a> | |
2018 <a name="IDX1124"></a> | |
2019 <p>Specify the context for the messages to be translated. | |
2020 See <a href="gettext_11.html#SEC206">Using contexts for solving ambiguities</a> for details. | |
2021 </p> | |
2022 </dd> | |
2023 <dt> ‘<samp>-d <var>textdomain</var></samp>’</dt> | |
2024 <dt> ‘<samp>--domain=<var>textdomain</var></samp>’</dt> | |
2025 <dd><a name="IDX1125"></a> | |
2026 <a name="IDX1126"></a> | |
2027 <p>Retrieve translated messages from <var>textdomain</var>. Usually a <var>textdomain</var> | |
2028 corresponds to a package, a program, or a module of a program. | |
2029 </p> | |
2030 </dd> | |
2031 <dt> ‘<samp>-e</samp>’</dt> | |
2032 <dd><a name="IDX1127"></a> | |
2033 <p>Enable expansion of some escape sequences. This option is for compatibility | |
2034 with the ‘<samp>echo</samp>’ program or shell built-in. The escape sequences | |
2035 ‘<samp>\a</samp>’, ‘<samp>\b</samp>’, ‘<samp>\c</samp>’, ‘<samp>\f</samp>’, ‘<samp>\n</samp>’, ‘<samp>\r</samp>’, ‘<samp>\t</samp>’, | |
2036 ‘<samp>\v</samp>’, ‘<samp>\\</samp>’, and ‘<samp>\</samp>’ followed by one to three octal digits, are | |
2037 interpreted like the System V ‘<samp>echo</samp>’ program did. | |
2038 </p> | |
2039 </dd> | |
2040 <dt> ‘<samp>-E</samp>’</dt> | |
2041 <dd><a name="IDX1128"></a> | |
2042 <p>This option is only for compatibility with the ‘<samp>echo</samp>’ program or shell | |
2043 built-in. It has no effect. | |
2044 </p> | |
2045 </dd> | |
2046 <dt> ‘<samp>-h</samp>’</dt> | |
2047 <dt> ‘<samp>--help</samp>’</dt> | |
2048 <dd><a name="IDX1129"></a> | |
2049 <a name="IDX1130"></a> | |
2050 <p>Display this help and exit. | |
2051 </p> | |
2052 </dd> | |
2053 <dt> ‘<samp>-n</samp>’</dt> | |
2054 <dd><a name="IDX1131"></a> | |
2055 <p>This option has only an effect if the <code>-s</code> option is given. It | |
2056 suppresses the additional newline at the end. | |
2057 </p> | |
2058 </dd> | |
2059 <dt> ‘<samp>-V</samp>’</dt> | |
2060 <dt> ‘<samp>--version</samp>’</dt> | |
2061 <dd><a name="IDX1132"></a> | |
2062 <a name="IDX1133"></a> | |
2063 <p>Output version information and exit. | |
2064 </p> | |
2065 </dd> | |
2066 <dt> ‘<samp>[<var>textdomain</var>] <var>msgid</var></samp>’</dt> | |
2067 <dd><p>Retrieve translated message corresponding to <var>msgid</var> from <var>textdomain</var>. | |
2068 </p> | |
2069 </dd> | |
2070 </dl> | |
2071 | |
2072 <p>If the <var>textdomain</var> parameter is not given, the domain is determined from | |
2073 the environment variable <code>TEXTDOMAIN</code>. If the message catalog is not | |
2074 found in the regular directory, another location can be specified with the | |
2075 environment variable <code>TEXTDOMAINDIR</code>. | |
2076 </p> | |
2077 <p>When used with the <code>-s</code> option the program behaves like the ‘<samp>echo</samp>’ | |
2078 command. But it does not simply copy its arguments to stdout. Instead those | |
2079 messages found in the selected catalog are translated. Also, a newline is | |
2080 added at the end, unless either the option <code>-n</code> is specified or the | |
2081 option <code>-e</code> is specified and some of the argument strings contains a | |
2082 ‘<samp>\c</samp>’ escape sequence. | |
2083 </p> | |
2084 <p>Note: <code>xgettext</code> supports only the one-argument form of the | |
2085 <code>gettext</code> invocation, where no options are present and the | |
2086 <var>textdomain</var> is implicit, from the environment. | |
2087 </p> | |
2088 | |
2089 <a name="ngettext-Invocation"></a> | |
2090 <a name="SEC312"></a> | |
2091 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC305">15.5.12.4 Invoking the <code>ngettext</code> program</a> </h4> | |
2092 | |
2093 | |
2094 <table><tr><td> </td><td><pre class="example">ngettext [<var>option</var>] [<var>textdomain</var>] <var>msgid</var> <var>msgid-plural</var> <var>count</var> | |
2095 </pre></td></tr></table> | |
2096 | |
2097 <a name="IDX1134"></a> | |
2098 <p>The <code>ngettext</code> program displays the native language translation of a | |
2099 textual message whose grammatical form depends on a number. | |
2100 </p> | |
2101 <p><strong>Arguments</strong> | |
2102 </p> | |
2103 <dl compact="compact"> | |
2104 <dt> ‘<samp>-c <var>context</var></samp>’</dt> | |
2105 <dt> ‘<samp>--context=<var>context</var></samp>’</dt> | |
2106 <dd><a name="IDX1135"></a> | |
2107 <a name="IDX1136"></a> | |
2108 <p>Specify the context for the messages to be translated. | |
2109 See <a href="gettext_11.html#SEC206">Using contexts for solving ambiguities</a> for details. | |
2110 </p> | |
2111 </dd> | |
2112 <dt> ‘<samp>-d <var>textdomain</var></samp>’</dt> | |
2113 <dt> ‘<samp>--domain=<var>textdomain</var></samp>’</dt> | |
2114 <dd><a name="IDX1137"></a> | |
2115 <a name="IDX1138"></a> | |
2116 <p>Retrieve translated messages from <var>textdomain</var>. Usually a <var>textdomain</var> | |
2117 corresponds to a package, a program, or a module of a program. | |
2118 </p> | |
2119 </dd> | |
2120 <dt> ‘<samp>-e</samp>’</dt> | |
2121 <dd><a name="IDX1139"></a> | |
2122 <p>Enable expansion of some escape sequences. This option is for compatibility | |
2123 with the ‘<samp>gettext</samp>’ program. The escape sequences | |
2124 ‘<samp>\a</samp>’, ‘<samp>\b</samp>’, ‘<samp>\f</samp>’, ‘<samp>\n</samp>’, ‘<samp>\r</samp>’, ‘<samp>\t</samp>’, | |
2125 ‘<samp>\v</samp>’, ‘<samp>\\</samp>’, and ‘<samp>\</samp>’ followed by one to three octal digits, are | |
2126 interpreted like the System V ‘<samp>echo</samp>’ program did. | |
2127 </p> | |
2128 </dd> | |
2129 <dt> ‘<samp>-E</samp>’</dt> | |
2130 <dd><a name="IDX1140"></a> | |
2131 <p>This option is only for compatibility with the ‘<samp>gettext</samp>’ program. It has | |
2132 no effect. | |
2133 </p> | |
2134 </dd> | |
2135 <dt> ‘<samp>-h</samp>’</dt> | |
2136 <dt> ‘<samp>--help</samp>’</dt> | |
2137 <dd><a name="IDX1141"></a> | |
2138 <a name="IDX1142"></a> | |
2139 <p>Display this help and exit. | |
2140 </p> | |
2141 </dd> | |
2142 <dt> ‘<samp>-V</samp>’</dt> | |
2143 <dt> ‘<samp>--version</samp>’</dt> | |
2144 <dd><a name="IDX1143"></a> | |
2145 <a name="IDX1144"></a> | |
2146 <p>Output version information and exit. | |
2147 </p> | |
2148 </dd> | |
2149 <dt> ‘<samp><var>textdomain</var></samp>’</dt> | |
2150 <dd><p>Retrieve translated message from <var>textdomain</var>. | |
2151 </p> | |
2152 </dd> | |
2153 <dt> ‘<samp><var>msgid</var> <var>msgid-plural</var></samp>’</dt> | |
2154 <dd><p>Translate <var>msgid</var> (English singular) / <var>msgid-plural</var> (English plural). | |
2155 </p> | |
2156 </dd> | |
2157 <dt> ‘<samp><var>count</var></samp>’</dt> | |
2158 <dd><p>Choose singular/plural form based on this value. | |
2159 </p> | |
2160 </dd> | |
2161 </dl> | |
2162 | |
2163 <p>If the <var>textdomain</var> parameter is not given, the domain is determined from | |
2164 the environment variable <code>TEXTDOMAIN</code>. If the message catalog is not | |
2165 found in the regular directory, another location can be specified with the | |
2166 environment variable <code>TEXTDOMAINDIR</code>. | |
2167 </p> | |
2168 <p>Note: <code>xgettext</code> supports only the three-arguments form of the | |
2169 <code>ngettext</code> invocation, where no options are present and the | |
2170 <var>textdomain</var> is implicit, from the environment. | |
2171 </p> | |
2172 | |
2173 <a name="envsubst-Invocation"></a> | |
2174 <a name="SEC313"></a> | |
2175 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC306">15.5.12.5 Invoking the <code>envsubst</code> program</a> </h4> | |
2176 | |
2177 | |
2178 <table><tr><td> </td><td><pre class="example">envsubst [<var>option</var>] [<var>shell-format</var>] | |
2179 </pre></td></tr></table> | |
2180 | |
2181 <a name="IDX1145"></a> | |
2182 <a name="IDX1146"></a> | |
2183 <a name="IDX1147"></a> | |
2184 <p>The <code>envsubst</code> program substitutes the values of environment variables. | |
2185 </p> | |
2186 <p><strong>Operation mode</strong> | |
2187 </p> | |
2188 <dl compact="compact"> | |
2189 <dt> ‘<samp>-v</samp>’</dt> | |
2190 <dt> ‘<samp>--variables</samp>’</dt> | |
2191 <dd><a name="IDX1148"></a> | |
2192 <a name="IDX1149"></a> | |
2193 <p>Output the variables occurring in <var>shell-format</var>. | |
2194 </p> | |
2195 </dd> | |
2196 </dl> | |
2197 | |
2198 <p><strong>Informative output</strong> | |
2199 </p> | |
2200 <dl compact="compact"> | |
2201 <dt> ‘<samp>-h</samp>’</dt> | |
2202 <dt> ‘<samp>--help</samp>’</dt> | |
2203 <dd><a name="IDX1150"></a> | |
2204 <a name="IDX1151"></a> | |
2205 <p>Display this help and exit. | |
2206 </p> | |
2207 </dd> | |
2208 <dt> ‘<samp>-V</samp>’</dt> | |
2209 <dt> ‘<samp>--version</samp>’</dt> | |
2210 <dd><a name="IDX1152"></a> | |
2211 <a name="IDX1153"></a> | |
2212 <p>Output version information and exit. | |
2213 </p> | |
2214 </dd> | |
2215 </dl> | |
2216 | |
2217 <p>In normal operation mode, standard input is copied to standard output, | |
2218 with references to environment variables of the form <code>$VARIABLE</code> or | |
2219 <code>${VARIABLE}</code> being replaced with the corresponding values. If a | |
2220 <var>shell-format</var> is given, only those environment variables that are | |
2221 referenced in <var>shell-format</var> are substituted; otherwise all environment | |
2222 variables references occurring in standard input are substituted. | |
2223 </p> | |
2224 <p>These substitutions are a subset of the substitutions that a shell performs | |
2225 on unquoted and double-quoted strings. Other kinds of substitutions done | |
2226 by a shell, such as <code>${<var>variable</var>-<var>default</var>}</code> or | |
2227 <code>$(<var>command-list</var>)</code> or <code>`<var>command-list</var>`</code>, are not performed | |
2228 by the <code>envsubst</code> program, due to security reasons. | |
2229 </p> | |
2230 <p>When <code>--variables</code> is used, standard input is ignored, and the output | |
2231 consists of the environment variables that are referenced in | |
2232 <var>shell-format</var>, one per line. | |
2233 </p> | |
2234 | |
2235 <a name="eval_005fgettext-Invocation"></a> | |
2236 <a name="SEC314"></a> | |
2237 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC307">15.5.12.6 Invoking the <code>eval_gettext</code> function</a> </h4> | |
2238 | |
2239 <table><tr><td> </td><td><pre class="example">eval_gettext <var>msgid</var> | |
2240 </pre></td></tr></table> | |
2241 | |
2242 <a name="IDX1154"></a> | |
2243 <p>This function outputs the native language translation of a textual message, | |
2244 performing dollar-substitution on the result. Note that only shell variables | |
2245 mentioned in <var>msgid</var> will be dollar-substituted in the result. | |
2246 </p> | |
2247 | |
2248 <a name="eval_005fngettext-Invocation"></a> | |
2249 <a name="SEC315"></a> | |
2250 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC308">15.5.12.7 Invoking the <code>eval_ngettext</code> function</a> </h4> | |
2251 | |
2252 <table><tr><td> </td><td><pre class="example">eval_ngettext <var>msgid</var> <var>msgid-plural</var> <var>count</var> | |
2253 </pre></td></tr></table> | |
2254 | |
2255 <a name="IDX1155"></a> | |
2256 <p>This function outputs the native language translation of a textual message | |
2257 whose grammatical form depends on a number, performing dollar-substitution | |
2258 on the result. Note that only shell variables mentioned in <var>msgid</var> or | |
2259 <var>msgid-plural</var> will be dollar-substituted in the result. | |
2260 </p> | |
2261 | |
2262 <a name="eval_005fpgettext-Invocation"></a> | |
2263 <a name="SEC316"></a> | |
2264 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC309">15.5.12.8 Invoking the <code>eval_pgettext</code> function</a> </h4> | |
2265 | |
2266 <table><tr><td> </td><td><pre class="example">eval_pgettext <var>msgctxt</var> <var>msgid</var> | |
2267 </pre></td></tr></table> | |
2268 | |
2269 <a name="IDX1156"></a> | |
2270 <p>This function outputs the native language translation of a textual message | |
2271 in the given context <var>msgctxt</var> (see <a href="gettext_11.html#SEC206">Using contexts for solving ambiguities</a>), performing | |
2272 dollar-substitution on the result. Note that only shell variables mentioned | |
2273 in <var>msgid</var> will be dollar-substituted in the result. | |
2274 </p> | |
2275 | |
2276 <a name="eval_005fnpgettext-Invocation"></a> | |
2277 <a name="SEC317"></a> | |
2278 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC310">15.5.12.9 Invoking the <code>eval_npgettext</code> function</a> </h4> | |
2279 | |
2280 <table><tr><td> </td><td><pre class="example">eval_npgettext <var>msgctxt</var> <var>msgid</var> <var>msgid-plural</var> <var>count</var> | |
2281 </pre></td></tr></table> | |
2282 | |
2283 <a name="IDX1157"></a> | |
2284 <p>This function outputs the native language translation of a textual message | |
2285 whose grammatical form depends on a number in the given context <var>msgctxt</var> | |
2286 (see <a href="gettext_11.html#SEC206">Using contexts for solving ambiguities</a>), performing dollar-substitution on the result. Note | |
2287 that only shell variables mentioned in <var>msgid</var> or <var>msgid-plural</var> | |
2288 will be dollar-substituted in the result. | |
2289 </p> | |
2290 | |
2291 <a name="bash"></a> | |
2292 <a name="SEC318"></a> | |
2293 <h3 class="subsection"> <a href="gettext_toc.html#TOC311">15.5.13 bash - Bourne-Again Shell Script</a> </h3> | |
2294 | |
2295 <p>GNU <code>bash</code> 2.0 or newer has a special shorthand for translating a | |
2296 string and substituting variable values in it: <code>$"msgid"</code>. But | |
2297 the use of this construct is <strong>discouraged</strong>, due to the security | |
2298 holes it opens and due to its portability problems. | |
2299 </p> | |
2300 <p>The security holes of <code>$"..."</code> come from the fact that after looking up | |
2301 the translation of the string, <code>bash</code> processes it like it processes | |
2302 any double-quoted string: dollar and backquote processing, like ‘<samp>eval</samp>’ | |
2303 does. | |
2304 </p> | |
2305 <ol> | |
2306 <li> | |
2307 In a locale whose encoding is one of BIG5, BIG5-HKSCS, GBK, GB18030, SHIFT_JIS, | |
2308 JOHAB, some double-byte characters have a second byte whose value is | |
2309 <code>0x60</code>. For example, the byte sequence <code>\xe0\x60</code> is a single | |
2310 character in these locales. Many versions of <code>bash</code> (all versions | |
2311 up to bash-2.05, and newer versions on platforms without <code>mbsrtowcs()</code> | |
2312 function) don't know about character boundaries and see a backquote character | |
2313 where there is only a particular Chinese character. Thus it can start | |
2314 executing part of the translation as a command list. This situation can occur | |
2315 even without the translator being aware of it: if the translator provides | |
2316 translations in the UTF-8 encoding, it is the <code>gettext()</code> function which | |
2317 will, during its conversion from the translator's encoding to the user's | |
2318 locale's encoding, produce the dangerous <code>\x60</code> bytes. | |
2319 | |
2320 </li><li> | |
2321 A translator could - voluntarily or inadvertently - use backquotes | |
2322 <code>"`...`"</code> or dollar-parentheses <code>"$(...)"</code> in her translations. | |
2323 The enclosed strings would be executed as command lists by the shell. | |
2324 </li></ol> | |
2325 | |
2326 <p>The portability problem is that <code>bash</code> must be built with | |
2327 internationalization support; this is normally not the case on systems | |
2328 that don't have the <code>gettext()</code> function in libc. | |
2329 </p> | |
2330 | |
2331 <a name="gawk"></a> | |
2332 <a name="SEC319"></a> | |
2333 <h3 class="subsection"> <a href="gettext_toc.html#TOC312">15.5.14 GNU awk</a> </h3> | |
2334 | |
2335 <dl compact="compact"> | |
2336 <dt> RPMs</dt> | |
2337 <dd><p>gawk 3.1 or newer | |
2338 </p> | |
2339 </dd> | |
2340 <dt> Ubuntu packages</dt> | |
2341 <dd><p>gawk | |
2342 </p> | |
2343 </dd> | |
2344 <dt> File extension</dt> | |
2345 <dd><p><code>awk</code>, <code>gawk</code>, <code>twjr</code>. | |
2346 The file extension <code>twjr</code> is used by TexiWeb Jr | |
2347 (<a href="https://github.com/arnoldrobbins/texiwebjr">https://github.com/arnoldrobbins/texiwebjr</a>). | |
2348 </p> | |
2349 </dd> | |
2350 <dt> String syntax</dt> | |
2351 <dd><p><code>"abc"</code> | |
2352 </p> | |
2353 </dd> | |
2354 <dt> gettext shorthand</dt> | |
2355 <dd><p><code>_"abc"</code> | |
2356 </p> | |
2357 </dd> | |
2358 <dt> gettext/ngettext functions</dt> | |
2359 <dd><p><code>dcgettext</code>, missing <code>dcngettext</code> in gawk-3.1.0 | |
2360 </p> | |
2361 </dd> | |
2362 <dt> textdomain</dt> | |
2363 <dd><p><code>TEXTDOMAIN</code> variable | |
2364 </p> | |
2365 </dd> | |
2366 <dt> bindtextdomain</dt> | |
2367 <dd><p><code>bindtextdomain</code> function | |
2368 </p> | |
2369 </dd> | |
2370 <dt> setlocale</dt> | |
2371 <dd><p>automatic, but missing <code>setlocale (LC_MESSAGES, "")</code> in gawk-3.1.0 | |
2372 </p> | |
2373 </dd> | |
2374 <dt> Prerequisite</dt> | |
2375 <dd><p>— | |
2376 </p> | |
2377 </dd> | |
2378 <dt> Use or emulate GNU gettext</dt> | |
2379 <dd><p>use | |
2380 </p> | |
2381 </dd> | |
2382 <dt> Extractor</dt> | |
2383 <dd><p><code>xgettext</code> | |
2384 </p> | |
2385 </dd> | |
2386 <dt> Formatting with positions</dt> | |
2387 <dd><p><code>printf "%2$d %1$d"</code> (GNU awk only) | |
2388 </p> | |
2389 </dd> | |
2390 <dt> Portability</dt> | |
2391 <dd><p>On platforms without gettext, no translation. On non-GNU awks, you must | |
2392 define <code>dcgettext</code>, <code>dcngettext</code> and <code>bindtextdomain</code> | |
2393 yourself. | |
2394 </p> | |
2395 </dd> | |
2396 <dt> po-mode marking</dt> | |
2397 <dd><p>— | |
2398 </p></dd> | |
2399 </dl> | |
2400 | |
2401 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-gawk</code>. | |
2402 </p> | |
2403 | |
2404 <a name="Lua"></a> | |
2405 <a name="SEC320"></a> | |
2406 <h3 class="subsection"> <a href="gettext_toc.html#TOC313">15.5.15 Lua</a> </h3> | |
2407 | |
2408 <dl compact="compact"> | |
2409 <dt> RPMs</dt> | |
2410 <dd><p>lua | |
2411 </p> | |
2412 </dd> | |
2413 <dt> Ubuntu packages</dt> | |
2414 <dd><p>lua, lua-gettext | |
2415 <br> | |
2416 You need to install the <code>lua-gettext</code> package from | |
2417 <a href="https://gitlab.com/sukhichev/lua-gettext/blob/master/README.us.md">https://gitlab.com/sukhichev/lua-gettext/blob/master/README.us.md</a>. | |
2418 Debian and Ubuntu packages of it are available. Download the | |
2419 appropriate one, and install it through | |
2420 ‘<samp>sudo dpkg -i lua-gettext_0.0_amd64.deb</samp>’. | |
2421 </p> | |
2422 </dd> | |
2423 <dt> File extension</dt> | |
2424 <dd><p><code>lua</code> | |
2425 </p> | |
2426 </dd> | |
2427 <dt> String syntax</dt> | |
2428 <dd><ul> | |
2429 <li> <code>"abc"</code> | |
2430 | |
2431 </li><li> <code>'abc'</code> | |
2432 | |
2433 </li><li> <code>[[abc]]</code> | |
2434 | |
2435 </li><li> <code>[=[abc]=]</code> | |
2436 | |
2437 </li><li> <code>[==[abc]==]</code> | |
2438 | |
2439 </li><li> ... | |
2440 | |
2441 </li></ul> | |
2442 | |
2443 </dd> | |
2444 <dt> gettext shorthand</dt> | |
2445 <dd><p><code>_("abc")</code> | |
2446 </p> | |
2447 </dd> | |
2448 <dt> gettext/ngettext functions</dt> | |
2449 <dd><p><code>gettext.gettext</code>, <code>gettext.dgettext</code>, <code>gettext.dcgettext</code>, | |
2450 <code>gettext.ngettext</code>, <code>gettext.dngettext</code>, <code>gettext.dcngettext</code> | |
2451 </p> | |
2452 </dd> | |
2453 <dt> textdomain</dt> | |
2454 <dd><p><code>textdomain</code> function | |
2455 </p> | |
2456 </dd> | |
2457 <dt> bindtextdomain</dt> | |
2458 <dd><p><code>bindtextdomain</code> function | |
2459 </p> | |
2460 </dd> | |
2461 <dt> setlocale</dt> | |
2462 <dd><p>automatic | |
2463 </p> | |
2464 </dd> | |
2465 <dt> Prerequisite</dt> | |
2466 <dd><p><code>require 'gettext'</code> or running lua interpreter with <code>-l gettext</code> option | |
2467 </p> | |
2468 </dd> | |
2469 <dt> Use or emulate GNU gettext</dt> | |
2470 <dd><p>use | |
2471 </p> | |
2472 </dd> | |
2473 <dt> Extractor</dt> | |
2474 <dd><p><code>xgettext</code> | |
2475 </p> | |
2476 </dd> | |
2477 <dt> Formatting with positions</dt> | |
2478 <dd><p>— | |
2479 </p> | |
2480 </dd> | |
2481 <dt> Portability</dt> | |
2482 <dd><p>On platforms without gettext, the functions are not available. | |
2483 </p> | |
2484 </dd> | |
2485 <dt> po-mode marking</dt> | |
2486 <dd><p>— | |
2487 </p></dd> | |
2488 </dl> | |
2489 | |
2490 | |
2491 <a name="Pascal"></a> | |
2492 <a name="SEC321"></a> | |
2493 <h3 class="subsection"> <a href="gettext_toc.html#TOC314">15.5.16 Pascal - Free Pascal Compiler</a> </h3> | |
2494 | |
2495 <dl compact="compact"> | |
2496 <dt> RPMs</dt> | |
2497 <dd><p>fpk | |
2498 </p> | |
2499 </dd> | |
2500 <dt> Ubuntu packages</dt> | |
2501 <dd><p>fp-compiler, fp-units-fcl | |
2502 </p> | |
2503 </dd> | |
2504 <dt> File extension</dt> | |
2505 <dd><p><code>pp</code>, <code>pas</code> | |
2506 </p> | |
2507 </dd> | |
2508 <dt> String syntax</dt> | |
2509 <dd><p><code>'abc'</code> | |
2510 </p> | |
2511 </dd> | |
2512 <dt> gettext shorthand</dt> | |
2513 <dd><p>automatic | |
2514 </p> | |
2515 </dd> | |
2516 <dt> gettext/ngettext functions</dt> | |
2517 <dd><p>—, use <code>ResourceString</code> data type instead | |
2518 </p> | |
2519 </dd> | |
2520 <dt> textdomain</dt> | |
2521 <dd><p>—, use <code>TranslateResourceStrings</code> function instead | |
2522 </p> | |
2523 </dd> | |
2524 <dt> bindtextdomain</dt> | |
2525 <dd><p>—, use <code>TranslateResourceStrings</code> function instead | |
2526 </p> | |
2527 </dd> | |
2528 <dt> setlocale</dt> | |
2529 <dd><p>automatic, but uses only LANG, not LC_MESSAGES or LC_ALL | |
2530 </p> | |
2531 </dd> | |
2532 <dt> Prerequisite</dt> | |
2533 <dd><p><code>{$mode delphi}</code> or <code>{$mode objfpc}</code><br><code>uses gettext;</code> | |
2534 </p> | |
2535 </dd> | |
2536 <dt> Use or emulate GNU gettext</dt> | |
2537 <dd><p>emulate partially | |
2538 </p> | |
2539 </dd> | |
2540 <dt> Extractor</dt> | |
2541 <dd><p><code>ppc386</code> followed by <code>xgettext</code> or <code>rstconv</code> | |
2542 </p> | |
2543 </dd> | |
2544 <dt> Formatting with positions</dt> | |
2545 <dd><p><code>uses sysutils;</code><br><code>format "%1:d %0:d"</code> | |
2546 </p> | |
2547 </dd> | |
2548 <dt> Portability</dt> | |
2549 <dd><p>? | |
2550 </p> | |
2551 </dd> | |
2552 <dt> po-mode marking</dt> | |
2553 <dd><p>— | |
2554 </p></dd> | |
2555 </dl> | |
2556 | |
2557 <p>The Pascal compiler has special support for the <code>ResourceString</code> data | |
2558 type. It generates a <code>.rst</code> file. This is then converted to a | |
2559 <code>.pot</code> file by use of <code>xgettext</code> or <code>rstconv</code>. At runtime, | |
2560 a <code>.mo</code> file corresponding to translations of this <code>.pot</code> file | |
2561 can be loaded using the <code>TranslateResourceStrings</code> function in the | |
2562 <code>gettext</code> unit. | |
2563 </p> | |
2564 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-pascal</code>. | |
2565 </p> | |
2566 | |
2567 <a name="Smalltalk"></a> | |
2568 <a name="SEC322"></a> | |
2569 <h3 class="subsection"> <a href="gettext_toc.html#TOC315">15.5.17 GNU Smalltalk</a> </h3> | |
2570 | |
2571 <dl compact="compact"> | |
2572 <dt> RPMs</dt> | |
2573 <dd><p>smalltalk | |
2574 </p> | |
2575 </dd> | |
2576 <dt> Ubuntu packages</dt> | |
2577 <dd><p>gnu-smalltalk | |
2578 </p> | |
2579 </dd> | |
2580 <dt> File extension</dt> | |
2581 <dd><p><code>st</code> | |
2582 </p> | |
2583 </dd> | |
2584 <dt> String syntax</dt> | |
2585 <dd><p><code>'abc'</code> | |
2586 </p> | |
2587 </dd> | |
2588 <dt> gettext shorthand</dt> | |
2589 <dd><p><code>NLS ? 'abc'</code> | |
2590 </p> | |
2591 </dd> | |
2592 <dt> gettext/ngettext functions</dt> | |
2593 <dd><p><code>LcMessagesDomain>>#at:</code>, <code>LcMessagesDomain>>#at:plural:with:</code> | |
2594 </p> | |
2595 </dd> | |
2596 <dt> textdomain</dt> | |
2597 <dd><p><code>LcMessages>>#domain:localeDirectory:</code> (returns a <code>LcMessagesDomain</code> | |
2598 object).<br> | |
2599 Example: <code>I18N Locale default messages domain: 'gettext' localeDirectory: /usr/local/share/locale'</code> | |
2600 </p> | |
2601 </dd> | |
2602 <dt> bindtextdomain</dt> | |
2603 <dd><p><code>LcMessages>>#domain:localeDirectory:</code>, see above. | |
2604 </p> | |
2605 </dd> | |
2606 <dt> setlocale</dt> | |
2607 <dd><p>Automatic if you use <code>I18N Locale default</code>. | |
2608 </p> | |
2609 </dd> | |
2610 <dt> Prerequisite</dt> | |
2611 <dd><p><code>PackageLoader fileInPackage: 'I18N'!</code> | |
2612 </p> | |
2613 </dd> | |
2614 <dt> Use or emulate GNU gettext</dt> | |
2615 <dd><p>emulate | |
2616 </p> | |
2617 </dd> | |
2618 <dt> Extractor</dt> | |
2619 <dd><p><code>xgettext</code> | |
2620 </p> | |
2621 </dd> | |
2622 <dt> Formatting with positions</dt> | |
2623 <dd><p><code>'%1 %2' bindWith: 'Hello' with: 'world'</code> | |
2624 </p> | |
2625 </dd> | |
2626 <dt> Portability</dt> | |
2627 <dd><p>fully portable | |
2628 </p> | |
2629 </dd> | |
2630 <dt> po-mode marking</dt> | |
2631 <dd><p>— | |
2632 </p></dd> | |
2633 </dl> | |
2634 | |
2635 <p>An example is available in the ‘<tt>examples</tt>’ directory: | |
2636 <code>hello-smalltalk</code>. | |
2637 </p> | |
2638 | |
2639 <a name="Vala"></a> | |
2640 <a name="SEC323"></a> | |
2641 <h3 class="subsection"> <a href="gettext_toc.html#TOC316">15.5.18 Vala</a> </h3> | |
2642 | |
2643 <dl compact="compact"> | |
2644 <dt> RPMs</dt> | |
2645 <dd><p>vala | |
2646 </p> | |
2647 </dd> | |
2648 <dt> Ubuntu packages</dt> | |
2649 <dd><p>valac | |
2650 </p> | |
2651 </dd> | |
2652 <dt> File extension</dt> | |
2653 <dd><p><code>vala</code> | |
2654 </p> | |
2655 </dd> | |
2656 <dt> String syntax</dt> | |
2657 <dd><ul> | |
2658 <li> <code>"abc"</code> | |
2659 | |
2660 </li><li> <code>"""abc"""</code> | |
2661 | |
2662 </li></ul> | |
2663 | |
2664 </dd> | |
2665 <dt> gettext shorthand</dt> | |
2666 <dd><p><code>_("abc")</code> | |
2667 </p> | |
2668 </dd> | |
2669 <dt> gettext/ngettext functions</dt> | |
2670 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code>, <code>ngettext</code>, | |
2671 <code>dngettext</code>, <code>dpgettext</code>, <code>dpgettext2</code> | |
2672 </p> | |
2673 </dd> | |
2674 <dt> textdomain</dt> | |
2675 <dd><p><code>textdomain</code> function, defined under the <code>Intl</code> namespace | |
2676 </p> | |
2677 </dd> | |
2678 <dt> bindtextdomain</dt> | |
2679 <dd><p><code>bindtextdomain</code> function, defined under the <code>Intl</code> namespace | |
2680 </p> | |
2681 </dd> | |
2682 <dt> setlocale</dt> | |
2683 <dd><p>Programmer must call <code>Intl.setlocale (LocaleCategory.ALL, "")</code> | |
2684 </p> | |
2685 </dd> | |
2686 <dt> Prerequisite</dt> | |
2687 <dd><p>— | |
2688 </p> | |
2689 </dd> | |
2690 <dt> Use or emulate GNU gettext</dt> | |
2691 <dd><p>Use | |
2692 </p> | |
2693 </dd> | |
2694 <dt> Extractor</dt> | |
2695 <dd><p><code>xgettext</code> | |
2696 </p> | |
2697 </dd> | |
2698 <dt> Formatting with positions</dt> | |
2699 <dd><p>Same as for the C language. | |
2700 </p> | |
2701 </dd> | |
2702 <dt> Portability</dt> | |
2703 <dd><p>autoconf (gettext.m4) and #if ENABLE_NLS | |
2704 </p> | |
2705 </dd> | |
2706 <dt> po-mode marking</dt> | |
2707 <dd><p>yes | |
2708 </p></dd> | |
2709 </dl> | |
2710 | |
2711 | |
2712 <a name="wxWidgets"></a> | |
2713 <a name="SEC324"></a> | |
2714 <h3 class="subsection"> <a href="gettext_toc.html#TOC317">15.5.19 wxWidgets library</a> </h3> | |
2715 | |
2716 <dl compact="compact"> | |
2717 <dt> RPMs</dt> | |
2718 <dd><p>wxGTK, gettext | |
2719 </p> | |
2720 </dd> | |
2721 <dt> Ubuntu packages</dt> | |
2722 <dd><p>libwxgtk3.0-dev | |
2723 </p> | |
2724 </dd> | |
2725 <dt> File extension</dt> | |
2726 <dd><p><code>cpp</code> | |
2727 </p> | |
2728 </dd> | |
2729 <dt> String syntax</dt> | |
2730 <dd><p><code>"abc"</code> | |
2731 </p> | |
2732 </dd> | |
2733 <dt> gettext shorthand</dt> | |
2734 <dd><p><code>_("abc")</code> | |
2735 </p> | |
2736 </dd> | |
2737 <dt> gettext/ngettext functions</dt> | |
2738 <dd><p><code>wxLocale::GetString</code>, <code>wxGetTranslation</code> | |
2739 </p> | |
2740 </dd> | |
2741 <dt> textdomain</dt> | |
2742 <dd><p><code>wxLocale::AddCatalog</code> | |
2743 </p> | |
2744 </dd> | |
2745 <dt> bindtextdomain</dt> | |
2746 <dd><p><code>wxLocale::AddCatalogLookupPathPrefix</code> | |
2747 </p> | |
2748 </dd> | |
2749 <dt> setlocale</dt> | |
2750 <dd><p><code>wxLocale::Init</code>, <code>wxSetLocale</code> | |
2751 </p> | |
2752 </dd> | |
2753 <dt> Prerequisite</dt> | |
2754 <dd><p><code>#include <wx/intl.h></code> | |
2755 </p> | |
2756 </dd> | |
2757 <dt> Use or emulate GNU gettext</dt> | |
2758 <dd><p>emulate, see <code>include/wx/intl.h</code> and <code>src/common/intl.cpp</code> | |
2759 </p> | |
2760 </dd> | |
2761 <dt> Extractor</dt> | |
2762 <dd><p><code>xgettext</code> | |
2763 </p> | |
2764 </dd> | |
2765 <dt> Formatting with positions</dt> | |
2766 <dd><p>wxString::Format supports positions if and only if the system has | |
2767 <code>wprintf()</code>, <code>vswprintf()</code> functions and they support positions | |
2768 according to POSIX. | |
2769 </p> | |
2770 </dd> | |
2771 <dt> Portability</dt> | |
2772 <dd><p>fully portable | |
2773 </p> | |
2774 </dd> | |
2775 <dt> po-mode marking</dt> | |
2776 <dd><p>yes | |
2777 </p></dd> | |
2778 </dl> | |
2779 | |
2780 | |
2781 <a name="Tcl"></a> | |
2782 <a name="SEC325"></a> | |
2783 <h3 class="subsection"> <a href="gettext_toc.html#TOC318">15.5.20 Tcl - Tk's scripting language</a> </h3> | |
2784 | |
2785 <dl compact="compact"> | |
2786 <dt> RPMs</dt> | |
2787 <dd><p>tcl | |
2788 </p> | |
2789 </dd> | |
2790 <dt> Ubuntu packages</dt> | |
2791 <dd><p>tcl | |
2792 </p> | |
2793 </dd> | |
2794 <dt> File extension</dt> | |
2795 <dd><p><code>tcl</code> | |
2796 </p> | |
2797 </dd> | |
2798 <dt> String syntax</dt> | |
2799 <dd><p><code>"abc"</code> | |
2800 </p> | |
2801 </dd> | |
2802 <dt> gettext shorthand</dt> | |
2803 <dd><p><code>[_ "abc"]</code> | |
2804 </p> | |
2805 </dd> | |
2806 <dt> gettext/ngettext functions</dt> | |
2807 <dd><p><code>::msgcat::mc</code> | |
2808 </p> | |
2809 </dd> | |
2810 <dt> textdomain</dt> | |
2811 <dd><p>— | |
2812 </p> | |
2813 </dd> | |
2814 <dt> bindtextdomain</dt> | |
2815 <dd><p>—, use <code>::msgcat::mcload</code> instead | |
2816 </p> | |
2817 </dd> | |
2818 <dt> setlocale</dt> | |
2819 <dd><p>automatic, uses LANG, but ignores LC_MESSAGES and LC_ALL | |
2820 </p> | |
2821 </dd> | |
2822 <dt> Prerequisite</dt> | |
2823 <dd><p><code>package require msgcat</code> | |
2824 <br><code>proc _ {s} {return [::msgcat::mc $s]}</code> | |
2825 </p> | |
2826 </dd> | |
2827 <dt> Use or emulate GNU gettext</dt> | |
2828 <dd><p>—, uses a Tcl specific message catalog format | |
2829 </p> | |
2830 </dd> | |
2831 <dt> Extractor</dt> | |
2832 <dd><p><code>xgettext -k_</code> | |
2833 </p> | |
2834 </dd> | |
2835 <dt> Formatting with positions</dt> | |
2836 <dd><p><code>format "%2\$d %1\$d"</code> | |
2837 </p> | |
2838 </dd> | |
2839 <dt> Portability</dt> | |
2840 <dd><p>fully portable | |
2841 </p> | |
2842 </dd> | |
2843 <dt> po-mode marking</dt> | |
2844 <dd><p>— | |
2845 </p></dd> | |
2846 </dl> | |
2847 | |
2848 <p>Two examples are available in the ‘<tt>examples</tt>’ directory: | |
2849 <code>hello-tcl</code>, <code>hello-tcl-tk</code>. | |
2850 </p> | |
2851 <p>Before marking strings as internationalizable, substitutions of variables | |
2852 into the string need to be converted to <code>format</code> applications. For | |
2853 example, <code>"file $filename not found"</code> becomes | |
2854 <code>[format "file %s not found" $filename]</code>. | |
2855 Only after this is done, can the strings be marked and extracted. | |
2856 After marking, this example becomes | |
2857 <code>[format [_ "file %s not found"] $filename]</code> or | |
2858 <code>[msgcat::mc "file %s not found" $filename]</code>. Note that the | |
2859 <code>msgcat::mc</code> function implicitly calls <code>format</code> when more than one | |
2860 argument is given. | |
2861 </p> | |
2862 | |
2863 <a name="Perl"></a> | |
2864 <a name="SEC326"></a> | |
2865 <h3 class="subsection"> <a href="gettext_toc.html#TOC319">15.5.21 Perl</a> </h3> | |
2866 | |
2867 <dl compact="compact"> | |
2868 <dt> RPMs</dt> | |
2869 <dd><p>perl | |
2870 </p> | |
2871 </dd> | |
2872 <dt> Ubuntu packages</dt> | |
2873 <dd><p>perl, libintl-perl | |
2874 </p> | |
2875 </dd> | |
2876 <dt> File extension</dt> | |
2877 <dd><p><code>pl</code>, <code>PL</code>, <code>pm</code>, <code>perl</code>, <code>cgi</code> | |
2878 </p> | |
2879 </dd> | |
2880 <dt> String syntax</dt> | |
2881 <dd><ul> | |
2882 <li> <code>"abc"</code> | |
2883 | |
2884 </li><li> <code>'abc'</code> | |
2885 | |
2886 </li><li> <code>qq (abc)</code> | |
2887 | |
2888 </li><li> <code>q (abc)</code> | |
2889 | |
2890 </li><li> <code>qr /abc/</code> | |
2891 | |
2892 </li><li> <code>qx (/bin/date)</code> | |
2893 | |
2894 </li><li> <code>/pattern match/</code> | |
2895 | |
2896 </li><li> <code>?pattern match?</code> | |
2897 | |
2898 </li><li> <code>s/substitution/operators/</code> | |
2899 | |
2900 </li><li> <code>$tied_hash{"message"}</code> | |
2901 | |
2902 </li><li> <code>$tied_hash_reference->{"message"}</code> | |
2903 | |
2904 </li><li> etc., issue the command ‘<samp>man perlsyn</samp>’ for details | |
2905 | |
2906 </li></ul> | |
2907 | |
2908 </dd> | |
2909 <dt> gettext shorthand</dt> | |
2910 <dd><p><code>__</code> (double underscore) | |
2911 </p> | |
2912 </dd> | |
2913 <dt> gettext/ngettext functions</dt> | |
2914 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code>, <code>ngettext</code>, | |
2915 <code>dngettext</code>, <code>dcngettext</code>, <code>pgettext</code>, <code>dpgettext</code>, | |
2916 <code>dcpgettext</code>, <code>npgettext</code>, <code>dnpgettext</code>, | |
2917 <code>dcnpgettext</code> | |
2918 </p> | |
2919 </dd> | |
2920 <dt> textdomain</dt> | |
2921 <dd><p><code>textdomain</code> function | |
2922 </p> | |
2923 </dd> | |
2924 <dt> bindtextdomain</dt> | |
2925 <dd><p><code>bindtextdomain</code> function | |
2926 </p> | |
2927 </dd> | |
2928 <dt> bind_textdomain_codeset </dt> | |
2929 <dd><p><code>bind_textdomain_codeset</code> function | |
2930 </p> | |
2931 </dd> | |
2932 <dt> setlocale</dt> | |
2933 <dd><p>Use <code>setlocale (LC_ALL, "");</code> | |
2934 </p> | |
2935 </dd> | |
2936 <dt> Prerequisite</dt> | |
2937 <dd><p><code>use POSIX;</code> | |
2938 <br><code>use Locale::TextDomain;</code> (included in the package libintl-perl | |
2939 which is available on the Comprehensive Perl Archive Network CPAN, | |
2940 https://www.cpan.org/). | |
2941 </p> | |
2942 </dd> | |
2943 <dt> Use or emulate GNU gettext</dt> | |
2944 <dd><p>platform dependent: gettext_pp emulates, gettext_xs uses GNU gettext | |
2945 </p> | |
2946 </dd> | |
2947 <dt> Extractor</dt> | |
2948 <dd><p><code>xgettext -k__ -k\$__ -k%__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 | |
2949 -kN__ -kN__n:1,2 -k__p:1c,2 -k__np:1c,2,3 -kN__p:1c,2 -kN__np:1c,2,3</code> | |
2950 </p> | |
2951 </dd> | |
2952 <dt> Formatting with positions</dt> | |
2953 <dd><p>Both kinds of format strings support formatting with positions. | |
2954 <br><code>printf "%2\$d %1\$d", ...</code> (requires Perl 5.8.0 or newer) | |
2955 <br><code>__expand("[new] replaces [old]", old => $oldvalue, new => $newvalue)</code> | |
2956 </p> | |
2957 </dd> | |
2958 <dt> Portability</dt> | |
2959 <dd><p>The <code>libintl-perl</code> package is platform independent but is not | |
2960 part of the Perl core. The programmer is responsible for | |
2961 providing a dummy implementation of the required functions if the | |
2962 package is not installed on the target system. | |
2963 </p> | |
2964 </dd> | |
2965 <dt> po-mode marking</dt> | |
2966 <dd><p>— | |
2967 </p> | |
2968 </dd> | |
2969 <dt> Documentation</dt> | |
2970 <dd><p>Included in <code>libintl-perl</code>, available on CPAN | |
2971 (https://www.cpan.org/). | |
2972 </p> | |
2973 </dd> | |
2974 </dl> | |
2975 | |
2976 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-perl</code>. | |
2977 </p> | |
2978 <a name="IDX1158"></a> | |
2979 | |
2980 <p>The <code>xgettext</code> parser backend for Perl differs significantly from | |
2981 the parser backends for other programming languages, just as Perl | |
2982 itself differs significantly from other programming languages. The | |
2983 Perl parser backend offers many more string marking facilities than | |
2984 the other backends but it also has some Perl specific limitations, the | |
2985 worst probably being its imperfectness. | |
2986 </p> | |
2987 | |
2988 | |
2989 <a name="General-Problems"></a> | |
2990 <a name="SEC327"></a> | |
2991 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC320">15.5.21.1 General Problems Parsing Perl Code</a> </h4> | |
2992 | |
2993 <p>It is often heard that only Perl can parse Perl. This is not true. | |
2994 Perl cannot be <em>parsed</em> at all, it can only be <em>executed</em>. | |
2995 Perl has various built-in ambiguities that can only be resolved at runtime. | |
2996 </p> | |
2997 <p>The following example may illustrate one common problem: | |
2998 </p> | |
2999 <table><tr><td> </td><td><pre class="example">print gettext "Hello World!"; | |
3000 </pre></td></tr></table> | |
3001 | |
3002 <p>Although this example looks like a bullet-proof case of a function | |
3003 invocation, it is not: | |
3004 </p> | |
3005 <table><tr><td> </td><td><pre class="example">open gettext, ">testfile" or die; | |
3006 print gettext "Hello world!" | |
3007 </pre></td></tr></table> | |
3008 | |
3009 <p>In this context, the string <code>gettext</code> looks more like a | |
3010 file handle. But not necessarily: | |
3011 </p> | |
3012 <table><tr><td> </td><td><pre class="example">use Locale::Messages qw (:libintl_h); | |
3013 open gettext ">testfile" or die; | |
3014 print gettext "Hello world!"; | |
3015 </pre></td></tr></table> | |
3016 | |
3017 <p>Now, the file is probably syntactically incorrect, provided that the module | |
3018 <code>Locale::Messages</code> found first in the Perl include path exports a | |
3019 function <code>gettext</code>. But what if the module | |
3020 <code>Locale::Messages</code> really looks like this? | |
3021 </p> | |
3022 <table><tr><td> </td><td><pre class="example">use vars qw (*gettext); | |
3023 | |
3024 1; | |
3025 </pre></td></tr></table> | |
3026 | |
3027 <p>In this case, the string <code>gettext</code> will be interpreted as a file | |
3028 handle again, and the above example will create a file ‘<tt>testfile</tt>’ | |
3029 and write the string “Hello world!” into it. Even advanced | |
3030 control flow analysis will not really help: | |
3031 </p> | |
3032 <table><tr><td> </td><td><pre class="example">if (0.5 < rand) { | |
3033 eval "use Sane"; | |
3034 } else { | |
3035 eval "use InSane"; | |
3036 } | |
3037 print gettext "Hello world!"; | |
3038 </pre></td></tr></table> | |
3039 | |
3040 <p>If the module <code>Sane</code> exports a function <code>gettext</code> that does | |
3041 what we expect, and the module <code>InSane</code> opens a file for writing | |
3042 and associates the <em>handle</em> <code>gettext</code> with this output | |
3043 stream, we are clueless again about what will happen at runtime. It is | |
3044 completely unpredictable. The truth is that Perl has so many ways to | |
3045 fill its symbol table at runtime that it is impossible to interpret a | |
3046 particular piece of code without executing it. | |
3047 </p> | |
3048 <p>Of course, <code>xgettext</code> will not execute your Perl sources while | |
3049 scanning for translatable strings, but rather use heuristics in order | |
3050 to guess what you meant. | |
3051 </p> | |
3052 <p>Another problem is the ambiguity of the slash and the question mark. | |
3053 Their interpretation depends on the context: | |
3054 </p> | |
3055 <table><tr><td> </td><td><pre class="example"># A pattern match. | |
3056 print "OK\n" if /foobar/; | |
3057 | |
3058 # A division. | |
3059 print 1 / 2; | |
3060 | |
3061 # Another pattern match. | |
3062 print "OK\n" if ?foobar?; | |
3063 | |
3064 # Conditional. | |
3065 print $x ? "foo" : "bar"; | |
3066 </pre></td></tr></table> | |
3067 | |
3068 <p>The slash may either act as the division operator or introduce a | |
3069 pattern match, whereas the question mark may act as the ternary | |
3070 conditional operator or as a pattern match, too. Other programming | |
3071 languages like <code>awk</code> present similar problems, but the consequences of a | |
3072 misinterpretation are particularly nasty with Perl sources. In <code>awk</code> | |
3073 for instance, a statement can never exceed one line and the parser | |
3074 can recover from a parsing error at the next newline and interpret | |
3075 the rest of the input stream correctly. Perl is different, as a | |
3076 pattern match is terminated by the next appearance of the delimiter | |
3077 (the slash or the question mark) in the input stream, regardless of | |
3078 the semantic context. If a slash is really a division sign but | |
3079 mis-interpreted as a pattern match, the rest of the input file is most | |
3080 probably parsed incorrectly. | |
3081 </p> | |
3082 <p>There are certain cases, where the ambiguity cannot be resolved at all: | |
3083 </p> | |
3084 <table><tr><td> </td><td><pre class="example">$x = wantarray ? 1 : 0; | |
3085 </pre></td></tr></table> | |
3086 | |
3087 <p>The Perl built-in function <code>wantarray</code> does not accept any arguments. | |
3088 The Perl parser therefore knows that the question mark does not start | |
3089 a regular expression but is the ternary conditional operator. | |
3090 </p> | |
3091 <table><tr><td> </td><td><pre class="example">sub wantarrays {} | |
3092 $x = wantarrays ? 1 : 0; | |
3093 </pre></td></tr></table> | |
3094 | |
3095 <p>Now the situation is different. The function <code>wantarrays</code> takes | |
3096 a variable number of arguments (like any non-prototyped Perl function). | |
3097 The question mark is now the delimiter of a pattern match, and hence | |
3098 the piece of code does not compile. | |
3099 </p> | |
3100 <table><tr><td> </td><td><pre class="example">sub wantarrays() {} | |
3101 $x = wantarrays ? 1 : 0; | |
3102 </pre></td></tr></table> | |
3103 | |
3104 <p>Now the function is prototyped, Perl knows that it does not accept any | |
3105 arguments, and the question mark is therefore interpreted as the | |
3106 ternaray operator again. But that unfortunately outsmarts <code>xgettext</code>. | |
3107 </p> | |
3108 <p>The Perl parser in <code>xgettext</code> cannot know whether a function has | |
3109 a prototype and what that prototype would look like. It therefore makes | |
3110 an educated guess. If a function is known to be a Perl built-in and | |
3111 this function does not accept any arguments, a following question mark | |
3112 or slash is treated as an operator, otherwise as the delimiter of a | |
3113 following regular expression. The Perl built-ins that do not accept | |
3114 arguments are <code>wantarray</code>, <code>fork</code>, <code>time</code>, <code>times</code>, | |
3115 <code>getlogin</code>, <code>getppid</code>, <code>getpwent</code>, <code>getgrent</code>, | |
3116 <code>gethostent</code>, <code>getnetent</code>, <code>getprotoent</code>, <code>getservent</code>, | |
3117 <code>setpwent</code>, <code>setgrent</code>, <code>endpwent</code>, <code>endgrent</code>, | |
3118 <code>endhostent</code>, <code>endnetent</code>, <code>endprotoent</code>, and | |
3119 <code>endservent</code>. | |
3120 </p> | |
3121 <p>If you find that <code>xgettext</code> fails to extract strings from | |
3122 portions of your sources, you should therefore look out for slashes | |
3123 and/or question marks preceding these sections. You may have come | |
3124 across a bug in <code>xgettext</code>'s Perl parser (and of course you | |
3125 should report that bug). In the meantime you should consider to | |
3126 reformulate your code in a manner less challenging to <code>xgettext</code>. | |
3127 </p> | |
3128 <p>In particular, if the parser is too dumb to see that a function | |
3129 does not accept arguments, use parentheses: | |
3130 </p> | |
3131 <table><tr><td> </td><td><pre class="example">$x = somefunc() ? 1 : 0; | |
3132 $y = (somefunc) ? 1 : 0; | |
3133 </pre></td></tr></table> | |
3134 | |
3135 <p>In fact the Perl parser itself has similar problems and warns you | |
3136 about such constructs. | |
3137 </p> | |
3138 | |
3139 <a name="Default-Keywords"></a> | |
3140 <a name="SEC328"></a> | |
3141 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC321">15.5.21.2 Which keywords will xgettext look for?</a> </h4> | |
3142 | |
3143 <p>Unless you instruct <code>xgettext</code> otherwise by invoking it with one | |
3144 of the options <code>--keyword</code> or <code>-k</code>, it will recognize the | |
3145 following keywords in your Perl sources: | |
3146 </p> | |
3147 <ul> | |
3148 <li> <code>gettext</code> | |
3149 | |
3150 </li><li> <code>dgettext:2</code> | |
3151 | |
3152 <p>The second argument will be extracted. | |
3153 </p> | |
3154 </li><li> <code>dcgettext:2</code> | |
3155 | |
3156 <p>The second argument will be extracted. | |
3157 </p> | |
3158 </li><li> <code>ngettext:1,2</code> | |
3159 | |
3160 <p>The first (singular) and the second (plural) argument will be | |
3161 extracted. | |
3162 </p> | |
3163 </li><li> <code>dngettext:2,3</code> | |
3164 | |
3165 <p>The second (singular) and the third (plural) argument will be | |
3166 extracted. | |
3167 </p> | |
3168 </li><li> <code>dcngettext:2,3</code> | |
3169 | |
3170 <p>The second (singular) and the third (plural) argument will be | |
3171 extracted. | |
3172 </p> | |
3173 </li><li> <code>pgettext:1c,2</code> | |
3174 | |
3175 <p>The first (message context) and the second argument will be extracted. | |
3176 </p> | |
3177 </li><li> <code>dpgettext:2c,3</code> | |
3178 | |
3179 <p>The second (message context) and the third argument will be extracted. | |
3180 </p> | |
3181 </li><li> <code>dcpgettext:2c,3</code> | |
3182 | |
3183 <p>The second (message context) and the third argument will be extracted. | |
3184 </p> | |
3185 </li><li> <code>npgettext:1c,2,3</code> | |
3186 | |
3187 <p>The first (message context), second (singular), and third (plural) | |
3188 argument will be extracted. | |
3189 </p> | |
3190 </li><li> <code>dnpgettext:2c,3,4</code> | |
3191 | |
3192 <p>The second (message context), third (singular), and fourth (plural) | |
3193 argument will be extracted. | |
3194 </p> | |
3195 </li><li> <code>dcnpgettext:2c,3,4</code> | |
3196 | |
3197 <p>The second (message context), third (singular), and fourth (plural) | |
3198 argument will be extracted. | |
3199 </p> | |
3200 </li><li> <code>gettext_noop</code> | |
3201 | |
3202 </li><li> <code>%gettext</code> | |
3203 | |
3204 <p>The keys of lookups into the hash <code>%gettext</code> will be extracted. | |
3205 </p> | |
3206 </li><li> <code>$gettext</code> | |
3207 | |
3208 <p>The keys of lookups into the hash reference <code>$gettext</code> will be extracted. | |
3209 </p> | |
3210 </li></ul> | |
3211 | |
3212 | |
3213 <a name="Special-Keywords"></a> | |
3214 <a name="SEC329"></a> | |
3215 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC322">15.5.21.3 How to Extract Hash Keys</a> </h4> | |
3216 | |
3217 <p>Translating messages at runtime is normally performed by looking up the | |
3218 original string in the translation database and returning the | |
3219 translated version. The “natural” Perl implementation is a hash | |
3220 lookup, and, of course, <code>xgettext</code> supports such practice. | |
3221 </p> | |
3222 <table><tr><td> </td><td><pre class="example">print __"Hello world!"; | |
3223 print $__{"Hello world!"}; | |
3224 print $__->{"Hello world!"}; | |
3225 print $$__{"Hello world!"}; | |
3226 </pre></td></tr></table> | |
3227 | |
3228 | |
3229 <p>The above four lines all do the same thing. The Perl module | |
3230 <code>Locale::TextDomain</code> exports by default a hash <code>%__</code> that | |
3231 is tied to the function <code>__()</code>. It also exports a reference | |
3232 <code>$__</code> to <code>%__</code>. | |
3233 </p> | |
3234 <p>If an argument to the <code>xgettext</code> option <code>--keyword</code>, | |
3235 resp. <code>-k</code> starts with a percent sign, the rest of the keyword is | |
3236 interpreted as the name of a hash. If it starts with a dollar | |
3237 sign, the rest of the keyword is interpreted as a reference to a | |
3238 hash. | |
3239 </p> | |
3240 <p>Note that you can omit the quotation marks (single or double) around | |
3241 the hash key (almost) whenever Perl itself allows it: | |
3242 </p> | |
3243 <table><tr><td> </td><td><pre class="example">print $gettext{Error}; | |
3244 </pre></td></tr></table> | |
3245 | |
3246 <p>The exact rule is: You can omit the surrounding quotes, when the hash | |
3247 key is a valid C (!) identifier, i.e. when it starts with an | |
3248 underscore or an ASCII letter and is followed by an arbitrary number | |
3249 of underscores, ASCII letters or digits. Other Unicode characters | |
3250 are <em>not</em> allowed, regardless of the <code>use utf8</code> pragma. | |
3251 </p> | |
3252 | |
3253 <a name="Quote_002dlike-Expressions"></a> | |
3254 <a name="SEC330"></a> | |
3255 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC323">15.5.21.4 What are Strings And Quote-like Expressions?</a> </h4> | |
3256 | |
3257 <p>Perl offers a plethora of different string constructs. Those that can | |
3258 be used either as arguments to functions or inside braces for hash | |
3259 lookups are generally supported by <code>xgettext</code>. | |
3260 </p> | |
3261 <ul> | |
3262 <li> <strong>double-quoted strings</strong> | |
3263 <br> | |
3264 <table><tr><td> </td><td><pre class="example">print gettext "Hello World!"; | |
3265 </pre></td></tr></table> | |
3266 | |
3267 </li><li> <strong>single-quoted strings</strong> | |
3268 <br> | |
3269 <table><tr><td> </td><td><pre class="example">print gettext 'Hello World!'; | |
3270 </pre></td></tr></table> | |
3271 | |
3272 </li><li> <strong>the operator qq</strong> | |
3273 <br> | |
3274 <table><tr><td> </td><td><pre class="example">print gettext qq |Hello World!|; | |
3275 print gettext qq <E-mail: <guido\@imperia.net>>; | |
3276 </pre></td></tr></table> | |
3277 | |
3278 <p>The operator <code>qq</code> is fully supported. You can use arbitrary | |
3279 delimiters, including the four bracketing delimiters (round, angle, | |
3280 square, curly) that nest. | |
3281 </p> | |
3282 </li><li> <strong>the operator q</strong> | |
3283 <br> | |
3284 <table><tr><td> </td><td><pre class="example">print gettext q |Hello World!|; | |
3285 print gettext q <E-mail: <guido@imperia.net>>; | |
3286 </pre></td></tr></table> | |
3287 | |
3288 <p>The operator <code>q</code> is fully supported. You can use arbitrary | |
3289 delimiters, including the four bracketing delimiters (round, angle, | |
3290 square, curly) that nest. | |
3291 </p> | |
3292 </li><li> <strong>the operator qx</strong> | |
3293 <br> | |
3294 <table><tr><td> </td><td><pre class="example">print gettext qx ;LANGUAGE=C /bin/date; | |
3295 print gettext qx [/usr/bin/ls | grep '^[A-Z]*']; | |
3296 </pre></td></tr></table> | |
3297 | |
3298 <p>The operator <code>qx</code> is fully supported. You can use arbitrary | |
3299 delimiters, including the four bracketing delimiters (round, angle, | |
3300 square, curly) that nest. | |
3301 </p> | |
3302 <p>The example is actually a useless use of <code>gettext</code>. It will | |
3303 invoke the <code>gettext</code> function on the output of the command | |
3304 specified with the <code>qx</code> operator. The feature was included | |
3305 in order to make the interface consistent (the parser will extract | |
3306 all strings and quote-like expressions). | |
3307 </p> | |
3308 </li><li> <strong>here documents</strong> | |
3309 <br> | |
3310 <table><tr><td> </td><td><pre class="example">print gettext <<'EOF'; | |
3311 program not found in $PATH | |
3312 EOF | |
3313 | |
3314 print ngettext <<EOF, <<"EOF"; | |
3315 one file deleted | |
3316 EOF | |
3317 several files deleted | |
3318 EOF | |
3319 </pre></td></tr></table> | |
3320 | |
3321 <p>Here-documents are recognized. If the delimiter is enclosed in single | |
3322 quotes, the string is not interpolated. If it is enclosed in double | |
3323 quotes or has no quotes at all, the string is interpolated. | |
3324 </p> | |
3325 <p>Delimiters that start with a digit are not supported! | |
3326 </p> | |
3327 </li></ul> | |
3328 | |
3329 | |
3330 <a name="Interpolation-I"></a> | |
3331 <a name="SEC331"></a> | |
3332 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC324">15.5.21.5 Invalid Uses Of String Interpolation</a> </h4> | |
3333 | |
3334 <p>Perl is capable of interpolating variables into strings. This offers | |
3335 some nice features in localized programs but can also lead to | |
3336 problems. | |
3337 </p> | |
3338 <p>A common error is a construct like the following: | |
3339 </p> | |
3340 <table><tr><td> </td><td><pre class="example">print gettext "This is the program $0!\n"; | |
3341 </pre></td></tr></table> | |
3342 | |
3343 <p>Perl will interpolate at runtime the value of the variable <code>$0</code> | |
3344 into the argument of the <code>gettext()</code> function. Hence, this | |
3345 argument is not a string constant but a variable argument (<code>$0</code> | |
3346 is a global variable that holds the name of the Perl script being | |
3347 executed). The interpolation is performed by Perl before the string | |
3348 argument is passed to <code>gettext()</code> and will therefore depend on | |
3349 the name of the script which can only be determined at runtime. | |
3350 Consequently, it is almost impossible that a translation can be looked | |
3351 up at runtime (except if, by accident, the interpolated string is found | |
3352 in the message catalog). | |
3353 </p> | |
3354 <p>The <code>xgettext</code> program will therefore terminate parsing with a fatal | |
3355 error if it encounters a variable inside of an extracted string. In | |
3356 general, this will happen for all kinds of string interpolations that | |
3357 cannot be safely performed at compile time. If you absolutely know | |
3358 what you are doing, you can always circumvent this behavior: | |
3359 </p> | |
3360 <table><tr><td> </td><td><pre class="example">my $know_what_i_am_doing = "This is program $0!\n"; | |
3361 print gettext $know_what_i_am_doing; | |
3362 </pre></td></tr></table> | |
3363 | |
3364 <p>Since the parser only recognizes strings and quote-like expressions, | |
3365 but not variables or other terms, the above construct will be | |
3366 accepted. You will have to find another way, however, to let your | |
3367 original string make it into your message catalog. | |
3368 </p> | |
3369 <p>If invoked with the option <code>--extract-all</code>, resp. <code>-a</code>, | |
3370 variable interpolation will be accepted. Rationale: You will | |
3371 generally use this option in order to prepare your sources for | |
3372 internationalization. | |
3373 </p> | |
3374 <p>Please see the manual page ‘<samp>man perlop</samp>’ for details of strings and | |
3375 quote-like expressions that are subject to interpolation and those | |
3376 that are not. Safe interpolations (that will not lead to a fatal | |
3377 error) are: | |
3378 </p> | |
3379 <ul> | |
3380 <li> the escape sequences <code>\t</code> (tab, HT, TAB), <code>\n</code> | |
3381 (newline, NL), <code>\r</code> (return, CR), <code>\f</code> (form feed, FF), | |
3382 <code>\b</code> (backspace, BS), <code>\a</code> (alarm, bell, BEL), and <code>\e</code> | |
3383 (escape, ESC). | |
3384 | |
3385 </li><li> octal chars, like <code>\033</code> | |
3386 <br> | |
3387 Note that octal escapes in the range of 400-777 are translated into a | |
3388 UTF-8 representation, regardless of the presence of the <code>use utf8</code> pragma. | |
3389 | |
3390 </li><li> hex chars, like <code>\x1b</code> | |
3391 | |
3392 </li><li> wide hex chars, like <code>\x{263a}</code> | |
3393 <br> | |
3394 Note that this escape is translated into a UTF-8 representation, | |
3395 regardless of the presence of the <code>use utf8</code> pragma. | |
3396 | |
3397 </li><li> control chars, like <code>\c[</code> (CTRL-[) | |
3398 | |
3399 </li><li> named Unicode chars, like <code>\N{LATIN CAPITAL LETTER C WITH CEDILLA}</code> | |
3400 <br> | |
3401 Note that this escape is translated into a UTF-8 representation, | |
3402 regardless of the presence of the <code>use utf8</code> pragma. | |
3403 </li></ul> | |
3404 | |
3405 <p>The following escapes are considered partially safe: | |
3406 </p> | |
3407 <ul> | |
3408 <li> <code>\l</code> lowercase next char | |
3409 | |
3410 </li><li> <code>\u</code> uppercase next char | |
3411 | |
3412 </li><li> <code>\L</code> lowercase till \E | |
3413 | |
3414 </li><li> <code>\U</code> uppercase till \E | |
3415 | |
3416 </li><li> <code>\E</code> end case modification | |
3417 | |
3418 </li><li> <code>\Q</code> quote non-word characters till \E | |
3419 | |
3420 </li></ul> | |
3421 | |
3422 <p>These escapes are only considered safe if the string consists of | |
3423 ASCII characters only. Translation of characters outside the range | |
3424 defined by ASCII is locale-dependent and can actually only be performed | |
3425 at runtime; <code>xgettext</code> doesn't do these locale-dependent translations | |
3426 at extraction time. | |
3427 </p> | |
3428 <p>Except for the modifier <code>\Q</code>, these translations, albeit valid, | |
3429 are generally useless and only obfuscate your sources. If a | |
3430 translation can be safely performed at compile time you can just as | |
3431 well write what you mean. | |
3432 </p> | |
3433 | |
3434 <a name="Interpolation-II"></a> | |
3435 <a name="SEC332"></a> | |
3436 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC325">15.5.21.6 Valid Uses Of String Interpolation</a> </h4> | |
3437 | |
3438 <p>Perl is often used to generate sources for other programming languages | |
3439 or arbitrary file formats. Web applications that output HTML code | |
3440 make a prominent example for such usage. | |
3441 </p> | |
3442 <p>You will often come across situations where you want to intersperse | |
3443 code written in the target (programming) language with translatable | |
3444 messages, like in the following HTML example: | |
3445 </p> | |
3446 <table><tr><td> </td><td><pre class="example">print gettext <<EOF; | |
3447 <h1>My Homepage</h1> | |
3448 <script language="JavaScript"><!-- | |
3449 for (i = 0; i < 100; ++i) { | |
3450 alert ("Thank you so much for visiting my homepage!"); | |
3451 } | |
3452 //--></script> | |
3453 EOF | |
3454 </pre></td></tr></table> | |
3455 | |
3456 <p>The parser will extract the entire here document, and it will appear | |
3457 entirely in the resulting PO file, including the JavaScript snippet | |
3458 embedded in the HTML code. If you exaggerate with constructs like | |
3459 the above, you will run the risk that the translators of your package | |
3460 will look out for a less challenging project. You should consider an | |
3461 alternative expression here: | |
3462 </p> | |
3463 <table><tr><td> </td><td><pre class="example">print <<EOF; | |
3464 <h1>$gettext{"My Homepage"}</h1> | |
3465 <script language="JavaScript"><!-- | |
3466 for (i = 0; i < 100; ++i) { | |
3467 alert ("$gettext{'Thank you so much for visiting my homepage!'}"); | |
3468 } | |
3469 //--></script> | |
3470 EOF | |
3471 </pre></td></tr></table> | |
3472 | |
3473 <p>Only the translatable portions of the code will be extracted here, and | |
3474 the resulting PO file will begrudgingly improve in terms of readability. | |
3475 </p> | |
3476 <p>You can interpolate hash lookups in all strings or quote-like | |
3477 expressions that are subject to interpolation (see the manual page | |
3478 ‘<samp>man perlop</samp>’ for details). Double interpolation is invalid, however: | |
3479 </p> | |
3480 <table><tr><td> </td><td><pre class="example"># TRANSLATORS: Replace "the earth" with the name of your planet. | |
3481 print gettext qq{Welcome to $gettext->{"the earth"}}; | |
3482 </pre></td></tr></table> | |
3483 | |
3484 <p>The <code>qq</code>-quoted string is recognized as an argument to <code>xgettext</code> in | |
3485 the first place, and checked for invalid variable interpolation. The | |
3486 dollar sign of hash-dereferencing will therefore terminate the parser | |
3487 with an “invalid interpolation” error. | |
3488 </p> | |
3489 <p>It is valid to interpolate hash lookups in regular expressions: | |
3490 </p> | |
3491 <table><tr><td> </td><td><pre class="example">if ($var =~ /$gettext{"the earth"}/) { | |
3492 print gettext "Match!\n"; | |
3493 } | |
3494 s/$gettext{"U. S. A."}/$gettext{"U. S. A."} $gettext{"(dial +0)"}/g; | |
3495 </pre></td></tr></table> | |
3496 | |
3497 | |
3498 <a name="Parentheses"></a> | |
3499 <a name="SEC333"></a> | |
3500 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC326">15.5.21.7 When To Use Parentheses</a> </h4> | |
3501 | |
3502 <p>In Perl, parentheses around function arguments are mostly optional. | |
3503 <code>xgettext</code> will always assume that all | |
3504 recognized keywords (except for hashes and hash references) are names | |
3505 of properly prototyped functions, and will (hopefully) only require | |
3506 parentheses where Perl itself requires them. All constructs in the | |
3507 following example are therefore ok to use: | |
3508 </p> | |
3509 <table><tr><td> </td><td><pre class="example">print gettext ("Hello World!\n"); | |
3510 print gettext "Hello World!\n"; | |
3511 print dgettext ($package => "Hello World!\n"); | |
3512 print dgettext $package, "Hello World!\n"; | |
3513 | |
3514 # The "fat comma" => turns the left-hand side argument into a | |
3515 # single-quoted string! | |
3516 print dgettext smellovision => "Hello World!\n"; | |
3517 | |
3518 # The following assignment only works with prototyped functions. | |
3519 # Otherwise, the functions will act as "greedy" list operators and | |
3520 # eat up all following arguments. | |
3521 my $anonymous_hash = { | |
3522 planet => gettext "earth", | |
3523 cakes => ngettext "one cake", "several cakes", $n, | |
3524 still => $works, | |
3525 }; | |
3526 # The same without fat comma: | |
3527 my $other_hash = { | |
3528 'planet', gettext "earth", | |
3529 'cakes', ngettext "one cake", "several cakes", $n, | |
3530 'still', $works, | |
3531 }; | |
3532 | |
3533 # Parentheses are only significant for the first argument. | |
3534 print dngettext 'package', ("one cake", "several cakes", $n), $discarded; | |
3535 </pre></td></tr></table> | |
3536 | |
3537 | |
3538 <a name="Long-Lines"></a> | |
3539 <a name="SEC334"></a> | |
3540 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC327">15.5.21.8 How To Grok with Long Lines</a> </h4> | |
3541 | |
3542 <p>The necessity of long messages can often lead to a cumbersome or | |
3543 unreadable coding style. Perl has several options that may prevent | |
3544 you from writing unreadable code, and | |
3545 <code>xgettext</code> does its best to do likewise. This is where the dot | |
3546 operator (the string concatenation operator) may come in handy: | |
3547 </p> | |
3548 <table><tr><td> </td><td><pre class="example">print gettext ("This is a very long" | |
3549 . " message that is still" | |
3550 . " readable, because" | |
3551 . " it is split into" | |
3552 . " multiple lines.\n"); | |
3553 </pre></td></tr></table> | |
3554 | |
3555 <p>Perl is smart enough to concatenate these constant string fragments | |
3556 into one long string at compile time, and so is | |
3557 <code>xgettext</code>. You will only find one long message in the resulting | |
3558 POT file. | |
3559 </p> | |
3560 <p>Note that the future Perl 6 will probably use the underscore | |
3561 (‘<samp>_</samp>’) as the string concatenation operator, and the dot | |
3562 (‘<samp>.</samp>’) for dereferencing. This new syntax is not yet supported by | |
3563 <code>xgettext</code>. | |
3564 </p> | |
3565 <p>If embedded newline characters are not an issue, or even desired, you | |
3566 may also insert newline characters inside quoted strings wherever you | |
3567 feel like it: | |
3568 </p> | |
3569 <table><tr><td> </td><td><pre class="example">print gettext ("<em>In HTML output | |
3570 embedded newlines are generally no | |
3571 problem, since adjacent whitespace | |
3572 is always rendered into a single | |
3573 space character.</em>"); | |
3574 </pre></td></tr></table> | |
3575 | |
3576 <p>You may also consider to use here documents: | |
3577 </p> | |
3578 <table><tr><td> </td><td><pre class="example">print gettext <<EOF; | |
3579 <em>In HTML output | |
3580 embedded newlines are generally no | |
3581 problem, since adjacent whitespace | |
3582 is always rendered into a single | |
3583 space character.</em> | |
3584 EOF | |
3585 </pre></td></tr></table> | |
3586 | |
3587 <p>Please do not forget that the line breaks are real, i.e. they | |
3588 translate into newline characters that will consequently show up in | |
3589 the resulting POT file. | |
3590 </p> | |
3591 | |
3592 <a name="Perl-Pitfalls"></a> | |
3593 <a name="SEC335"></a> | |
3594 <h4 class="subsubsection"> <a href="gettext_toc.html#TOC328">15.5.21.9 Bugs, Pitfalls, And Things That Do Not Work</a> </h4> | |
3595 | |
3596 <p>The foregoing sections should have proven that | |
3597 <code>xgettext</code> is quite smart in extracting translatable strings from | |
3598 Perl sources. Yet, some more or less exotic constructs that could be | |
3599 expected to work, actually do not work. | |
3600 </p> | |
3601 <p>One of the more relevant limitations can be found in the | |
3602 implementation of variable interpolation inside quoted strings. Only | |
3603 simple hash lookups can be used there: | |
3604 </p> | |
3605 <table><tr><td> </td><td><pre class="example">print <<EOF; | |
3606 $gettext{"The dot operator" | |
3607 . " does not work" | |
3608 . "here!"} | |
3609 Likewise, you cannot @{[ gettext ("interpolate function calls") ]} | |
3610 inside quoted strings or quote-like expressions. | |
3611 EOF | |
3612 </pre></td></tr></table> | |
3613 | |
3614 <p>This is valid Perl code and will actually trigger invocations of the | |
3615 <code>gettext</code> function at runtime. Yet, the Perl parser in | |
3616 <code>xgettext</code> will fail to recognize the strings. A less obvious | |
3617 example can be found in the interpolation of regular expressions: | |
3618 </p> | |
3619 <table><tr><td> </td><td><pre class="example">s/<!--START_OF_WEEK-->/gettext ("Sunday")/e; | |
3620 </pre></td></tr></table> | |
3621 | |
3622 <p>The modifier <code>e</code> will cause the substitution to be interpreted as | |
3623 an evaluable statement. Consequently, at runtime the function | |
3624 <code>gettext()</code> is called, but again, the parser fails to extract the | |
3625 string “Sunday”. Use a temporary variable as a simple workaround if | |
3626 you really happen to need this feature: | |
3627 </p> | |
3628 <table><tr><td> </td><td><pre class="example">my $sunday = gettext "Sunday"; | |
3629 s/<!--START_OF_WEEK-->/$sunday/; | |
3630 </pre></td></tr></table> | |
3631 | |
3632 <p>Hash slices would also be handy but are not recognized: | |
3633 </p> | |
3634 <table><tr><td> </td><td><pre class="example">my @weekdays = @gettext{'Sunday', 'Monday', 'Tuesday', 'Wednesday', | |
3635 'Thursday', 'Friday', 'Saturday'}; | |
3636 # Or even: | |
3637 @weekdays = @gettext{qw (Sunday Monday Tuesday Wednesday Thursday | |
3638 Friday Saturday) }; | |
3639 </pre></td></tr></table> | |
3640 | |
3641 <p>This is perfectly valid usage of the tied hash <code>%gettext</code> but the | |
3642 strings are not recognized and therefore will not be extracted. | |
3643 </p> | |
3644 <p>Another caveat of the current version is its rudimentary support for | |
3645 non-ASCII characters in identifiers. You may encounter serious | |
3646 problems if you use identifiers with characters outside the range of | |
3647 'A'-'Z', 'a'-'z', '0'-'9' and the underscore '_'. | |
3648 </p> | |
3649 <p>Maybe some of these missing features will be implemented in future | |
3650 versions, but since you can always make do without them at minimal effort, | |
3651 these todos have very low priority. | |
3652 </p> | |
3653 <p>A nasty problem are brace format strings that already contain braces | |
3654 as part of the normal text, for example the usage strings typically | |
3655 encountered in programs: | |
3656 </p> | |
3657 <table><tr><td> </td><td><pre class="example">die "usage: $0 {OPTIONS} FILENAME...\n"; | |
3658 </pre></td></tr></table> | |
3659 | |
3660 <p>If you want to internationalize this code with Perl brace format strings, | |
3661 you will run into a problem: | |
3662 </p> | |
3663 <table><tr><td> </td><td><pre class="example">die __x ("usage: {program} {OPTIONS} FILENAME...\n", program => $0); | |
3664 </pre></td></tr></table> | |
3665 | |
3666 <p>Whereas ‘<samp>{program}</samp>’ is a placeholder, ‘<samp>{OPTIONS}</samp>’ | |
3667 is not and should probably be translated. Yet, there is no way to teach | |
3668 the Perl parser in <code>xgettext</code> to recognize the first one, and leave | |
3669 the other one alone. | |
3670 </p> | |
3671 <p>There are two possible work-arounds for this problem. If you are | |
3672 sure that your program will run under Perl 5.8.0 or newer (these | |
3673 Perl versions handle positional parameters in <code>printf()</code>) or | |
3674 if you are sure that the translator will not have to reorder the arguments | |
3675 in her translation – for example if you have only one brace placeholder | |
3676 in your string, or if it describes a syntax, like in this one –, you can | |
3677 mark the string as <code>no-perl-brace-format</code> and use <code>printf()</code>: | |
3678 </p> | |
3679 <table><tr><td> </td><td><pre class="example"># xgettext: no-perl-brace-format | |
3680 die sprintf ("usage: %s {OPTIONS} FILENAME...\n", $0); | |
3681 </pre></td></tr></table> | |
3682 | |
3683 <p>If you want to use the more portable Perl brace format, you will have to do | |
3684 put placeholders in place of the literal braces: | |
3685 </p> | |
3686 <table><tr><td> </td><td><pre class="example">die __x ("usage: {program} {[}OPTIONS{]} FILENAME...\n", | |
3687 program => $0, '[' => '{', ']' => '}'); | |
3688 </pre></td></tr></table> | |
3689 | |
3690 <p>Perl brace format strings know no escaping mechanism. No matter how this | |
3691 escaping mechanism looked like, it would either give the programmer a | |
3692 hard time, make translating Perl brace format strings heavy-going, or | |
3693 result in a performance penalty at runtime, when the format directives | |
3694 get executed. Most of the time you will happily get along with | |
3695 <code>printf()</code> for this special case. | |
3696 </p> | |
3697 | |
3698 <a name="PHP"></a> | |
3699 <a name="SEC336"></a> | |
3700 <h3 class="subsection"> <a href="gettext_toc.html#TOC329">15.5.22 PHP Hypertext Preprocessor</a> </h3> | |
3701 | |
3702 <dl compact="compact"> | |
3703 <dt> RPMs</dt> | |
3704 <dd><p>mod_php4, mod_php4-core, phpdoc | |
3705 </p> | |
3706 </dd> | |
3707 <dt> Ubuntu packages</dt> | |
3708 <dd><p>php | |
3709 </p> | |
3710 </dd> | |
3711 <dt> File extension</dt> | |
3712 <dd><p><code>php</code>, <code>php3</code>, <code>php4</code> | |
3713 </p> | |
3714 </dd> | |
3715 <dt> String syntax</dt> | |
3716 <dd><p><code>"abc"</code>, <code>'abc'</code> | |
3717 </p> | |
3718 </dd> | |
3719 <dt> gettext shorthand</dt> | |
3720 <dd><p><code>_("abc")</code> | |
3721 </p> | |
3722 </dd> | |
3723 <dt> gettext/ngettext functions</dt> | |
3724 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code>; starting with PHP 4.2.0 | |
3725 also <code>ngettext</code>, <code>dngettext</code>, <code>dcngettext</code> | |
3726 </p> | |
3727 </dd> | |
3728 <dt> textdomain</dt> | |
3729 <dd><p><code>textdomain</code> function | |
3730 </p> | |
3731 </dd> | |
3732 <dt> bindtextdomain</dt> | |
3733 <dd><p><code>bindtextdomain</code> function | |
3734 </p> | |
3735 </dd> | |
3736 <dt> setlocale</dt> | |
3737 <dd><p>Programmer must call <code>setlocale (LC_ALL, "")</code> | |
3738 </p> | |
3739 </dd> | |
3740 <dt> Prerequisite</dt> | |
3741 <dd><p>— | |
3742 </p> | |
3743 </dd> | |
3744 <dt> Use or emulate GNU gettext</dt> | |
3745 <dd><p>use | |
3746 </p> | |
3747 </dd> | |
3748 <dt> Extractor</dt> | |
3749 <dd><p><code>xgettext</code> | |
3750 </p> | |
3751 </dd> | |
3752 <dt> Formatting with positions</dt> | |
3753 <dd><p><code>printf "%2\$d %1\$d"</code> | |
3754 </p> | |
3755 </dd> | |
3756 <dt> Portability</dt> | |
3757 <dd><p>On platforms without gettext, the functions are not available. | |
3758 </p> | |
3759 </dd> | |
3760 <dt> po-mode marking</dt> | |
3761 <dd><p>— | |
3762 </p></dd> | |
3763 </dl> | |
3764 | |
3765 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-php</code>. | |
3766 </p> | |
3767 | |
3768 <a name="Pike"></a> | |
3769 <a name="SEC337"></a> | |
3770 <h3 class="subsection"> <a href="gettext_toc.html#TOC330">15.5.23 Pike</a> </h3> | |
3771 | |
3772 <dl compact="compact"> | |
3773 <dt> RPMs</dt> | |
3774 <dd><p>roxen | |
3775 </p> | |
3776 </dd> | |
3777 <dt> Ubuntu packages</dt> | |
3778 <dd><p>pike8.0 or pike7.8 | |
3779 </p> | |
3780 </dd> | |
3781 <dt> File extension</dt> | |
3782 <dd><p><code>pike</code> | |
3783 </p> | |
3784 </dd> | |
3785 <dt> String syntax</dt> | |
3786 <dd><p><code>"abc"</code> | |
3787 </p> | |
3788 </dd> | |
3789 <dt> gettext shorthand</dt> | |
3790 <dd><p>— | |
3791 </p> | |
3792 </dd> | |
3793 <dt> gettext/ngettext functions</dt> | |
3794 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code> | |
3795 </p> | |
3796 </dd> | |
3797 <dt> textdomain</dt> | |
3798 <dd><p><code>textdomain</code> function | |
3799 </p> | |
3800 </dd> | |
3801 <dt> bindtextdomain</dt> | |
3802 <dd><p><code>bindtextdomain</code> function | |
3803 </p> | |
3804 </dd> | |
3805 <dt> setlocale</dt> | |
3806 <dd><p><code>setlocale</code> function | |
3807 </p> | |
3808 </dd> | |
3809 <dt> Prerequisite</dt> | |
3810 <dd><p><code>import Locale.Gettext;</code> | |
3811 </p> | |
3812 </dd> | |
3813 <dt> Use or emulate GNU gettext</dt> | |
3814 <dd><p>use | |
3815 </p> | |
3816 </dd> | |
3817 <dt> Extractor</dt> | |
3818 <dd><p>— | |
3819 </p> | |
3820 </dd> | |
3821 <dt> Formatting with positions</dt> | |
3822 <dd><p>— | |
3823 </p> | |
3824 </dd> | |
3825 <dt> Portability</dt> | |
3826 <dd><p>On platforms without gettext, the functions are not available. | |
3827 </p> | |
3828 </dd> | |
3829 <dt> po-mode marking</dt> | |
3830 <dd><p>— | |
3831 </p></dd> | |
3832 </dl> | |
3833 | |
3834 | |
3835 <a name="GCC_002dsource"></a> | |
3836 <a name="SEC338"></a> | |
3837 <h3 class="subsection"> <a href="gettext_toc.html#TOC331">15.5.24 GNU Compiler Collection sources</a> </h3> | |
3838 | |
3839 <dl compact="compact"> | |
3840 <dt> RPMs</dt> | |
3841 <dd><p>gcc | |
3842 </p> | |
3843 </dd> | |
3844 <dt> Ubuntu packages</dt> | |
3845 <dd><p>gcc | |
3846 </p> | |
3847 </dd> | |
3848 <dt> File extension</dt> | |
3849 <dd><p><code>c</code>, <code>h</code>. | |
3850 </p> | |
3851 </dd> | |
3852 <dt> String syntax</dt> | |
3853 <dd><p><code>"abc"</code> | |
3854 </p> | |
3855 </dd> | |
3856 <dt> gettext shorthand</dt> | |
3857 <dd><p><code>_("abc")</code> | |
3858 </p> | |
3859 </dd> | |
3860 <dt> gettext/ngettext functions</dt> | |
3861 <dd><p><code>gettext</code>, <code>dgettext</code>, <code>dcgettext</code>, <code>ngettext</code>, | |
3862 <code>dngettext</code>, <code>dcngettext</code> | |
3863 </p> | |
3864 </dd> | |
3865 <dt> textdomain</dt> | |
3866 <dd><p><code>textdomain</code> function | |
3867 </p> | |
3868 </dd> | |
3869 <dt> bindtextdomain</dt> | |
3870 <dd><p><code>bindtextdomain</code> function | |
3871 </p> | |
3872 </dd> | |
3873 <dt> setlocale</dt> | |
3874 <dd><p>Programmer must call <code>setlocale (LC_ALL, "")</code> | |
3875 </p> | |
3876 </dd> | |
3877 <dt> Prerequisite</dt> | |
3878 <dd><p><code>#include "intl.h"</code> | |
3879 </p> | |
3880 </dd> | |
3881 <dt> Use or emulate GNU gettext</dt> | |
3882 <dd><p>Use | |
3883 </p> | |
3884 </dd> | |
3885 <dt> Extractor</dt> | |
3886 <dd><p><code>xgettext -k_</code> | |
3887 </p> | |
3888 </dd> | |
3889 <dt> Formatting with positions</dt> | |
3890 <dd><p>— | |
3891 </p> | |
3892 </dd> | |
3893 <dt> Portability</dt> | |
3894 <dd><p>Uses autoconf macros | |
3895 </p> | |
3896 </dd> | |
3897 <dt> po-mode marking</dt> | |
3898 <dd><p>yes | |
3899 </p></dd> | |
3900 </dl> | |
3901 | |
3902 | |
3903 <a name="YCP"></a> | |
3904 <a name="SEC339"></a> | |
3905 <h3 class="subsection"> <a href="gettext_toc.html#TOC332">15.5.25 YCP - YaST2 scripting language</a> </h3> | |
3906 | |
3907 <dl compact="compact"> | |
3908 <dt> RPMs</dt> | |
3909 <dd><p>libycp, libycp-devel, yast2-core, yast2-core-devel | |
3910 </p> | |
3911 </dd> | |
3912 <dt> Ubuntu packages</dt> | |
3913 <dd><p>— | |
3914 </p> | |
3915 </dd> | |
3916 <dt> File extension</dt> | |
3917 <dd><p><code>ycp</code> | |
3918 </p> | |
3919 </dd> | |
3920 <dt> String syntax</dt> | |
3921 <dd><p><code>"abc"</code> | |
3922 </p> | |
3923 </dd> | |
3924 <dt> gettext shorthand</dt> | |
3925 <dd><p><code>_("abc")</code> | |
3926 </p> | |
3927 </dd> | |
3928 <dt> gettext/ngettext functions</dt> | |
3929 <dd><p><code>_()</code> with 1 or 3 arguments | |
3930 </p> | |
3931 </dd> | |
3932 <dt> textdomain</dt> | |
3933 <dd><p><code>textdomain</code> statement | |
3934 </p> | |
3935 </dd> | |
3936 <dt> bindtextdomain</dt> | |
3937 <dd><p>— | |
3938 </p> | |
3939 </dd> | |
3940 <dt> setlocale</dt> | |
3941 <dd><p>— | |
3942 </p> | |
3943 </dd> | |
3944 <dt> Prerequisite</dt> | |
3945 <dd><p>— | |
3946 </p> | |
3947 </dd> | |
3948 <dt> Use or emulate GNU gettext</dt> | |
3949 <dd><p>use | |
3950 </p> | |
3951 </dd> | |
3952 <dt> Extractor</dt> | |
3953 <dd><p><code>xgettext</code> | |
3954 </p> | |
3955 </dd> | |
3956 <dt> Formatting with positions</dt> | |
3957 <dd><p><code>sformat "%2 %1"</code> | |
3958 </p> | |
3959 </dd> | |
3960 <dt> Portability</dt> | |
3961 <dd><p>fully portable | |
3962 </p> | |
3963 </dd> | |
3964 <dt> po-mode marking</dt> | |
3965 <dd><p>— | |
3966 </p></dd> | |
3967 </dl> | |
3968 | |
3969 <p>An example is available in the ‘<tt>examples</tt>’ directory: <code>hello-ycp</code>. | |
3970 </p> | |
3971 | |
3972 | |
3973 <table cellpadding="1" cellspacing="1" border="0"> | |
3974 <tr><td valign="middle" align="left">[<a href="#SEC263" title="Beginning of this chapter or previous chapter"> << </a>]</td> | |
3975 <td valign="middle" align="left">[<a href="gettext_16.html#SEC340" title="Next chapter"> >> </a>]</td> | |
3976 <td valign="middle" align="left"> </td> | |
3977 <td valign="middle" align="left"> </td> | |
3978 <td valign="middle" align="left"> </td> | |
3979 <td valign="middle" align="left"> </td> | |
3980 <td valign="middle" align="left"> </td> | |
3981 <td valign="middle" align="left">[<a href="gettext_toc.html#SEC_Top" title="Cover (top) of document">Top</a>]</td> | |
3982 <td valign="middle" align="left">[<a href="gettext_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> | |
3983 <td valign="middle" align="left">[<a href="gettext_21.html#SEC389" title="Index">Index</a>]</td> | |
3984 <td valign="middle" align="left">[<a href="gettext_abt.html#SEC_About" title="About (help)"> ? </a>]</td> | |
3985 </tr></table> | |
3986 <p> | |
3987 <font size="-1"> | |
3988 This document was generated by <em>Bruno Haible</em> on <em>February, 21 2024</em> using <a href="https://www.nongnu.org/texi2html/"><em>texi2html 1.78a</em></a>. | |
3989 </font> | |
3990 <br> | |
3991 | |
3992 </p> | |
3993 </body> | |
3994 </html> |