1<HTML>
2<HEAD>
3<!-- This HTML file has been created by texi2html 1.52b
4     from gettext.texi on 27 November 2006 -->
5
6<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
7<TITLE>GNU gettext utilities - 4  Preparing Program Sources</TITLE>
8</HEAD>
9<BODY>
10Go to the <A HREF="gettext_1.html">first</A>, <A HREF="gettext_3.html">previous</A>, <A HREF="gettext_5.html">next</A>, <A HREF="gettext_25.html">last</A> section, <A HREF="gettext_toc.html">table of contents</A>.
11<P><HR><P>
12
13
14<H1><A NAME="SEC11" HREF="gettext_toc.html#TOC11">4  Preparing Program Sources</A></H1>
15<P>
16<A NAME="IDX102"></A>
17
18</P>
19
20<P>
21For the programmer, changes to the C source code fall into three
22categories.  First, you have to make the localization functions
23known to all modules needing message translation.  Second, you should
24properly trigger the operation of GNU <CODE>gettext</CODE> when the program
25initializes, usually from the <CODE>main</CODE> function.  Last, you should
26identify, adjust and mark all constant strings in your program
27needing translation.
28
29</P>
30
31
32
33<H2><A NAME="SEC12" HREF="gettext_toc.html#TOC12">4.1  Importing the <CODE>gettext</CODE> declaration</A></H2>
34
35<P>
36Presuming that your set of programs, or package, has been adjusted
37so all needed GNU <CODE>gettext</CODE> files are available, and your
38<TT>&lsquo;Makefile&rsquo;</TT> files are adjusted (see section <A HREF="gettext_13.html#SEC196">13  The Maintainer's View</A>), each C module
39having translated C strings should contain the line:
40
41</P>
42<P>
43<A NAME="IDX103"></A>
44
45<PRE>
46#include &#60;libintl.h&#62;
47</PRE>
48
49<P>
50Similarly, each C module containing <CODE>printf()</CODE>/<CODE>fprintf()</CODE>/...
51calls with a format string that could be a translated C string (even if
52the C string comes from a different C module) should contain the line:
53
54</P>
55
56<PRE>
57#include &#60;libintl.h&#62;
58</PRE>
59
60
61
62<H2><A NAME="SEC13" HREF="gettext_toc.html#TOC13">4.2  Triggering <CODE>gettext</CODE> Operations</A></H2>
63
64<P>
65<A NAME="IDX104"></A>
66The initialization of locale data should be done with more or less
67the same code in every program, as demonstrated below:
68
69</P>
70
71<PRE>
72int
73main (int argc, char *argv[])
74{
75  ...
76  setlocale (LC_ALL, "");
77  bindtextdomain (PACKAGE, LOCALEDIR);
78  textdomain (PACKAGE);
79  ...
80}
81</PRE>
82
83<P>
84<VAR>PACKAGE</VAR> and <VAR>LOCALEDIR</VAR> should be provided either by
85<TT>&lsquo;config.h&rsquo;</TT> or by the Makefile.  For now consult the <CODE>gettext</CODE>
86or <CODE>hello</CODE> sources for more information.
87
88</P>
89<P>
90<A NAME="IDX105"></A>
91<A NAME="IDX106"></A>
92The use of <CODE>LC_ALL</CODE> might not be appropriate for you.
93<CODE>LC_ALL</CODE> includes all locale categories and especially
94<CODE>LC_CTYPE</CODE>.  This later category is responsible for determining
95character classes with the <CODE>isalnum</CODE> etc. functions from
96<TT>&lsquo;ctype.h&rsquo;</TT> which could especially for programs, which process some
97kind of input language, be wrong.  For example this would mean that a
98source code using the &ccedil; (c-cedilla character) is runnable in
99France but not in the U.S.
100
101</P>
102<P>
103Some systems also have problems with parsing numbers using the
104<CODE>scanf</CODE> functions if an other but the <CODE>LC_ALL</CODE> locale is used.
105The standards say that additional formats but the one known in the
106<CODE>"C"</CODE> locale might be recognized.  But some systems seem to reject
107numbers in the <CODE>"C"</CODE> locale format.  In some situation, it might
108also be a problem with the notation itself which makes it impossible to
109recognize whether the number is in the <CODE>"C"</CODE> locale or the local
110format.  This can happen if thousands separator characters are used.
111Some locales define this character according to the national
112conventions to <CODE>'.'</CODE> which is the same character used in the
113<CODE>"C"</CODE> locale to denote the decimal point.
114
115</P>
116<P>
117So it is sometimes necessary to replace the <CODE>LC_ALL</CODE> line in the
118code above by a sequence of <CODE>setlocale</CODE> lines
119
120</P>
121
122<PRE>
123{
124  ...
125  setlocale (LC_CTYPE, "");
126  setlocale (LC_MESSAGES, "");
127  ...
128}
129</PRE>
130
131<P>
132<A NAME="IDX107"></A>
133<A NAME="IDX108"></A>
134<A NAME="IDX109"></A>
135<A NAME="IDX110"></A>
136<A NAME="IDX111"></A>
137<A NAME="IDX112"></A>
138<A NAME="IDX113"></A>
139On all POSIX conformant systems the locale categories <CODE>LC_CTYPE</CODE>,
140<CODE>LC_MESSAGES</CODE>, <CODE>LC_COLLATE</CODE>, <CODE>LC_MONETARY</CODE>,
141<CODE>LC_NUMERIC</CODE>, and <CODE>LC_TIME</CODE> are available.  On some systems
142which are only ISO C compliant, <CODE>LC_MESSAGES</CODE> is missing, but
143a substitute for it is defined in GNU gettext's <CODE>&#60;libintl.h&#62;</CODE>.
144
145</P>
146<P>
147Note that changing the <CODE>LC_CTYPE</CODE> also affects the functions
148declared in the <CODE>&#60;ctype.h&#62;</CODE> standard header.  If this is not
149desirable in your application (for example in a compiler's parser),
150you can use a set of substitute functions which hardwire the C locale,
151such as found in the <CODE>&#60;c-ctype.h&#62;</CODE> and <CODE>&#60;c-ctype.c&#62;</CODE> files
152in the gettext source distribution.
153
154</P>
155<P>
156It is also possible to switch the locale forth and back between the
157environment dependent locale and the C locale, but this approach is
158normally avoided because a <CODE>setlocale</CODE> call is expensive,
159because it is tedious to determine the places where a locale switch
160is needed in a large program's source, and because switching a locale
161is not multithread-safe.
162
163</P>
164
165
166<H2><A NAME="SEC14" HREF="gettext_toc.html#TOC14">4.3  Preparing Translatable Strings</A></H2>
167
168<P>
169<A NAME="IDX114"></A>
170Before strings can be marked for translations, they sometimes need to
171be adjusted.  Usually preparing a string for translation is done right
172before marking it, during the marking phase which is described in the
173next sections.  What you have to keep in mind while doing that is the
174following.
175
176</P>
177
178<UL>
179<LI>
180
181Decent English style.
182
183<LI>
184
185Entire sentences.
186
187<LI>
188
189Split at paragraphs.
190
191<LI>
192
193Use format strings instead of string concatenation.
194
195<LI>
196
197Avoid unusual markup and unusual control characters.
198</UL>
199
200<P>
201Let's look at some examples of these guidelines.
202
203</P>
204<P>
205<A NAME="IDX115"></A>
206Translatable strings should be in good English style.  If slang language
207with abbreviations and shortcuts is used, often translators will not
208understand the message and will produce very inappropriate translations.
209
210</P>
211
212<PRE>
213"%s: is parameter\n"
214</PRE>
215
216<P>
217This is nearly untranslatable: Is the displayed item <EM>a</EM> parameter or
218<EM>the</EM> parameter?
219
220</P>
221
222<PRE>
223"No match"
224</PRE>
225
226<P>
227The ambiguity in this message makes it unintelligible: Is the program
228attempting to set something on fire? Does it mean "The given object does
229not match the template"? Does it mean "The template does not fit for any
230of the objects"?
231
232</P>
233<P>
234<A NAME="IDX116"></A>
235In both cases, adding more words to the message will help both the
236translator and the English speaking user.
237
238</P>
239<P>
240<A NAME="IDX117"></A>
241Translatable strings should be entire sentences.  It is often not possible
242to translate single verbs or adjectives in a substitutable way.
243
244</P>
245
246<PRE>
247printf ("File %s is %s protected", filename, rw ? "write" : "read");
248</PRE>
249
250<P>
251Most translators will not look at the source and will thus only see the
252string <CODE>"File %s is %s protected"</CODE>, which is unintelligible.  Change
253this to
254
255</P>
256
257<PRE>
258printf (rw ? "File %s is write protected" : "File %s is read protected",
259        filename);
260</PRE>
261
262<P>
263This way the translator will not only understand the message, she will
264also be able to find the appropriate grammatical construction.  A French
265translator for example translates "write protected" like "protected
266against writing".
267
268</P>
269<P>
270Entire sentences are also important because in many languages, the
271declination of some word in a sentence depends on the gender or the
272number (singular/plural) of another part of the sentence.  There are
273usually more interdependencies between words than in English.  The
274consequence is that asking a translator to translate two half-sentences
275and then combining these two half-sentences through dumb string concatenation
276will not work, for many languages, even though it would work for English.
277That's why translators need to handle entire sentences.
278
279</P>
280<P>
281Often sentences don't fit into a single line.  If a sentence is output
282using two subsequent <CODE>printf</CODE> statements, like this
283
284</P>
285
286<PRE>
287printf ("Locale charset \"%s\" is different from\n", lcharset);
288printf ("input file charset \"%s\".\n", fcharset);
289</PRE>
290
291<P>
292the translator would have to translate two half sentences, but nothing
293in the POT file would tell her that the two half sentences belong together.
294It is necessary to merge the two <CODE>printf</CODE> statements so that the
295translator can handle the entire sentence at once and decide at which
296place to insert a line break in the translation (if at all):
297
298</P>
299
300<PRE>
301printf ("Locale charset \"%s\" is different from\n\
302input file charset \"%s\".\n", lcharset, fcharset);
303</PRE>
304
305<P>
306You may now ask: how about two or more adjacent sentences? Like in this case:
307
308</P>
309
310<PRE>
311puts ("Apollo 13 scenario: Stack overflow handling failed.");
312puts ("On the next stack overflow we will crash!!!");
313</PRE>
314
315<P>
316Should these two statements merged into a single one? I would recommend to
317merge them if the two sentences are related to each other, because then it
318makes it easier for the translator to understand and translate both.  On
319the other hand, if one of the two messages is a stereotypic one, occurring
320in other places as well, you will do a favour to the translator by not
321merging the two.  (Identical messages occurring in several places are
322combined by xgettext, so the translator has to handle them once only.)
323
324</P>
325<P>
326<A NAME="IDX118"></A>
327Translatable strings should be limited to one paragraph; don't let a
328single message be longer than ten lines.  The reason is that when the
329translatable string changes, the translator is faced with the task of
330updating the entire translated string.  Maybe only a single word will
331have changed in the English string, but the translator doesn't see that
332(with the current translation tools), therefore she has to proofread
333the entire message.
334
335</P>
336<P>
337<A NAME="IDX119"></A>
338Many GNU programs have a <SAMP>&lsquo;--help&rsquo;</SAMP> output that extends over several
339screen pages.  It is a courtesy towards the translators to split such a
340message into several ones of five to ten lines each.  While doing that,
341you can also attempt to split the documented options into groups,
342such as the input options, the output options, and the informative
343output options.  This will help every user to find the option he is
344looking for.
345
346</P>
347<P>
348<A NAME="IDX120"></A>
349<A NAME="IDX121"></A>
350Hardcoded string concatenation is sometimes used to construct English
351strings:
352
353</P>
354
355<PRE>
356strcpy (s, "Replace ");
357strcat (s, object1);
358strcat (s, " with ");
359strcat (s, object2);
360strcat (s, "?");
361</PRE>
362
363<P>
364In order to present to the translator only entire sentences, and also
365because in some languages the translator might want to swap the order
366of <CODE>object1</CODE> and <CODE>object2</CODE>, it is necessary to change this
367to use a format string:
368
369</P>
370
371<PRE>
372sprintf (s, "Replace %s with %s?", object1, object2);
373</PRE>
374
375<P>
376<A NAME="IDX122"></A>
377A similar case is compile time concatenation of strings.  The ISO C 99
378include file <CODE>&#60;inttypes.h&#62;</CODE> contains a macro <CODE>PRId64</CODE> that
379can be used as a formatting directive for outputting an <SAMP>&lsquo;int64_t&rsquo;</SAMP>
380integer through <CODE>printf</CODE>.  It expands to a constant string, usually
381"d" or "ld" or "lld" or something like this, depending on the platform.
382Assume you have code like
383
384</P>
385
386<PRE>
387printf ("The amount is %0" PRId64 "\n", number);
388</PRE>
389
390<P>
391The <CODE>gettext</CODE> tools and library have special support for these
392<CODE>&#60;inttypes.h&#62;</CODE> macros.  You can therefore simply write
393
394</P>
395
396<PRE>
397printf (gettext ("The amount is %0" PRId64 "\n"), number);
398</PRE>
399
400<P>
401The PO file will contain the string "The amount is %0&#60;PRId64&#62;\n".
402The translators will provide a translation containing "%0&#60;PRId64&#62;"
403as well, and at runtime the <CODE>gettext</CODE> function's result will
404contain the appropriate constant string, "d" or "ld" or "lld".
405
406</P>
407<P>
408This works only for the predefined <CODE>&#60;inttypes.h&#62;</CODE> macros.  If
409you have defined your own similar macros, let's say <SAMP>&lsquo;MYPRId64&rsquo;</SAMP>,
410that are not known to <CODE>xgettext</CODE>, the solution for this problem
411is to change the code like this:
412
413</P>
414
415<PRE>
416char buf1[100];
417sprintf (buf1, "%0" MYPRId64, number);
418printf (gettext ("The amount is %s\n"), buf1);
419</PRE>
420
421<P>
422This means, you put the platform dependent code in one statement, and the
423internationalization code in a different statement.  Note that a buffer length
424of 100 is safe, because all available hardware integer types are limited to
425128 bits, and to print a 128 bit integer one needs at most 54 characters,
426regardless whether in decimal, octal or hexadecimal.
427
428</P>
429<P>
430<A NAME="IDX123"></A>
431<A NAME="IDX124"></A>
432All this applies to other programming languages as well.  For example, in
433Java and C#, string concatenation is very frequently used, because it is a
434compiler built-in operator.  Like in C, in Java, you would change
435
436</P>
437
438<PRE>
439System.out.println("Replace "+object1+" with "+object2+"?");
440</PRE>
441
442<P>
443into a statement involving a format string:
444
445</P>
446
447<PRE>
448System.out.println(
449    MessageFormat.format("Replace {0} with {1}?",
450                         new Object[] { object1, object2 }));
451</PRE>
452
453<P>
454Similarly, in C#, you would change
455
456</P>
457
458<PRE>
459Console.WriteLine("Replace "+object1+" with "+object2+"?");
460</PRE>
461
462<P>
463into a statement involving a format string:
464
465</P>
466
467<PRE>
468Console.WriteLine(
469    String.Format("Replace {0} with {1}?", object1, object2));
470</PRE>
471
472<P>
473<A NAME="IDX125"></A>
474<A NAME="IDX126"></A>
475Unusual markup or control characters should not be used in translatable
476strings.  Translators will likely not understand the particular meaning
477of the markup or control characters.
478
479</P>
480<P>
481For example, if you have a convention that <SAMP>&lsquo;|&rsquo;</SAMP> delimits the
482left-hand and right-hand part of some GUI elements, translators will
483often not understand it without specific comments.  It might be
484better to have the translator translate the left-hand and right-hand
485part separately.
486
487</P>
488<P>
489Another example is the <SAMP>&lsquo;argp&rsquo;</SAMP> convention to use a single <SAMP>&lsquo;\v&rsquo;</SAMP>
490(vertical tab) control character to delimit two sections inside a
491string.  This is flawed.  Some translators may convert it to a simple
492newline, some to blank lines.  With some PO file editors it may not be
493easy to even enter a vertical tab control character.  So, you cannot
494be sure that the translation will contain a <SAMP>&lsquo;\v&rsquo;</SAMP> character, at the
495corresponding position.  The solution is, again, to let the translator
496translate two separate strings and combine at run-time the two translated
497strings with the <SAMP>&lsquo;\v&rsquo;</SAMP> required by the convention.
498
499</P>
500<P>
501HTML markup, however, is common enough that it's probably ok to use in
502translatable strings.  But please bear in mind that the GNU gettext tools
503don't verify that the translations are well-formed HTML.
504
505</P>
506
507
508<H2><A NAME="SEC15" HREF="gettext_toc.html#TOC15">4.4  How Marks Appear in Sources</A></H2>
509<P>
510<A NAME="IDX127"></A>
511
512</P>
513<P>
514All strings requiring translation should be marked in the C sources.  Marking
515is done in such a way that each translatable string appears to be
516the sole argument of some function or preprocessor macro.  There are
517only a few such possible functions or macros meant for translation,
518and their names are said to be marking keywords.  The marking is
519attached to strings themselves, rather than to what we do with them.
520This approach has more uses.  A blatant example is an error message
521produced by formatting.  The format string needs translation, as
522well as some strings inserted through some <SAMP>&lsquo;%s&rsquo;</SAMP> specification
523in the format, while the result from <CODE>sprintf</CODE> may have so many
524different instances that it is impractical to list them all in some
525<SAMP>&lsquo;error_string_out()&rsquo;</SAMP> routine, say.
526
527</P>
528<P>
529This marking operation has two goals.  The first goal of marking
530is for triggering the retrieval of the translation, at run time.
531The keyword is possibly resolved into a routine able to dynamically
532return the proper translation, as far as possible or wanted, for the
533argument string.  Most localizable strings are found in executable
534positions, that is, attached to variables or given as parameters to
535functions.  But this is not universal usage, and some translatable
536strings appear in structured initializations.  See section <A HREF="gettext_4.html#SEC18">4.7  Special Cases of Translatable Strings</A>.
537
538</P>
539<P>
540The second goal of the marking operation is to help <CODE>xgettext</CODE>
541at properly extracting all translatable strings when it scans a set
542of program sources and produces PO file templates.
543
544</P>
545<P>
546The canonical keyword for marking translatable strings is
547<SAMP>&lsquo;gettext&rsquo;</SAMP>, it gave its name to the whole GNU <CODE>gettext</CODE>
548package.  For packages making only light use of the <SAMP>&lsquo;gettext&rsquo;</SAMP>
549keyword, macro or function, it is easily used <EM>as is</EM>.  However,
550for packages using the <CODE>gettext</CODE> interface more heavily, it
551is usually more convenient to give the main keyword a shorter, less
552obtrusive name.  Indeed, the keyword might appear on a lot of strings
553all over the package, and programmers usually do not want nor need
554their program sources to remind them forcefully, all the time, that they
555are internationalized.  Further, a long keyword has the disadvantage
556of using more horizontal space, forcing more indentation work on
557sources for those trying to keep them within 79 or 80 columns.
558
559</P>
560<P>
561<A NAME="IDX128"></A>
562Many packages use <SAMP>&lsquo;_&rsquo;</SAMP> (a simple underline) as a keyword,
563and write <SAMP>&lsquo;_("Translatable string")&rsquo;</SAMP> instead of <SAMP>&lsquo;gettext
564("Translatable string")&rsquo;</SAMP>.  Further, the coding rule, from GNU standards,
565wanting that there is a space between the keyword and the opening
566parenthesis is relaxed, in practice, for this particular usage.
567So, the textual overhead per translatable string is reduced to
568only three characters: the underline and the two parentheses.
569However, even if GNU <CODE>gettext</CODE> uses this convention internally,
570it does not offer it officially.  The real, genuine keyword is truly
571<SAMP>&lsquo;gettext&rsquo;</SAMP> indeed.  It is fairly easy for those wanting to use
572<SAMP>&lsquo;_&rsquo;</SAMP> instead of <SAMP>&lsquo;gettext&rsquo;</SAMP> to declare:
573
574</P>
575
576<PRE>
577#include &#60;libintl.h&#62;
578#define _(String) gettext (String)
579</PRE>
580
581<P>
582instead of merely using <SAMP>&lsquo;#include &#60;libintl.h&#62;&rsquo;</SAMP>.
583
584</P>
585<P>
586The marking keywords <SAMP>&lsquo;gettext&rsquo;</SAMP> and <SAMP>&lsquo;_&rsquo;</SAMP> take the translatable
587string as sole argument.  It is also possible to define marking functions
588that take it at another argument position.  It is even possible to make
589the marked argument position depend on the total number of arguments of
590the function call; this is useful in C++.  All this is achieved using
591<CODE>xgettext</CODE>'s <SAMP>&lsquo;--keyword&rsquo;</SAMP> option.
592
593</P>
594<P>
595Note also that long strings can be split across lines, into multiple
596adjacent string tokens.  Automatic string concatenation is performed
597at compile time according to ISO C and ISO C++; <CODE>xgettext</CODE> also
598supports this syntax.
599
600</P>
601<P>
602Later on, the maintenance is relatively easy.  If, as a programmer,
603you add or modify a string, you will have to ask yourself if the
604new or altered string requires translation, and include it within
605<SAMP>&lsquo;_()&rsquo;</SAMP> if you think it should be translated.  For example, <SAMP>&lsquo;"%s"&rsquo;</SAMP>
606is an example of string <EM>not</EM> requiring translation.  But
607<SAMP>&lsquo;"%s: %d"&rsquo;</SAMP> <EM>does</EM> require translation, because in French, unlike
608in English, it's customary to put a space before a colon.
609
610</P>
611
612
613<H2><A NAME="SEC16" HREF="gettext_toc.html#TOC16">4.5  Marking Translatable Strings</A></H2>
614<P>
615<A NAME="IDX129"></A>
616
617</P>
618<P>
619In PO mode, one set of features is meant more for the programmer than
620for the translator, and allows him to interactively mark which strings,
621in a set of program sources, are translatable, and which are not.
622Even if it is a fairly easy job for a programmer to find and mark
623such strings by other means, using any editor of his choice, PO mode
624makes this work more comfortable.  Further, this gives translators
625who feel a little like programmers, or programmers who feel a little
626like translators, a tool letting them work at marking translatable
627strings in the program sources, while simultaneously producing a set of
628translation in some language, for the package being internationalized.
629
630</P>
631<P>
632<A NAME="IDX130"></A>
633The set of program sources, targeted by the PO mode commands describe
634here, should have an Emacs tags table constructed for your project,
635prior to using these PO file commands.  This is easy to do.  In any
636shell window, change the directory to the root of your project, then
637execute a command resembling:
638
639</P>
640
641<PRE>
642etags src/*.[hc] lib/*.[hc]
643</PRE>
644
645<P>
646presuming here you want to process all <TT>&lsquo;.h&rsquo;</TT> and <TT>&lsquo;.c&rsquo;</TT> files
647from the <TT>&lsquo;src/&rsquo;</TT> and <TT>&lsquo;lib/&rsquo;</TT> directories.  This command will
648explore all said files and create a <TT>&lsquo;TAGS&rsquo;</TT> file in your root
649directory, somewhat summarizing the contents using a special file
650format Emacs can understand.
651
652</P>
653<P>
654<A NAME="IDX131"></A>
655For packages following the GNU coding standards, there is
656a make goal <CODE>tags</CODE> or <CODE>TAGS</CODE> which constructs the tag files in
657all directories and for all files containing source code.
658
659</P>
660<P>
661Once your <TT>&lsquo;TAGS&rsquo;</TT> file is ready, the following commands assist
662the programmer at marking translatable strings in his set of sources.
663But these commands are necessarily driven from within a PO file
664window, and it is likely that you do not even have such a PO file yet.
665This is not a problem at all, as you may safely open a new, empty PO
666file, mainly for using these commands.  This empty PO file will slowly
667fill in while you mark strings as translatable in your program sources.
668
669</P>
670<DL COMPACT>
671
672<DT><KBD>,</KBD>
673<DD>
674<A NAME="IDX132"></A>
675Search through program sources for a string which looks like a
676candidate for translation (<CODE>po-tags-search</CODE>).
677
678<DT><KBD>M-,</KBD>
679<DD>
680<A NAME="IDX133"></A>
681Mark the last string found with <SAMP>&lsquo;_()&rsquo;</SAMP> (<CODE>po-mark-translatable</CODE>).
682
683<DT><KBD>M-.</KBD>
684<DD>
685<A NAME="IDX134"></A>
686Mark the last string found with a keyword taken from a set of possible
687keywords.  This command with a prefix allows some management of these
688keywords (<CODE>po-select-mark-and-mark</CODE>).
689
690</DL>
691
692<P>
693<A NAME="IDX135"></A>
694The <KBD>,</KBD> (<CODE>po-tags-search</CODE>) command searches for the next
695occurrence of a string which looks like a possible candidate for
696translation, and displays the program source in another Emacs window,
697positioned in such a way that the string is near the top of this other
698window.  If the string is too big to fit whole in this window, it is
699positioned so only its end is shown.  In any case, the cursor
700is left in the PO file window.  If the shown string would be better
701presented differently in different native languages, you may mark it
702using <KBD>M-,</KBD> or <KBD>M-.</KBD>.  Otherwise, you might rather ignore it
703and skip to the next string by merely repeating the <KBD>,</KBD> command.
704
705</P>
706<P>
707A string is a good candidate for translation if it contains a sequence
708of three or more letters.  A string containing at most two letters in
709a row will be considered as a candidate if it has more letters than
710non-letters.  The command disregards strings containing no letters,
711or isolated letters only.  It also disregards strings within comments,
712or strings already marked with some keyword PO mode knows (see below).
713
714</P>
715<P>
716If you have never told Emacs about some <TT>&lsquo;TAGS&rsquo;</TT> file to use, the
717command will request that you specify one from the minibuffer, the
718first time you use the command.  You may later change your <TT>&lsquo;TAGS&rsquo;</TT>
719file by using the regular Emacs command <KBD>M-x visit-tags-table</KBD>,
720which will ask you to name the precise <TT>&lsquo;TAGS&rsquo;</TT> file you want
721to use.  See section ���Tag Tables��� in <CITE>The Emacs Editor</CITE>.
722
723</P>
724<P>
725Each time you use the <KBD>,</KBD> command, the search resumes from where it was
726left by the previous search, and goes through all program sources,
727obeying the <TT>&lsquo;TAGS&rsquo;</TT> file, until all sources have been processed.
728However, by giving a prefix argument to the command (<KBD>C-u
729,)</KBD>, you may request that the search be restarted all over again
730from the first program source; but in this case, strings that you
731recently marked as translatable will be automatically skipped.
732
733</P>
734<P>
735Using this <KBD>,</KBD> command does not prevent using of other regular
736Emacs tags commands.  For example, regular <CODE>tags-search</CODE> or
737<CODE>tags-query-replace</CODE> commands may be used without disrupting the
738independent <KBD>,</KBD> search sequence.  However, as implemented, the
739<EM>initial</EM> <KBD>,</KBD> command (or the <KBD>,</KBD> command is used with a
740prefix) might also reinitialize the regular Emacs tags searching to the
741first tags file, this reinitialization might be considered spurious.
742
743</P>
744<P>
745<A NAME="IDX136"></A>
746<A NAME="IDX137"></A>
747The <KBD>M-,</KBD> (<CODE>po-mark-translatable</CODE>) command will mark the
748recently found string with the <SAMP>&lsquo;_&rsquo;</SAMP> keyword.  The <KBD>M-.</KBD>
749(<CODE>po-select-mark-and-mark</CODE>) command will request that you type
750one keyword from the minibuffer and use that keyword for marking
751the string.  Both commands will automatically create a new PO file
752untranslated entry for the string being marked, and make it the
753current entry (making it easy for you to immediately proceed to its
754translation, if you feel like doing it right away).  It is possible
755that the modifications made to the program source by <KBD>M-,</KBD> or
756<KBD>M-.</KBD> render some source line longer than 80 columns, forcing you
757to break and re-indent this line differently.  You may use the <KBD>O</KBD>
758command from PO mode, or any other window changing command from
759Emacs, to break out into the program source window, and do any
760needed adjustments.  You will have to use some regular Emacs command
761to return the cursor to the PO file window, if you want command
762<KBD>,</KBD> for the next string, say.
763
764</P>
765<P>
766The <KBD>M-.</KBD> command has a few built-in speedups, so you do not
767have to explicitly type all keywords all the time.  The first such
768speedup is that you are presented with a <EM>preferred</EM> keyword,
769which you may accept by merely typing <KBD><KBD>RET</KBD></KBD> at the prompt.
770The second speedup is that you may type any non-ambiguous prefix of the
771keyword you really mean, and the command will complete it automatically
772for you.  This also means that PO mode has to <EM>know</EM> all
773your possible keywords, and that it will not accept mistyped keywords.
774
775</P>
776<P>
777If you reply <KBD>?</KBD> to the keyword request, the command gives a
778list of all known keywords, from which you may choose.  When the
779command is prefixed by an argument (<KBD>C-u M-.</KBD>), it inhibits
780updating any program source or PO file buffer, and does some simple
781keyword management instead.  In this case, the command asks for a
782keyword, written in full, which becomes a new allowed keyword for
783later <KBD>M-.</KBD> commands.  Moreover, this new keyword automatically
784becomes the <EM>preferred</EM> keyword for later commands.  By typing
785an already known keyword in response to <KBD>C-u M-.</KBD>, one merely
786changes the <EM>preferred</EM> keyword and does nothing more.
787
788</P>
789<P>
790All keywords known for <KBD>M-.</KBD> are recognized by the <KBD>,</KBD> command
791when scanning for strings, and strings already marked by any of those
792known keywords are automatically skipped.  If many PO files are opened
793simultaneously, each one has its own independent set of known keywords.
794There is no provision in PO mode, currently, for deleting a known
795keyword, you have to quit the file (maybe using <KBD>q</KBD>) and reopen
796it afresh.  When a PO file is newly brought up in an Emacs window, only
797<SAMP>&lsquo;gettext&rsquo;</SAMP> and <SAMP>&lsquo;_&rsquo;</SAMP> are known as keywords, and <SAMP>&lsquo;gettext&rsquo;</SAMP>
798is preferred for the <KBD>M-.</KBD> command.  In fact, this is not useful to
799prefer <SAMP>&lsquo;_&rsquo;</SAMP>, as this one is already built in the <KBD>M-,</KBD> command.
800
801</P>
802
803
804<H2><A NAME="SEC17" HREF="gettext_toc.html#TOC17">4.6  Special Comments preceding Keywords</A></H2>
805
806<P>
807<A NAME="IDX138"></A>
808In C programs strings are often used within calls of functions from the
809<CODE>printf</CODE> family.  The special thing about these format strings is
810that they can contain format specifiers introduced with <KBD>%</KBD>.  Assume
811we have the code
812
813</P>
814
815<PRE>
816printf (gettext ("String `%s' has %d characters\n"), s, strlen (s));
817</PRE>
818
819<P>
820A possible German translation for the above string might be:
821
822</P>
823
824<PRE>
825"%d Zeichen lang ist die Zeichenkette `%s'"
826</PRE>
827
828<P>
829A C programmer, even if he cannot speak German, will recognize that
830there is something wrong here.  The order of the two format specifiers
831is changed but of course the arguments in the <CODE>printf</CODE> don't have.
832This will most probably lead to problems because now the length of the
833string is regarded as the address.
834
835</P>
836<P>
837To prevent errors at runtime caused by translations the <CODE>msgfmt</CODE>
838tool can check statically whether the arguments in the original and the
839translation string match in type and number.  If this is not the case
840and the <SAMP>&lsquo;-c&rsquo;</SAMP> option has been passed to <CODE>msgfmt</CODE>, <CODE>msgfmt</CODE>
841will give an error and refuse to produce a MO file.  Thus consequent
842use of <SAMP>&lsquo;msgfmt -c&rsquo;</SAMP> will catch the error, so that it cannot cause
843cause problems at runtime.
844
845</P>
846<P>
847If the word order in the above German translation would be correct one
848would have to write
849
850</P>
851
852<PRE>
853"%2$d Zeichen lang ist die Zeichenkette `%1$s'"
854</PRE>
855
856<P>
857The routines in <CODE>msgfmt</CODE> know about this special notation.
858
859</P>
860<P>
861Because not all strings in a program must be format strings it is not
862useful for <CODE>msgfmt</CODE> to test all the strings in the <TT>&lsquo;.po&rsquo;</TT> file.
863This might cause problems because the string might contain what looks
864like a format specifier, but the string is not used in <CODE>printf</CODE>.
865
866</P>
867<P>
868Therefore the <CODE>xgettext</CODE> adds a special tag to those messages it
869thinks might be a format string.  There is no absolute rule for this,
870only a heuristic.  In the <TT>&lsquo;.po&rsquo;</TT> file the entry is marked using the
871<CODE>c-format</CODE> flag in the <CODE>#,</CODE> comment line (see section <A HREF="gettext_3.html#SEC10">3  The Format of PO Files</A>).
872
873</P>
874<P>
875<A NAME="IDX139"></A>
876<A NAME="IDX140"></A>
877The careful reader now might say that this again can cause problems.
878The heuristic might guess it wrong.  This is true and therefore
879<CODE>xgettext</CODE> knows about a special kind of comment which lets
880the programmer take over the decision.  If in the same line as or
881the immediately preceding line to the <CODE>gettext</CODE> keyword
882the <CODE>xgettext</CODE> program finds a comment containing the words
883<CODE>xgettext:c-format</CODE>, it will mark the string in any case with
884the <CODE>c-format</CODE> flag.  This kind of comment should be used when
885<CODE>xgettext</CODE> does not recognize the string as a format string but
886it really is one and it should be tested.  Please note that when the
887comment is in the same line as the <CODE>gettext</CODE> keyword, it must be
888before the string to be translated.
889
890</P>
891<P>
892This situation happens quite often.  The <CODE>printf</CODE> function is often
893called with strings which do not contain a format specifier.  Of course
894one would normally use <CODE>fputs</CODE> but it does happen.  In this case
895<CODE>xgettext</CODE> does not recognize this as a format string but what
896happens if the translation introduces a valid format specifier?  The
897<CODE>printf</CODE> function will try to access one of the parameters but none
898exists because the original code does not pass any parameters.
899
900</P>
901<P>
902<CODE>xgettext</CODE> of course could make a wrong decision the other way
903round, i.e. a string marked as a format string actually is not a format
904string.  In this case the <CODE>msgfmt</CODE> might give too many warnings and
905would prevent translating the <TT>&lsquo;.po&rsquo;</TT> file.  The method to prevent
906this wrong decision is similar to the one used above, only the comment
907to use must contain the string <CODE>xgettext:no-c-format</CODE>.
908
909</P>
910<P>
911If a string is marked with <CODE>c-format</CODE> and this is not correct the
912user can find out who is responsible for the decision.  See
913section <A HREF="gettext_5.html#SEC22">5.1  Invoking the <CODE>xgettext</CODE> Program</A> to see how the <CODE>--debug</CODE> option can be
914used for solving this problem.
915
916</P>
917
918
919<H2><A NAME="SEC18" HREF="gettext_toc.html#TOC18">4.7  Special Cases of Translatable Strings</A></H2>
920
921<P>
922<A NAME="IDX141"></A>
923The attentive reader might now point out that it is not always possible
924to mark translatable string with <CODE>gettext</CODE> or something like this.
925Consider the following case:
926
927</P>
928
929<PRE>
930{
931  static const char *messages[] = {
932    "some very meaningful message",
933    "and another one"
934  };
935  const char *string;
936  ...
937  string
938    = index &#62; 1 ? "a default message" : messages[index];
939
940  fputs (string);
941  ...
942}
943</PRE>
944
945<P>
946While it is no problem to mark the string <CODE>"a default message"</CODE> it
947is not possible to mark the string initializers for <CODE>messages</CODE>.
948What is to be done?  We have to fulfill two tasks.  First we have to mark the
949strings so that the <CODE>xgettext</CODE> program (see section <A HREF="gettext_5.html#SEC22">5.1  Invoking the <CODE>xgettext</CODE> Program</A>)
950can find them, and second we have to translate the string at runtime
951before printing them.
952
953</P>
954<P>
955The first task can be fulfilled by creating a new keyword, which names a
956no-op.  For the second we have to mark all access points to a string
957from the array.  So one solution can look like this:
958
959</P>
960
961<PRE>
962#define gettext_noop(String) String
963
964{
965  static const char *messages[] = {
966    gettext_noop ("some very meaningful message"),
967    gettext_noop ("and another one")
968  };
969  const char *string;
970  ...
971  string
972    = index &#62; 1 ? gettext ("a default message") : gettext (messages[index]);
973
974  fputs (string);
975  ...
976}
977</PRE>
978
979<P>
980Please convince yourself that the string which is written by
981<CODE>fputs</CODE> is translated in any case.  How to get <CODE>xgettext</CODE> know
982the additional keyword <CODE>gettext_noop</CODE> is explained in section <A HREF="gettext_5.html#SEC22">5.1  Invoking the <CODE>xgettext</CODE> Program</A>.
983
984</P>
985<P>
986The above is of course not the only solution.  You could also come along
987with the following one:
988
989</P>
990
991<PRE>
992#define gettext_noop(String) String
993
994{
995  static const char *messages[] = {
996    gettext_noop ("some very meaningful message",
997    gettext_noop ("and another one")
998  };
999  const char *string;
1000  ...
1001  string
1002    = index &#62; 1 ? gettext_noop ("a default message") : messages[index];
1003
1004  fputs (gettext (string));
1005  ...
1006}
1007</PRE>
1008
1009<P>
1010But this has a drawback.  The programmer has to take care that
1011he uses <CODE>gettext_noop</CODE> for the string <CODE>"a default message"</CODE>.
1012A use of <CODE>gettext</CODE> could have in rare cases unpredictable results.
1013
1014</P>
1015<P>
1016One advantage is that you need not make control flow analysis to make
1017sure the output is really translated in any case.  But this analysis is
1018generally not very difficult.  If it should be in any situation you can
1019use this second method in this situation.
1020
1021</P>
1022
1023
1024<H2><A NAME="SEC19" HREF="gettext_toc.html#TOC19">4.8  Marking Proper Names for Translation</A></H2>
1025
1026<P>
1027Should names of persons, cities, locations etc. be marked for translation
1028or not?  People who only know languages that can be written with Latin
1029letters (English, Spanish, French, German, etc.) are tempted to say ���no���,
1030because names usually do not change when transported between these languages.
1031However, in general when translating from one script to another, names
1032are translated too, usually phonetically or by transliteration.  For
1033example, Russian or Greek names are converted to the Latin alphabet when
1034being translated to English, and English or French names are converted
1035to the Katakana script when being translated to Japanese.  This is
1036necessary because the speakers of the target language in general cannot
1037read the script the name is originally written in.
1038
1039</P>
1040<P>
1041As a programmer, you should therefore make sure that names are marked
1042for translation, with a special comment telling the translators that it
1043is a proper name and how to pronounce it.  Like this:
1044
1045</P>
1046
1047<PRE>
1048printf (_("Written by %s.\n"),
1049        /* TRANSLATORS: This is a proper name.  See the gettext
1050           manual, section Names.  Note this is actually a non-ASCII
1051           name: The first name is (with Unicode escapes)
1052           "Fran\u00e7ois" or (with HTML entities) "Fran&#38;ccedil;ois".
1053           Pronunciation is like "fraa-swa pee-nar".  */
1054        _("Francois Pinard"));
1055</PRE>
1056
1057<P>
1058As a translator, you should use some care when translating names, because
1059it is frustrating if people see their names mutilated or distorted.  If
1060your language uses the Latin script, all you need to do is to reproduce
1061the name as perfectly as you can within the usual character set of your
1062language.  In this particular case, this means to provide a translation
1063containing the c-cedilla character.  If your language uses a different
1064script and the people speaking it don't usually read Latin words, it means
1065transliteration; but you should still give, in parentheses, the original
1066writing of the name -- for the sake of the people that do read the Latin
1067script.  Here is an example, using Greek as the target script:
1068
1069</P>
1070
1071<PRE>
1072#. This is a proper name.  See the gettext
1073#. manual, section Names.  Note this is actually a non-ASCII
1074#. name: The first name is (with Unicode escapes)
1075#. "Fran\u00e7ois" or (with HTML entities) "Fran&#38;ccedil;ois".
1076#. Pronunciation is like "fraa-swa pee-nar".
1077msgid "Francois Pinard"
1078msgstr "\phi\rho\alpha\sigma\omicron\alpha \pi\iota\nu\alpha\rho"
1079       " (Francois Pinard)"
1080</PRE>
1081
1082<P>
1083Because translation of names is such a sensitive domain, it is a good
1084idea to test your translation before submitting it.
1085
1086</P>
1087<P>
1088The translation project <A HREF="http://sourceforge.net/projects/translation">http://sourceforge.net/projects/translation</A>
1089has set up a POT file and translation domain consisting of program author
1090names, with better facilities for the translator than those presented here.
1091Namely, there the original name is written directly in Unicode (rather
1092than with Unicode escapes or HTML entities), and the pronunciation is
1093denoted using the International Phonetic Alphabet (see
1094<A HREF="http://www.wikipedia.org/wiki/International_Phonetic_Alphabet">http://www.wikipedia.org/wiki/International_Phonetic_Alphabet</A>).
1095
1096</P>
1097<P>
1098However, we don't recommend this approach for all POT files in all packages,
1099because this would force translators to use PO files in UTF-8 encoding,
1100which is - in the current state of software (as of 2003) - a major hassle
1101for translators using GNU Emacs or XEmacs with po-mode.
1102
1103</P>
1104
1105
1106<H2><A NAME="SEC20" HREF="gettext_toc.html#TOC20">4.9  Preparing Library Sources</A></H2>
1107
1108<P>
1109When you are preparing a library, not a program, for the use of
1110<CODE>gettext</CODE>, only a few details are different.  Here we assume that
1111the library has a translation domain and a POT file of its own.  (If
1112it uses the translation domain and POT file of the main program, then
1113the previous sections apply without changes.)
1114
1115</P>
1116
1117<OL>
1118<LI>
1119
1120The library code doesn't call <CODE>setlocale (LC_ALL, "")</CODE>.  It's the
1121responsibility of the main program to set the locale.  The library's
1122documentation should mention this fact, so that developers of programs
1123using the library are aware of it.
1124
1125<LI>
1126
1127The library code doesn't call <CODE>textdomain (PACKAGE)</CODE>, because it
1128would interfere with the text domain set by the main program.
1129
1130<LI>
1131
1132The initialization code for a program was
1133
1134
1135<PRE>
1136  setlocale (LC_ALL, "");
1137  bindtextdomain (PACKAGE, LOCALEDIR);
1138  textdomain (PACKAGE);
1139</PRE>
1140
1141For a library it is reduced to
1142
1143
1144<PRE>
1145  bindtextdomain (PACKAGE, LOCALEDIR);
1146</PRE>
1147
1148If your library's API doesn't already have an initialization function,
1149you need to create one, containing at least the <CODE>bindtextdomain</CODE>
1150invocation.  However, you usually don't need to export and document this
1151initialization function: It is sufficient that all entry points of the
1152library call the initialization function if it hasn't been called before.
1153The typical idiom used to achieve this is a static boolean variable that
1154indicates whether the initialization function has been called. Like this:
1155
1156
1157<PRE>
1158static bool libfoo_initialized;
1159
1160static void
1161libfoo_initialize (void)
1162{
1163  bindtextdomain (PACKAGE, LOCALEDIR);
1164  libfoo_initialized = true;
1165}
1166
1167/* This function is part of the exported API.  */
1168struct foo *
1169create_foo (...)
1170{
1171  /* Must ensure the initialization is performed.  */
1172  if (!libfoo_initialized)
1173    libfoo_initialize ();
1174  ...
1175}
1176
1177/* This function is part of the exported API.  The argument must be
1178   non-NULL and have been created through create_foo().  */
1179int
1180foo_refcount (struct foo *argument)
1181{
1182  /* No need to invoke the initialization function here, because
1183     create_foo() must already have been called before.  */
1184  ...
1185}
1186</PRE>
1187
1188<LI>
1189
1190The usual declaration of the <SAMP>&lsquo;_&rsquo;</SAMP> macro in each source file was
1191
1192
1193<PRE>
1194#include &#60;libintl.h&#62;
1195#define _(String) gettext (String)
1196</PRE>
1197
1198for a program.  For a library, which has its own translation domain,
1199it reads like this:
1200
1201
1202<PRE>
1203#include &#60;libintl.h&#62;
1204#define _(String) dgettext (PACKAGE, String)
1205</PRE>
1206
1207In other words, <CODE>dgettext</CODE> is used instead of <CODE>gettext</CODE>.
1208Similarly, the <CODE>dngettext</CODE> function should be used in place of the
1209<CODE>ngettext</CODE> function.
1210</OL>
1211
1212<P><HR><P>
1213Go to the <A HREF="gettext_1.html">first</A>, <A HREF="gettext_3.html">previous</A>, <A HREF="gettext_5.html">next</A>, <A HREF="gettext_25.html">last</A> section, <A HREF="gettext_toc.html">table of contents</A>.
1214</BODY>
1215</HTML>
1216