]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/i18n.c
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / libguile / i18n.c
1 /* Copyright (C) 2004, 2006, 2008 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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include "libguile/_scm.h"
23 #include "libguile/feature.h"
24 #include "libguile/i18n.h"
25 #include "libguile/strings.h"
26 #include "libguile/dynwind.h"
27
28 #include "gettext.h"
29 #include <locale.h>
30
31
32 int
33 scm_i_to_lc_category (SCM category, int allow_lc_all)
34 {
35   int c_category = scm_to_int (category);
36   switch (c_category)
37     {
38 #ifdef LC_CTYPE
39     case LC_CTYPE:
40 #endif
41 #ifdef LC_NUMERIC
42     case LC_NUMERIC:
43 #endif
44 #ifdef LC_COLLATE
45     case LC_COLLATE:
46 #endif
47 #ifdef LC_TIME
48     case LC_TIME:
49 #endif
50 #ifdef LC_MONETARY
51     case LC_MONETARY:
52 #endif
53 #ifdef LC_MESSAGES
54     case LC_MESSAGES:
55 #endif
56 #ifdef LC_PAPER
57     case LC_PAPER:
58 #endif
59 #ifdef LC_NAME
60     case LC_NAME:
61 #endif
62 #ifdef LC_ADDRESS
63     case LC_ADDRESS:
64 #endif
65 #ifdef LC_TELEPHONE
66     case LC_TELEPHONE:
67 #endif
68 #ifdef LC_MEASUREMENT
69     case LC_MEASUREMENT:
70 #endif
71 #ifdef LC_IDENTIFICATION
72     case LC_IDENTIFICATION:
73 #endif
74       return c_category;
75 #ifdef LC_ALL
76     case LC_ALL:
77       if (allow_lc_all)
78         return c_category;
79 #endif  
80     }
81   scm_wrong_type_arg (0, 0, category);
82 }
83
84 SCM_DEFINE (scm_gettext, "gettext", 1, 2, 0,
85             (SCM msgid, SCM domain, SCM category),
86             "Return the translation of @var{msgid} in the message domain "
87             "@var{domain}. @var{domain} is optional and defaults to the "
88             "domain set through (textdomain).  @var{category} is optional "
89             "and defaults to LC_MESSAGES.")
90 #define FUNC_NAME s_scm_gettext
91 {
92   char *c_msgid;
93   char const *c_result;
94   SCM result;
95
96   scm_dynwind_begin (0);
97
98   c_msgid = scm_to_locale_string (msgid);
99   scm_dynwind_free (c_msgid);
100
101   if (SCM_UNBNDP (domain))
102     {
103       /* 1 argument case.  */
104       c_result = gettext (c_msgid);
105     }
106   else
107     {
108       char *c_domain;
109
110       c_domain = scm_to_locale_string (domain);
111       scm_dynwind_free (c_domain);
112
113       if (SCM_UNBNDP (category))
114         {
115           /* 2 argument case.  */
116           c_result = dgettext (c_domain, c_msgid);
117         }
118       else
119         {
120           /* 3 argument case.  */
121           int c_category;
122
123           c_category = scm_i_to_lc_category (category, 0);
124           c_result = dcgettext (c_domain, c_msgid, c_category);
125         }
126     }
127
128   if (c_result == c_msgid)
129     result = msgid;
130   else
131     result = scm_from_locale_string (c_result);
132
133   scm_dynwind_end ();
134   return result;
135 }
136 #undef FUNC_NAME
137
138
139 SCM_DEFINE (scm_ngettext, "ngettext", 3, 2, 0,
140             (SCM msgid, SCM msgid_plural, SCM n, SCM domain, SCM category),
141             "Return the translation of @var{msgid}/@var{msgid_plural} in the "
142             "message domain @var{domain}, with the plural form being chosen "
143             "appropriately for the number @var{n}.  @var{domain} is optional "
144             "and defaults to the domain set through (textdomain). "
145             "@var{category} is optional and defaults to LC_MESSAGES.")
146 #define FUNC_NAME s_scm_ngettext
147 {
148   char *c_msgid;
149   char *c_msgid_plural;
150   unsigned long c_n;
151   const char *c_result;
152   SCM result;
153
154   scm_dynwind_begin (0);
155
156   c_msgid = scm_to_locale_string (msgid);
157   scm_dynwind_free (c_msgid);
158
159   c_msgid_plural = scm_to_locale_string (msgid_plural);
160   scm_dynwind_free (c_msgid_plural);
161
162   c_n = scm_to_ulong (n);
163
164   if (SCM_UNBNDP (domain))
165     {
166       /* 3 argument case.  */
167       c_result = ngettext (c_msgid, c_msgid_plural, c_n);
168     }
169   else
170     {
171       char *c_domain;
172
173       c_domain = scm_to_locale_string (domain);
174       scm_dynwind_free (c_domain);
175
176       if (SCM_UNBNDP (category))
177         {
178           /* 4 argument case.  */
179           c_result = dngettext (c_domain, c_msgid, c_msgid_plural, c_n);
180         }
181       else
182         {
183           /* 5 argument case.  */
184           int c_category;
185
186           c_category = scm_i_to_lc_category (category, 0);
187           c_result = dcngettext (c_domain, c_msgid, c_msgid_plural, c_n,
188                                  c_category);
189         }
190     }
191
192   if (c_result == c_msgid)
193     result = msgid;
194   else if (c_result == c_msgid_plural)
195     result = msgid_plural;
196   else
197     result = scm_from_locale_string (c_result);
198   
199   scm_dynwind_end ();
200   return result;
201 }
202 #undef FUNC_NAME
203
204 SCM_DEFINE (scm_textdomain, "textdomain", 0, 1, 0,
205             (SCM domainname),
206             "If optional parameter @var{domainname} is supplied, "
207             "set the textdomain.  "
208             "Return the textdomain.")
209 #define FUNC_NAME s_scm_textdomain
210 {
211   char const *c_result;
212   char *c_domain;
213   SCM result = SCM_BOOL_F;
214
215   scm_dynwind_begin (0);
216
217   if (SCM_UNBNDP (domainname))
218     c_domain = NULL;
219   else
220     {
221       c_domain = scm_to_locale_string (domainname);
222       scm_dynwind_free (c_domain);
223     }
224
225   c_result = textdomain (c_domain);
226   if (c_result != NULL)
227     result = scm_from_locale_string (c_result);
228   else if (!SCM_UNBNDP (domainname))
229     SCM_SYSERROR;
230
231   scm_dynwind_end ();
232   return result;
233 }
234 #undef FUNC_NAME
235
236 SCM_DEFINE (scm_bindtextdomain, "bindtextdomain", 1, 1, 0,
237             (SCM domainname, SCM directory),
238             "If optional parameter @var{directory} is supplied, "
239             "set message catalogs to directory @var{directory}.  "
240             "Return the directory bound to @var{domainname}.")
241 #define FUNC_NAME s_scm_bindtextdomain
242 {
243   char *c_domain;
244   char *c_directory;
245   char const *c_result;
246   SCM result;
247
248   scm_dynwind_begin (0);
249
250   if (SCM_UNBNDP (directory))
251     c_directory = NULL;
252   else
253     {
254       c_directory = scm_to_locale_string (directory);
255       scm_dynwind_free (c_directory);
256     }
257
258   c_domain = scm_to_locale_string (domainname);
259   scm_dynwind_free (c_domain);
260
261   c_result = bindtextdomain (c_domain, c_directory);
262
263   if (c_result != NULL)
264     result = scm_from_locale_string (c_result);
265   else if (!SCM_UNBNDP (directory))
266     SCM_SYSERROR;
267   else
268     result = SCM_BOOL_F;
269
270   scm_dynwind_end ();
271   return result;
272 }
273 #undef FUNC_NAME
274
275 SCM_DEFINE (scm_bind_textdomain_codeset, "bind-textdomain-codeset", 1, 1, 0,
276             (SCM domainname, SCM encoding),
277             "If optional parameter @var{encoding} is supplied, "
278             "set encoding for message catalogs of @var{domainname}.  "
279             "Return the encoding of @var{domainname}.")
280 #define FUNC_NAME s_scm_bind_textdomain_codeset
281 {
282   char *c_domain;
283   char *c_encoding;
284   char const *c_result;
285   SCM result;
286
287   scm_dynwind_begin (0);
288
289   if (SCM_UNBNDP (encoding))
290     c_encoding = NULL;
291   else
292     {
293       c_encoding = scm_to_locale_string (encoding);
294       scm_dynwind_free (c_encoding);
295     }
296
297   c_domain = scm_to_locale_string (domainname);
298   scm_dynwind_free (c_domain);
299
300   c_result = bind_textdomain_codeset (c_domain, c_encoding);
301
302   if (c_result != NULL)
303     result = scm_from_locale_string (c_result);
304   else if (!SCM_UNBNDP (encoding))
305     SCM_SYSERROR;
306   else
307     result = SCM_BOOL_F;
308
309   scm_dynwind_end ();
310   return result;
311 }
312 #undef FUNC_NAME
313
314 void 
315 scm_init_i18n ()
316 {
317   scm_add_feature ("i18n");
318 #include "libguile/i18n.x"
319 }
320
321
322 /*
323   Local Variables:
324   c-file-style: "gnu"
325   End:
326 */