Added missing files for `(ice-9 i18n)'.
[bpt/guile.git] / libguile / gettext.c
CommitLineData
5b3a39c7
LC
1/* Copyright (C) 2004, 2006 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19#if HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include "libguile/_scm.h"
24#include "libguile/feature.h"
25#include "libguile/strings.h"
26#include "libguile/dynwind.h"
27
28#include "libguile/gettext.h"
29#include "libgettext.h"
30#include <locale.h>
31
32
33int
34scm_i_to_lc_category (SCM category, int allow_lc_all)
35{
36 int c_category = scm_to_int (category);
37 switch (c_category)
38 {
39#ifdef LC_CTYPE
40 case LC_CTYPE:
41#endif
42#ifdef LC_NUMERIC
43 case LC_NUMERIC:
44#endif
45#ifdef LC_COLLATE
46 case LC_COLLATE:
47#endif
48#ifdef LC_TIME
49 case LC_TIME:
50#endif
51#ifdef LC_MONETARY
52 case LC_MONETARY:
53#endif
54#ifdef LC_MESSAGES
55 case LC_MESSAGES:
56#endif
57#ifdef LC_PAPER
58 case LC_PAPER:
59#endif
60#ifdef LC_NAME
61 case LC_NAME:
62#endif
63#ifdef LC_ADDRESS
64 case LC_ADDRESS:
65#endif
66#ifdef LC_TELEPHONE
67 case LC_TELEPHONE:
68#endif
69#ifdef LC_MEASUREMENT
70 case LC_MEASUREMENT:
71#endif
72#ifdef LC_IDENTIFICATION
73 case LC_IDENTIFICATION:
74#endif
75 return c_category;
76#ifdef LC_ALL
77 case LC_ALL:
78 if (allow_lc_all)
79 return c_category;
80#endif
81 }
82 scm_wrong_type_arg (0, 0, category);
83}
84
85SCM_DEFINE (scm_gettext, "gettext", 1, 2, 0,
86 (SCM msgid, SCM domain, SCM category),
87 "Return the translation of @var{msgid} in the message domain "
88 "@var{domain}. @var{domain} is optional and defaults to the "
89 "domain set through (textdomain). @var{category} is optional "
90 "and defaults to LC_MESSAGES.")
91#define FUNC_NAME s_scm_gettext
92{
93 char *c_msgid;
94 char const *c_result;
95 SCM result;
96
97 scm_dynwind_begin (0);
98
99 c_msgid = scm_to_locale_string (msgid);
100 scm_dynwind_free (c_msgid);
101
102 if (SCM_UNBNDP (domain))
103 {
104 /* 1 argument case. */
105 c_result = gettext (c_msgid);
106 }
107 else
108 {
109 char *c_domain;
110
111 c_domain = scm_to_locale_string (domain);
112 scm_dynwind_free (c_domain);
113
114 if (SCM_UNBNDP (category))
115 {
116 /* 2 argument case. */
117 c_result = dgettext (c_domain, c_msgid);
118 }
119 else
120 {
121 /* 3 argument case. */
122 int c_category;
123
124 c_category = scm_i_to_lc_category (category, 0);
125 c_result = dcgettext (c_domain, c_msgid, c_category);
126 }
127 }
128
129 if (c_result == c_msgid)
130 result = msgid;
131 else
132 result = scm_from_locale_string (c_result);
133
134 scm_dynwind_end ();
135 return result;
136}
137#undef FUNC_NAME
138
139
140SCM_DEFINE (scm_ngettext, "ngettext", 3, 2, 0,
141 (SCM msgid, SCM msgid_plural, SCM n, SCM domain, SCM category),
142 "Return the translation of @var{msgid}/@var{msgid_plural} in the "
143 "message domain @var{domain}, with the plural form being chosen "
144 "appropriately for the number @var{n}. @var{domain} is optional "
145 "and defaults to the domain set through (textdomain). "
146 "@var{category} is optional and defaults to LC_MESSAGES.")
147#define FUNC_NAME s_scm_ngettext
148{
149 char *c_msgid;
150 char *c_msgid_plural;
151 unsigned long c_n;
152 const char *c_result;
153 SCM result;
154
155 scm_dynwind_begin (0);
156
157 c_msgid = scm_to_locale_string (msgid);
158 scm_dynwind_free (c_msgid);
159
160 c_msgid_plural = scm_to_locale_string (msgid_plural);
161 scm_dynwind_free (c_msgid_plural);
162
163 c_n = scm_to_ulong (n);
164
165 if (SCM_UNBNDP (domain))
166 {
167 /* 3 argument case. */
168 c_result = ngettext (c_msgid, c_msgid_plural, c_n);
169 }
170 else
171 {
172 char *c_domain;
173
174 c_domain = scm_to_locale_string (domain);
175 scm_dynwind_free (c_domain);
176
177 if (SCM_UNBNDP (category))
178 {
179 /* 4 argument case. */
180 c_result = dngettext (c_domain, c_msgid, c_msgid_plural, c_n);
181 }
182 else
183 {
184 /* 5 argument case. */
185 int c_category;
186
187 c_category = scm_i_to_lc_category (category, 0);
188 c_result = dcngettext (c_domain, c_msgid, c_msgid_plural, c_n,
189 c_category);
190 }
191 }
192
193 if (c_result == c_msgid)
194 result = msgid;
195 else if (c_result == c_msgid_plural)
196 result = msgid_plural;
197 else
198 result = scm_from_locale_string (c_result);
199
200 scm_dynwind_end ();
201 return result;
202}
203#undef FUNC_NAME
204
205SCM_DEFINE (scm_textdomain, "textdomain", 0, 1, 0,
206 (SCM domainname),
207 "If optional parameter @var{domainname} is supplied, "
208 "set the textdomain. "
209 "Return the textdomain.")
210#define FUNC_NAME s_scm_textdomain
211{
212 char const *c_result;
213 char *c_domain;
214 SCM result = SCM_BOOL_F;
215
216 scm_dynwind_begin (0);
217
218 if (SCM_UNBNDP (domainname))
219 c_domain = NULL;
220 else
221 {
222 c_domain = scm_to_locale_string (domainname);
223 scm_dynwind_free (c_domain);
224 }
225
226 c_result = textdomain (c_domain);
227 if (c_result != NULL)
228 result = scm_from_locale_string (c_result);
229 else if (!SCM_UNBNDP (domainname))
230 SCM_SYSERROR;
231
232 scm_dynwind_end ();
233 return result;
234}
235#undef FUNC_NAME
236
237SCM_DEFINE (scm_bindtextdomain, "bindtextdomain", 1, 1, 0,
238 (SCM domainname, SCM directory),
239 "If optional parameter @var{directory} is supplied, "
240 "set message catalogs to directory @var{directory}. "
241 "Return the directory bound to @var{domainname}.")
242#define FUNC_NAME s_scm_bindtextdomain
243{
244 char *c_domain;
245 char *c_directory;
246 char const *c_result;
247 SCM result;
248
249 scm_dynwind_begin (0);
250
251 if (SCM_UNBNDP (directory))
252 c_directory = NULL;
253 else
254 {
255 c_directory = scm_to_locale_string (directory);
256 scm_dynwind_free (c_directory);
257 }
258
259 c_domain = scm_to_locale_string (domainname);
260 scm_dynwind_free (c_domain);
261
262 c_result = bindtextdomain (c_domain, c_directory);
263
264 if (c_result != NULL)
265 result = scm_from_locale_string (c_result);
266 else if (!SCM_UNBNDP (directory))
267 SCM_SYSERROR;
268 else
269 result = SCM_BOOL_F;
270
271 scm_dynwind_end ();
272 return result;
273}
274#undef FUNC_NAME
275
276SCM_DEFINE (scm_bind_textdomain_codeset, "bind-textdomain-codeset", 1, 1, 0,
277 (SCM domainname, SCM encoding),
278 "If optional parameter @var{encoding} is supplied, "
279 "set encoding for message catalogs of @var{domainname}. "
280 "Return the encoding of @var{domainname}.")
281#define FUNC_NAME s_scm_bind_textdomain_codeset
282{
283 char *c_domain;
284 char *c_encoding;
285 char const *c_result;
286 SCM result;
287
288 scm_dynwind_begin (0);
289
290 if (SCM_UNBNDP (encoding))
291 c_encoding = NULL;
292 else
293 {
294 c_encoding = scm_to_locale_string (encoding);
295 scm_dynwind_free (c_encoding);
296 }
297
298 c_domain = scm_to_locale_string (domainname);
299 scm_dynwind_free (c_domain);
300
301 c_result = bind_textdomain_codeset (c_domain, c_encoding);
302
303 if (c_result != NULL)
304 result = scm_from_locale_string (c_result);
305 else if (!SCM_UNBNDP (encoding))
306 SCM_SYSERROR;
307 else
308 result = SCM_BOOL_F;
309
310 scm_dynwind_end ();
311 return result;
312}
313#undef FUNC_NAME
314
315void
316scm_init_gettext ()
317{
318 /* When gettext support was first added (in 1.8.0), it provided feature
319 `i18n'. We keep this as is although the name is a bit misleading
320 now. */
321 scm_add_feature ("i18n");
322
323#include "libguile/gettext.x"
324}
325
326
327/*
328 Local Variables:
329 c-file-style: "gnu"
330 End:
331*/