]> icculus.org git repositories - mikachu/openbox.git/blob - src/i18n.cc
make sure that the only time Click-to-place kicks in is when initially placing/mappin...
[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    HAVE_STDLIB_H
38 #  include <stdlib.h>
39 #endif // HAVE_STDLIB_H
40
41 #ifdef    HAVE_STRING_H
42 #  include <string.h>
43 #endif // HAVE_STRING_H
44
45 #ifdef    HAVE_STDIO_H
46 #  include <stdio.h>
47 #endif // HAVE_STDIO_H
48
49 #ifdef    HAVE_LOCALE_H
50 #  include <locale.h>
51 #endif // HAVE_LOCALE_H
52
53 // the rest of bb source uses True and False from X, so we continue that
54 #define True true
55 #define False false
56
57 static I18n static_i18n;
58 I18n *i18n;
59
60 void NLSInit(const char *catalog) {
61   i18n = &static_i18n;
62
63   i18n->openCatalog(catalog);
64 }
65
66
67 I18n::I18n(void) {
68   mb = False;
69 #ifdef    HAVE_SETLOCALE
70   locale = setlocale(LC_ALL, "");
71   if (! locale) {
72     fprintf(stderr, "failed to set locale, reverting to \"C\"\n");
73 #endif // HAVE_SETLOCALE
74     locale = "C";
75 #ifdef    HAVE_SETLOCALE
76   } else {
77     // MB_CUR_MAX returns the size of a char in the current locale
78     if (MB_CUR_MAX > 1)
79       mb = True;
80     // truncate any encoding off the end of the locale
81     char *l = strchr(locale, '@');
82     if (l) *l = '\0';
83     l = strchr(locale, '.');
84     if (l) *l = '\0';
85   }
86
87 #ifdef HAVE_CATOPEN
88   catalog_fd = (nl_catd) -1;
89 #endif
90 #endif // HAVE_SETLOCALE
91
92   catalog_filename = (char *) 0;
93 }
94
95
96 I18n::~I18n(void) {
97   delete [] catalog_filename;
98
99 #if defined(NLS) && defined(HAVE_CATCLOSE)
100   if (catalog_fd != (nl_catd) -1)
101     catclose(catalog_fd);
102 #endif // HAVE_CATCLOSE
103 }
104
105
106 void I18n::openCatalog(const char *catalog) {
107 #if defined(NLS) && defined(HAVE_CATOPEN)
108   int lp = strlen(LOCALEPATH), lc = strlen(locale),
109       ct = strlen(catalog), len = lp + lc + ct + 3;
110   catalog_filename = new char[len];
111
112   strncpy(catalog_filename, LOCALEPATH, lp);
113   *(catalog_filename + lp) = '/';
114   strncpy(catalog_filename + lp + 1, locale, lc);
115   *(catalog_filename + lp + lc + 1) = '/';
116   strncpy(catalog_filename + lp + lc + 2, catalog, ct + 1);
117
118 #  ifdef    MCLoadBySet
119   catalog_fd = catopen(catalog_filename, MCLoadBySet);
120 #  else // !MCLoadBySet
121   catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE);
122 #  endif // MCLoadBySet
123
124   if (catalog_fd == (nl_catd) -1)
125     fprintf(stderr, "failed to open catalog, using default messages\n");
126 #else // !HAVE_CATOPEN
127
128   catalog_filename = (char *) 0;
129 #endif // HAVE_CATOPEN
130 }
131
132
133 const char *I18n::getMessage(int set, int msg, const char *msgString) const {
134 #if   defined(NLS) && defined(HAVE_CATGETS)
135   if (catalog_fd != (nl_catd) -1)
136     return (const char *) catgets(catalog_fd, set, msg, msgString);
137   else
138 #endif
139     return msgString;
140 }