2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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.
23 // stupid macros needed to access some functions in version 2 of the GNU C
30 # include "../config.h"
31 #endif // HAVE_CONFIG_H
35 #include <X11/Xlocale.h>
39 #endif // HAVE_STDLIB_H
43 #endif // HAVE_STRING_H
47 #endif // HAVE_STDIO_H
51 #endif // HAVE_LOCALE_H
53 // the rest of bb source uses True and False from X, so we continue that
57 static I18n static_i18n;
60 void NLSInit(const char *catalog) {
63 i18n->openCatalog(catalog);
70 locale = setlocale(LC_ALL, "");
72 fprintf(stderr, "failed to set locale, reverting to \"C\"\n");
73 #endif // HAVE_SETLOCALE
77 // MB_CUR_MAX returns the size of a char in the current locale
80 // truncate any encoding off the end of the locale
81 char *l = strchr(locale, '@');
83 l = strchr(locale, '.');
88 catalog_fd = (nl_catd) -1;
90 #endif // HAVE_SETLOCALE
92 catalog_filename = (char *) 0;
97 delete [] catalog_filename;
99 #if defined(NLS) && defined(HAVE_CATCLOSE)
100 if (catalog_fd != (nl_catd) -1)
101 catclose(catalog_fd);
102 #endif // HAVE_CATCLOSE
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];
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);
119 catalog_fd = catopen(catalog_filename, MCLoadBySet);
120 # else // !MCLoadBySet
121 catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE);
122 # endif // MCLoadBySet
124 if (catalog_fd == (nl_catd) -1)
125 fprintf(stderr, "failed to open catalog, using default messages\n");
126 #else // !HAVE_CATOPEN
128 catalog_filename = (char *) 0;
129 #endif // HAVE_CATOPEN
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);