Initial revision
[mikachu/openbox.git] / src / i18n.cc
1 // i18n.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #define   _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 #  include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #include "i18n.h"
34
35 #include <X11/Xlocale.h>
36
37 #ifdef    STDC_HEADERS
38 #  include <stdlib.h>
39 #  include <string.h>
40 #  include <stdio.h>
41 #endif // STDC_HEADERS
42
43 #ifdef    HAVE_LOCALE_H
44 #  include <locale.h>
45 #endif // HAVE_LOCALE_H
46
47 // the rest of bb source uses True and False from X, so we continue that
48 #define True true
49 #define False false
50
51 static I18n static_i18n;
52 I18n *i18n;
53
54 void NLSInit(const char *catalog) {
55   i18n = &static_i18n;
56
57   i18n->openCatalog(catalog);
58 }
59
60
61 I18n::I18n(void) {
62   mb = False;
63 #ifdef    HAVE_SETLOCALE
64   locale = setlocale(LC_ALL, "");
65   if (! locale) {
66     fprintf(stderr, "failed to set locale, reverting to \"C\"\n");
67 #endif // HAVE_SETLOCALE
68     locale = "C";
69 #ifdef    HAVE_SETLOCALE
70   } else {
71     // MB_CUR_MAX returns the size of a char in the current locale
72     if (MB_CUR_MAX > 1)
73       mb = True;
74     // truncate any encoding off the end of the locale
75     char *l = strchr(locale, '@');
76     if (l) *l = '\0';
77     l = strchr(locale, '.');
78     if (l) *l = '\0';
79   }
80
81 #ifdef HAVE_CATOPEN
82   catalog_fd = (nl_catd) -1;
83 #endif
84 #endif // HAVE_SETLOCALE
85
86   catalog_filename = (char *) 0;
87 }
88
89
90 I18n::~I18n(void) {
91   delete [] catalog_filename;
92
93 #if defined(NLS) && defined(HAVE_CATCLOSE)
94   if (catalog_fd != (nl_catd) -1)
95     catclose(catalog_fd);
96 #endif // HAVE_CATCLOSE
97 }
98
99
100 void I18n::openCatalog(const char *catalog) {
101 #if defined(NLS) && defined(HAVE_CATOPEN)
102   int lp = strlen(LOCALEPATH), lc = strlen(locale),
103       ct = strlen(catalog), len = lp + lc + ct + 3;
104   catalog_filename = new char[len];
105
106   strncpy(catalog_filename, LOCALEPATH, lp);
107   *(catalog_filename + lp) = '/';
108   strncpy(catalog_filename + lp + 1, locale, lc);
109   *(catalog_filename + lp + lc + 1) = '/';
110   strncpy(catalog_filename + lp + lc + 2, catalog, ct + 1);
111
112 #  ifdef    MCLoadBySet
113   catalog_fd = catopen(catalog_filename, MCLoadBySet);
114 #  else // !MCLoadBySet
115   catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE);
116 #  endif // MCLoadBySet
117
118   if (catalog_fd == (nl_catd) -1)
119     fprintf(stderr, "failed to open catalog, using default messages\n");
120 #else // !HAVE_CATOPEN
121
122   catalog_filename = (char *) 0;
123 #endif // HAVE_CATOPEN
124 }
125
126
127 const char *I18n::getMessage(int set, int msg, const char *msgString) const {
128 #if   defined(NLS) && defined(HAVE_CATGETS)
129   if (catalog_fd != (nl_catd) -1)
130     return (const char *) catgets(catalog_fd, set, msg, msgString);
131   else
132 #endif
133     return msgString;
134 }