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