Commit | Line | Data |
---|---|---|
fd936c91 MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004 | |
4 | @c Free Software Foundation, Inc. | |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
7 | @page | |
8 | @node Internationalization | |
9 | @section Support for Internationalization | |
10 | ||
09ecf78c KR |
11 | Guile provides an interface to GNU @code{gettext} for translating |
12 | message strings (@pxref{Introduction,,, gettext, GNU @code{gettext} | |
13 | utilities}). | |
14 | ||
15 | Messages are collected in domains, so different libraries and programs | |
16 | maintain different message catalogues. The @var{domain} parameter in | |
17 | the functions below is a string (it becomes part of the message | |
18 | catalog filename). | |
19 | ||
20 | When @code{gettext} is not available, or if Guile was configured | |
21 | @samp{--without-nls}, dummy functions doing no translation are | |
22 | provided. | |
23 | ||
24 | @deffn {Scheme Procedure} gettext msg [domain [category]] | |
25 | @deffnx {C Function} scm_gettext (msg, domain, category) | |
26 | Return the translation of @var{msg} in @var{domain}. @var{domain} is | |
27 | optional and defaults to the domain set through @code{textdomain} | |
28 | below. @var{category} is optional and defaults to @code{LC_MESSAGES} | |
29 | (@pxref{Locales}). | |
30 | ||
31 | Normal usage is for @var{msg} to be a literal string. | |
32 | @command{xgettext} can extract those from the source to form a message | |
33 | catalogue ready for translators (@pxref{xgettext Invocation,, Invoking | |
34 | the @command{xgettext} Program, gettext, GNU @code{gettext} | |
35 | utilities}). | |
36 | ||
37 | @example | |
38 | (display (gettext "You are in a maze of twisty passages.")) | |
39 | @end example | |
40 | ||
41 | @code{_} is a commonly used shorthand, an application can make that an | |
42 | alias for @code{gettext}. Or a library can make a definition that | |
43 | uses its specific @var{domain} (so an application can change the | |
44 | default without affecting the library). | |
45 | ||
46 | @example | |
47 | (define (_ msg) (gettext msg "mylibrary")) | |
48 | (display (_ "File not found.")) | |
49 | @end example | |
50 | ||
51 | @code{_} is also a good place to perhaps strip disambiguating extra | |
52 | text from the message string, as for instance in @ref{GUI program | |
53 | problems,, How to use @code{gettext} in GUI programs, gettext, GNU | |
54 | @code{gettext} utilities}. | |
fd936c91 MV |
55 | @end deffn |
56 | ||
09ecf78c KR |
57 | @deffn {Scheme Procedure} ngettext msg msgplural n [domain [category]] |
58 | @deffnx {C Function} scm_ngettext (msg, msgplural, n, domain, category) | |
59 | Return the translation of @var{msg}/@var{msgplural} in @var{domain}, | |
60 | with a plural form chosen appropriately for the number @var{n}. | |
61 | @var{domain} is optional and defaults to the domain set through | |
62 | @code{textdomain} below. @var{category} is optional and defaults to | |
63 | @code{LC_MESSAGES} (@pxref{Locales}). | |
64 | ||
65 | @var{msg} is the singular form, and @var{msgplural} the plural. When | |
66 | no translation is available, @var{msg} is used if @math{@var{n} = 1}, | |
67 | or @var{msgplural} otherwise. When translated, the message catalogue | |
68 | can have a different rule, and can have more than two possible forms. | |
69 | ||
70 | As per @code{gettext} above, normal usage is for @var{msg} and | |
71 | @var{msgplural} to be literal strings, since @command{xgettext} can | |
72 | extract them from the source to build a message catalogue. For | |
73 | example, | |
74 | ||
75 | @example | |
76 | (define (done n) | |
77 | (format #t (ngettext "~a file processed\n" | |
78 | "~a files processed\n" n) | |
79 | n)) | |
80 | ||
81 | (done 1) @print{} 1 file processed | |
82 | (done 3) @print{} 3 files processed | |
83 | @end example | |
84 | ||
85 | It's important to use @code{ngettext} rather than plain @code{gettext} | |
86 | for plurals, since the rules for singular and plural forms in English | |
87 | are not the same in other languages. Only @code{ngettext} will allow | |
88 | translators to give correct forms. | |
fd936c91 MV |
89 | @end deffn |
90 | ||
09ecf78c KR |
91 | @deffn {Scheme Procedure} textdomain [domain] |
92 | @deffnx {C Function} scm_textdomain (domain) | |
93 | Get or set the default gettext domain. When called with no parameter | |
94 | the current domain is returned. When called with a parameter, | |
95 | @var{domain} is set as the current domain, and that new value | |
96 | returned. For example, | |
97 | ||
98 | @example | |
99 | (textdomain "myprog") | |
100 | @result{} "myprog" | |
101 | @end example | |
fd936c91 MV |
102 | @end deffn |
103 | ||
09ecf78c KR |
104 | @deffn {Scheme Procedure} bindtextdomain domain [directory] |
105 | @deffnx {C Function} scm_bindtextdomain (domain, directory) | |
106 | Get or set the directory under which to find message files for | |
107 | @var{domain}. When called without a @var{directory} the current | |
108 | setting is returned. When called with a @var{directory}, | |
109 | @var{directory} is set for @var{domain} and that new setting returned. | |
110 | For example, | |
111 | ||
112 | @example | |
113 | (bindtextdomain "myprog" "/my/tree/share/locale") | |
114 | @result{} "/my/tree/share/locale" | |
115 | @end example | |
116 | ||
117 | When using Autoconf/Automake, an application should arrange for the | |
118 | configured @code{localedir} to get into the program (by substituting, | |
119 | or by generating a config file) and set that for its domain. This | |
120 | ensures the catalogue can be found even when installed in a | |
121 | non-standard location. | |
fd936c91 MV |
122 | @end deffn |
123 | ||
09ecf78c KR |
124 | @deffn {Scheme Procedure} bind-textdomain-codeset domain [encoding] |
125 | @deffnx {C Function} scm_bind_textdomain_codeset (domain, encoding) | |
126 | Get or set the text encoding to be used by @code{gettext} for messages | |
127 | from @var{domain}. @var{encoding} is a string, the name of a coding | |
128 | system, for instance @nicode{"8859_1"}. (On a Unix/POSIX system the | |
129 | @command{iconv} program can list all available encodings.) | |
130 | ||
131 | When called without an @var{encoding} the current setting is returned, | |
132 | or @code{#f} if none yet set. When called with an @var{encoding}, it | |
133 | is set for @var{domain} and that new setting returned. For example, | |
134 | ||
135 | @example | |
136 | (bind-textdomain-codeset "myprog") | |
137 | @result{} #f | |
138 | (bind-textdomain-codeset "myprog" "latin-9") | |
139 | @result{} "latin-9" | |
140 | @end example | |
141 | ||
142 | The encoding requested can be different from the translated data file, | |
143 | messages will be recoded as necessary. But note that when there is no | |
144 | translation, @code{gettext} returns its @var{msg} unchanged, ie.@: | |
145 | without any recoding. For that reason source message strings are best | |
146 | as plain ASCII. | |
147 | ||
148 | Currently Guile has no understanding of multi-byte characters, and | |
149 | string functions won't recognise character boundaries in multi-byte | |
150 | strings. An application will at least be able to pass such strings | |
151 | through to some output though. Perhaps this will change in the | |
152 | future. | |
fd936c91 MV |
153 | @end deffn |
154 | ||
155 | @c Local Variables: | |
156 | @c TeX-master: "guile.texi" | |
157 | @c End: |