diff options
-rw-r--r-- | src/i18n.cc | 133 | ||||
-rw-r--r-- | src/i18n.hh | 44 |
2 files changed, 86 insertions, 91 deletions
diff --git a/src/i18n.cc b/src/i18n.cc index 96641eb..85c170e 100644 --- a/src/i18n.cc +++ b/src/i18n.cc | |||
@@ -22,69 +22,76 @@ | |||
22 | // 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 |
23 | // DEALINGS IN THE SOFTWARE. | 23 | // DEALINGS IN THE SOFTWARE. |
24 | 24 | ||
25 | // $Id: i18n.cc,v 1.2 2002/01/09 14:11:20 fluxgen Exp $ | 25 | // $Id: i18n.cc,v 1.3 2002/04/04 00:21:48 fluxgen Exp $ |
26 | 26 | ||
27 | // stupid macros needed to access some functions in version 2 of the GNU C | 27 | //usr GNU extensions |
28 | // library | 28 | #ifndef _GNU_SOURCE |
29 | #ifndef _GNU_SOURCE | 29 | #define _GNU_SOURCE |
30 | #define _GNU_SOURCE | ||
31 | #endif // _GNU_SOURCE | 30 | #endif // _GNU_SOURCE |
32 | 31 | ||
33 | #ifdef HAVE_CONFIG_H | 32 | #ifdef HAVE_CONFIG_H |
34 | # include "../config.h" | 33 | #include "../config.h" |
35 | #endif // HAVE_CONFIG_H | 34 | #endif // HAVE_CONFIG_H |
36 | 35 | ||
37 | #include "i18n.hh" | 36 | #include "i18n.hh" |
38 | 37 | ||
39 | #include <X11/Xlocale.h> | 38 | #include <X11/Xlocale.h> |
40 | 39 | ||
41 | #ifdef STDC_HEADERS | 40 | #ifdef STDC_HEADERS |
42 | # include <stdlib.h> | 41 | # include <stdlib.h> |
43 | # include <string.h> | 42 | # include <string.h> |
44 | # include <stdio.h> | 43 | # include <stdio.h> |
45 | #endif // STDC_HEADERS | 44 | #endif // STDC_HEADERS |
46 | 45 | ||
47 | #ifdef HAVE_LOCALE_H | 46 | #ifdef HAVE_LOCALE_H |
48 | # include <locale.h> | 47 | # include <locale.h> |
49 | #endif // HAVE_LOCALE_H | 48 | #endif // HAVE_LOCALE_H |
50 | 49 | ||
50 | #include <iostream> | ||
51 | using std::cerr; | ||
52 | using std::endl; | ||
53 | using std::string; | ||
51 | 54 | ||
52 | void NLSInit(const char *catalog) { | 55 | void NLSInit(const char *catalog) { |
53 | I18n *i18n = I18n::instance(); | 56 | I18n *i18n = I18n::instance(); |
54 | i18n->openCatalog(catalog); | 57 | i18n->openCatalog(catalog); |
55 | } | 58 | } |
56 | 59 | ||
57 | 60 | ||
58 | I18n::I18n(void) { | 61 | I18n::I18n():m_multibyte(false), m_catalog_fd(0) { |
59 | #ifdef HAVE_SETLOCALE | 62 | #ifdef HAVE_SETLOCALE |
60 | locale = setlocale(LC_ALL, ""); | 63 | //make sure we don't get 0 to m_locale string |
61 | if (! locale) { | 64 | char *temp = setlocale(LC_ALL, ""); |
62 | 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; | ||
63 | #endif // HAVE_SETLOCALE | 68 | #endif // HAVE_SETLOCALE |
64 | locale = "C"; | 69 | m_locale = "C"; |
65 | mb = 0; | 70 | #ifdef HAVE_SETLOCALE |
66 | #ifdef HAVE_SETLOCALE | 71 | } else { |
67 | } else if (! strcmp(locale, "C") || ! strcmp(locale, "POSIX")) { | 72 | // MB_CUR_MAX returns the size of a char in the current locale |
68 | mb = 0; | 73 | if (MB_CUR_MAX > 1) |
69 | } else { | 74 | m_multibyte = true; |
70 | mb = 1; | 75 | |
71 | // truncate any encoding off the end of the locale | 76 | // truncate any encoding off the end of the locale |
72 | char *l = strchr(locale, '.'); | 77 | |
73 | if (l) *l = '\0'; | 78 | string::size_type index = m_locale.find('@'); |
74 | } | 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 | } | ||
75 | #endif // HAVE_SETLOCALE | 86 | #endif // HAVE_SETLOCALE |
76 | |||
77 | catalog_filename = (char *) 0; | ||
78 | catalog_fd = (nl_catd) -1; | ||
79 | } | 87 | } |
80 | 88 | ||
81 | 89 | ||
82 | I18n::~I18n(void) { | 90 | I18n::~I18n() { |
83 | delete catalog_filename; | ||
84 | 91 | ||
85 | #if defined(NLS) && defined(HAVE_CATCLOSE) | 92 | #if defined(NLS) && defined(HAVE_CATCLOSE) |
86 | if (catalog_fd != (nl_catd) -1) | 93 | if (m_catalog_fd != (nl_catd)-1) |
87 | catclose(catalog_fd); | 94 | catclose(m_catalog_fd); |
88 | #endif // HAVE_CATCLOSE | 95 | #endif // HAVE_CATCLOSE |
89 | } | 96 | } |
90 | 97 | ||
@@ -95,37 +102,35 @@ I18n *I18n::instance() { | |||
95 | 102 | ||
96 | void I18n::openCatalog(const char *catalog) { | 103 | void I18n::openCatalog(const char *catalog) { |
97 | #if defined(NLS) && defined(HAVE_CATOPEN) | 104 | #if defined(NLS) && defined(HAVE_CATOPEN) |
98 | int lp = strlen(LOCALEPATH), lc = strlen(locale), | 105 | |
99 | ct = strlen(catalog), len = lp + lc + ct + 3; | 106 | string catalog_filename = LOCALEPATH; |
100 | catalog_filename = new char[len]; | 107 | catalog_filename += '/'; |
101 | 108 | catalog_filename += m_locale; | |
102 | strncpy(catalog_filename, LOCALEPATH, lp); | 109 | catalog_filename += '/'; |
103 | *(catalog_filename + lp) = '/'; | 110 | catalog_filename += catalog; |
104 | strncpy(catalog_filename + lp + 1, locale, lc); | 111 | |
105 | *(catalog_filename + lp + lc + 1) = '/'; | 112 | #ifdef MCLoadBySet |
106 | strncpy(catalog_filename + lp + lc + 2, catalog, ct + 1); | 113 | m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet); |
107 | 114 | #else // !MCLoadBySet | |
108 | # ifdef MCLoadBySet | 115 | m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE); |
109 | catalog_fd = catopen(catalog_filename, MCLoadBySet); | 116 | #endif // MCLoadBySet |
110 | # else // !MCLoadBySet | 117 | |
111 | catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE); | 118 | if (m_catalog_fd == (nl_catd)-1) |
112 | # endif // MCLoadBySet | 119 | cerr<<"Warning: Failed to open catalog, using default messages."<<endl; |
113 | |||
114 | if (catalog_fd == (nl_catd) -1) | ||
115 | fprintf(stderr, "failed to open catalog, using default messages\n"); | ||
116 | 120 | ||
117 | #else // !HAVE_CATOPEN | 121 | #else // !HAVE_CATOPEN |
118 | catalog_fd = (nl_catd) -1; | 122 | |
119 | catalog_filename = (char *) 0; | 123 | m_catalog_fd = (nl_catd)-1; |
120 | #endif // HAVE_CATOPEN | 124 | #endif // HAVE_CATOPEN |
121 | } | 125 | } |
122 | 126 | ||
123 | 127 | ||
124 | const char *I18n::getMessage(int set, int msg, const char *s) { | 128 | const char *I18n::getMessage(int set_number, int message_number, const char *default_message) { |
125 | #if defined(NLS) && defined(HAVE_CATGETS) | 129 | |
126 | if (catalog_fd != (nl_catd) -1) | 130 | #if defined(NLS) && defined(HAVE_CATGETS) |
127 | return (const char *) catgets(catalog_fd, set, msg, s); | 131 | if (m_catalog_fd != (nl_catd)-1) |
128 | else | 132 | return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message); |
133 | else | ||
129 | #endif | 134 | #endif |
130 | return s; | 135 | return default_message; |
131 | } | 136 | } |
diff --git a/src/i18n.hh b/src/i18n.hh index dca2cde..05f1339 100644 --- a/src/i18n.hh +++ b/src/i18n.hh | |||
@@ -22,14 +22,12 @@ | |||
22 | // 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 |
23 | // DEALINGS IN THE SOFTWARE. | 23 | // DEALINGS IN THE SOFTWARE. |
24 | 24 | ||
25 | // $Id: i18n.hh,v 1.6 2002/03/18 15:46:42 fluxgen Exp $ | 25 | // $Id: i18n.hh,v 1.7 2002/04/04 00:21:48 fluxgen Exp $ |
26 | 26 | ||
27 | #ifndef I18N_HH | 27 | #ifndef I18N_HH |
28 | #define I18N_HH | 28 | #define I18N_HH |
29 | 29 | ||
30 | #ifdef NLS | 30 | #include "../nls/blackbox-nls.hh" |
31 | # include "../nls/blackbox-nls.hh" | ||
32 | #endif // NLS | ||
33 | 31 | ||
34 | #ifdef HAVE_LOCALE_H | 32 | #ifdef HAVE_LOCALE_H |
35 | # include <locale.h> | 33 | # include <locale.h> |
@@ -46,34 +44,26 @@ extern "C" { | |||
46 | # include "nl_types_cygnus.h" | 44 | # include "nl_types_cygnus.h" |
47 | #endif | 45 | #endif |
48 | 46 | ||
49 | class I18n { | 47 | #include <string> |
50 | private: | ||
51 | char *locale, *catalog_filename; | ||
52 | int mb; | ||
53 | nl_catd catalog_fd; | ||
54 | 48 | ||
55 | 49 | class I18n { | |
56 | protected: | ||
57 | I18n(void); | ||
58 | |||
59 | public: | 50 | public: |
60 | //so old compilators dont complain | ||
61 | ~I18n(void); | ||
62 | |||
63 | static I18n *instance(); | 51 | static I18n *instance(); |
64 | inline const char *getLocale(void) const { return locale; } | 52 | inline const char *getLocale(void) const { return m_locale.c_str(); } |
65 | inline const char *getCatalogFilename(void) const { return catalog_filename; } | 53 | inline bool multibyte(void) const { return m_multibyte; } |
66 | 54 | inline const nl_catd &getCatalogFd(void) const { return m_catalog_fd; } | |
67 | inline const int multibyte(void) const { return mb; } | ||
68 | 55 | ||
69 | inline const nl_catd &getCatalogFd(void) const { return catalog_fd; } | 56 | const char *getMessage(int set_number, int message_number, const char *default_messsage = 0); |
70 | |||
71 | const char *getMessage(int, int, const char * = 0); | ||
72 | void openCatalog(const char *); | 57 | void openCatalog(const char *); |
73 | }; | 58 | private: |
59 | I18n(); | ||
60 | ~I18n(); | ||
61 | std::string m_locale; | ||
62 | bool m_multibyte; | ||
63 | nl_catd m_catalog_fd; | ||
74 | 64 | ||
65 | }; | ||
75 | 66 | ||
76 | //extern I18n *i18n; | 67 | void NLSInit(const char *); |
77 | extern void NLSInit(const char *); | ||
78 | 68 | ||
79 | #endif // __i18n_h | 69 | #endif // I18N_HH |