aboutsummaryrefslogtreecommitdiff
path: root/src/i18n.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/i18n.cc')
-rw-r--r--src/i18n.cc138
1 files changed, 74 insertions, 64 deletions
diff --git a/src/i18n.cc b/src/i18n.cc
index 5f102dd..7fb0cc2 100644
--- a/src/i18n.cc
+++ b/src/i18n.cc
@@ -1,3 +1,6 @@
1// i18n.hh for Fluxbox Window Manager
2// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
1// i18n.cc for Blackbox - an X11 Window manager 4// i18n.cc for Blackbox - an X11 Window manager
2// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) 5// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
3// 6//
@@ -19,67 +22,76 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 23// DEALINGS IN THE SOFTWARE.
21 24
22// stupid macros needed to access some functions in version 2 of the GNU C 25// $Id: i18n.cc,v 1.6 2002/08/13 23:54:41 fluxgen Exp $
23// library 26
24#ifndef _GNU_SOURCE 27//usr GNU extensions
25#define _GNU_SOURCE 28#ifndef _GNU_SOURCE
29#define _GNU_SOURCE
26#endif // _GNU_SOURCE 30#endif // _GNU_SOURCE
27 31
28#ifdef HAVE_CONFIG_H 32#ifdef HAVE_CONFIG_H
29# include "../config.h" 33#include "../config.h"
30#endif // HAVE_CONFIG_H 34#endif // HAVE_CONFIG_H
31 35
32#include "i18n.hh" 36#include "i18n.hh"
33 37
34#include <X11/Xlocale.h> 38#include <X11/Xlocale.h>
35 39
36#ifdef STDC_HEADERS 40#ifdef STDC_HEADERS
37# include <stdlib.h> 41# include <stdlib.h>
38# include <string.h> 42# include <string.h>
39# include <stdio.h> 43# include <stdio.h>
40#endif // STDC_HEADERS 44#endif // STDC_HEADERS
41 45
42#ifdef HAVE_LOCALE_H 46#ifdef HAVE_LOCALE_H
43# include <locale.h> 47# include <locale.h>
44#endif // HAVE_LOCALE_H 48#endif // HAVE_LOCALE_H
45 49
50#include <iostream>
51using std::cerr;
52using std::endl;
53using std::string;
46 54
47void NLSInit(const char *catalog) { 55void NLSInit(const char *catalog) {
48 I18n *i18n = I18n::instance(); 56 I18n *i18n = I18n::instance();
49 i18n->openCatalog(catalog); 57 i18n->openCatalog(catalog);
50} 58}
51 59
52 60
53I18n::I18n(void) { 61I18n::I18n():m_multibyte(false), m_catalog_fd((nl_catd)(-1)) {
54#ifdef HAVE_SETLOCALE 62#ifdef HAVE_SETLOCALE
55 locale = setlocale(LC_ALL, ""); 63 //make sure we don't get 0 to m_locale string
56 if (! locale) { 64 char *temp = setlocale(LC_ALL, "");
57 fprintf(stderr, "failed to set locale, reverting to \"C\"\n"); 65 m_locale = ( temp ? temp : "");
66 if (m_locale.size() == 0) {
67 cerr<<"Warning: Failed to set locale, reverting to \"C\""<<endl;
58#endif // HAVE_SETLOCALE 68#endif // HAVE_SETLOCALE
59 locale = "C"; 69 m_locale = "C";
60 mb = 0; 70#ifdef HAVE_SETLOCALE
61#ifdef HAVE_SETLOCALE 71 } else {
62 } else if (! strcmp(locale, "C") || ! strcmp(locale, "POSIX")) { 72 // MB_CUR_MAX returns the size of a char in the current locale
63 mb = 0; 73 if (MB_CUR_MAX > 1)
64 } else { 74 m_multibyte = true;
65 mb = 1; 75
66 // truncate any encoding off the end of the locale 76 // truncate any encoding off the end of the locale
67 char *l = strchr(locale, '.'); 77
68 if (l) *l = '\0'; 78 string::size_type index = m_locale.find('@');
69 } 79 if (index != string::npos)
80 m_locale.erase(index); //erase all characters starting at index
81
82 index = m_locale.find('.');
83 if (index != string::npos)
84 m_locale.erase(index); //erase all characters starting at index
85 }
70#endif // HAVE_SETLOCALE 86#endif // HAVE_SETLOCALE
71
72 catalog_filename = (char *) 0;
73 catalog_fd = (nl_catd) -1;
74} 87}
75 88
76 89
77I18n::~I18n(void) { 90I18n::~I18n() {
78 delete catalog_filename;
79 91
80#if defined(NLS) && defined(HAVE_CATCLOSE) 92#if defined(NLS) && defined(HAVE_CATCLOSE)
81 if (catalog_fd != (nl_catd) -1) 93 if (m_catalog_fd != (nl_catd)-1)
82 catclose(catalog_fd); 94 catclose(m_catalog_fd);
83#endif // HAVE_CATCLOSE 95#endif // HAVE_CATCLOSE
84} 96}
85 97
@@ -90,37 +102,35 @@ I18n *I18n::instance() {
90 102
91void I18n::openCatalog(const char *catalog) { 103void I18n::openCatalog(const char *catalog) {
92#if defined(NLS) && defined(HAVE_CATOPEN) 104#if defined(NLS) && defined(HAVE_CATOPEN)
93 int lp = strlen(LOCALEPATH), lc = strlen(locale), 105
94 ct = strlen(catalog), len = lp + lc + ct + 3; 106 string catalog_filename = LOCALEPATH;
95 catalog_filename = new char[len]; 107 catalog_filename += '/';
96 108 catalog_filename += m_locale;
97 strncpy(catalog_filename, LOCALEPATH, lp); 109 catalog_filename += '/';
98 *(catalog_filename + lp) = '/'; 110 catalog_filename += catalog;
99 strncpy(catalog_filename + lp + 1, locale, lc); 111
100 *(catalog_filename + lp + lc + 1) = '/'; 112#ifdef MCLoadBySet
101 strncpy(catalog_filename + lp + lc + 2, catalog, ct + 1); 113 m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet);
102 114#else // !MCLoadBySet
103# ifdef MCLoadBySet 115 m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE);
104 catalog_fd = catopen(catalog_filename, MCLoadBySet); 116#endif // MCLoadBySet
105# else // !MCLoadBySet 117
106 catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE); 118 if (m_catalog_fd == (nl_catd)-1)
107# endif // MCLoadBySet 119 cerr<<"Warning: Failed to open catalog, using default messages."<<endl;
108
109 if (catalog_fd == (nl_catd) -1)
110 fprintf(stderr, "failed to open catalog, using default messages\n");
111 120
112#else // !HAVE_CATOPEN 121#else // !HAVE_CATOPEN
113 catalog_fd = (nl_catd) -1; 122
114 catalog_filename = (char *) 0; 123 m_catalog_fd = (nl_catd)-1;
115#endif // HAVE_CATOPEN 124#endif // HAVE_CATOPEN
116} 125}
117 126
118 127
119const char *I18n::getMessage(int set, int msg, const char *s) { 128const char *I18n::getMessage(int set_number, int message_number, const char *default_message) {
120#if defined(NLS) && defined(HAVE_CATGETS) 129
121 if (catalog_fd != (nl_catd) -1) 130#if defined(NLS) && defined(HAVE_CATGETS)
122 return (const char *) catgets(catalog_fd, set, msg, s); 131 if (m_catalog_fd != (nl_catd)-1)
123 else 132 return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message);
124#endif 133 else
125 return s; 134#endif // NLS && HAVE_CATGETS
135 return default_message;
126} 136}