From 0a87632032895a475806585f2dc34ade4212a1cf Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Sat, 31 Jan 2015 12:04:30 +0100 Subject: Clean up FbTk::I18n API Move private stuff into FbTk/I18n.cc. --- src/FbTk/I18n.cc | 177 ++++++++++++++++++++++------------------- src/FbTk/I18n.hh | 43 ++-------- src/main.cc | 2 +- util/fbsetroot.cc | 20 +---- util/fluxbox-update_configs.cc | 2 +- 5 files changed, 108 insertions(+), 136 deletions(-) diff --git a/src/FbTk/I18n.cc b/src/FbTk/I18n.cc index a7ed338..f53e5ed 100644 --- a/src/FbTk/I18n.cc +++ b/src/FbTk/I18n.cc @@ -35,38 +35,96 @@ #include -#ifdef HAVE_CSTDLIB - #include -#else - #include -#endif -#ifdef HAVE_CSTRING - #include -#else - #include -#endif -#ifdef HAVE_CSTDIO - #include -#else - #include -#endif - +#include +#include #include +#ifdef HAVE_LOCALE_H +#include +#endif // HAVE_LOCALE_H + +#ifdef HAVE_NL_TYPES_H +// this is needed for linux libc5 systems +extern "C" { +#include +} +#elif defined(__CYGWIN__) || defined(__EMX__) || defined(__APPLE__) +extern "C" { +typedef int nl_catd; +char *catgets(nl_catd cat, int set_number, int message_number, char *message); +nl_catd catopen(char *name, int flag); +void catclose(nl_catd cat); +} + +#endif // HAVE_NL_TYPES_H + + using std::cerr; using std::endl; using std::string; +namespace { + +const nl_catd INVALID_CATALOG = ((nl_catd)(-1)); +nl_catd s_catalog_fd = INVALID_CATALOG; + +} + + namespace FbTk { -void NLSInit(const char *catalog) { +void I18n::init(const char* catalog) { + static bool init = false; + if (init) { + return; + } + +#if defined(NLS) && defined(HAVE_CATOPEN) + FbStringUtil::init(); - I18n *i18n = I18n::instance(); - i18n->openCatalog(catalog); -} + I18n& i18n = I18n::instance(); + + string filename = LOCALEPATH; + filename += '/'; + filename += i18n.m_locale; + filename += '/'; + filename += catalog; + + if (!FileUtil::isRegularFile(filename.c_str()) && i18n.m_locale != "C" && FbStringUtil::haveUTF8()) { + // try the UTF-8 catalog, this also picks up situations where + // the codeset somehow isn't specified -I18n::I18n():m_multibyte(false), m_utf8_translate(false), m_catalog_fd((nl_catd)(-1)) { + // remove everything after @ + string::size_type index = i18n.m_locale.find('.'); + // erase all characters starting at index + if (index != string::npos) + i18n.m_locale.erase(index); + + i18n.m_locale.append(".UTF-8"); + i18n.m_utf8_translate = true; + + filename = LOCALEPATH; + filename += '/'; + filename += i18n.m_locale; + filename += '/'; + filename += catalog; + } + +#ifdef MCLoadBySet + s_catalog_fd = catopen(filename.c_str(), MCLoadBySet); +#else // !MCLoadBySet + s_catalog_fd = catopen(filename.c_str(), NL_CAT_LOCALE); +#endif // MCLoadBySet + + if (s_catalog_fd == INVALID_CATALOG) { + cerr<<"Warning: Failed to open file("< -#endif // HAVE_LOCALE_H - -#ifdef HAVE_NL_TYPES_H -// this is needed for linux libc5 systems -extern "C" { -#include -} -#elif defined(__CYGWIN__) || defined(__EMX__) || defined(__APPLE__) -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus -typedef int nl_catd; -char *catgets(nl_catd cat, int set_number, int message_number, char *message); -nl_catd catopen(char *name, int flag); -void catclose(nl_catd cat); -#ifdef __cplusplus -} -#endif // __cplusplus - -#endif // HAVE_NL_TYPES_H - // Some defines to help out #ifdef NLS #define _FB_USES_NLS \ - FbTk::I18n &i18n = *FbTk::I18n::instance() + FbTk::I18n &i18n = FbTk::I18n::instance() // ignore the description, it's for helping translators @@ -97,27 +74,23 @@ namespace FbTk { class I18n { public: - static I18n *instance(); + + static void init(const char*); + static I18n& instance(); + const char *getLocale() const { return m_locale.c_str(); } bool multibyte() const { return m_multibyte; } - const nl_catd &getCatalogFd() const { return m_catalog_fd; } - FbString getMessage(int set_number, int message_number, - const char *default_messsage = 0, bool translate_fb = false) const; + const char *default_messsage = 0, bool translate_fb = false) const; - void openCatalog(const char *catalog); private: I18n(); ~I18n(); std::string m_locale; - bool m_multibyte, m_utf8_translate; - nl_catd m_catalog_fd; - - + bool m_multibyte; + bool m_utf8_translate; }; -void NLSInit(const char *); - } // end namespace FbTk #endif // I18N_HH diff --git a/src/main.cc b/src/main.cc index 66a5adb..d1b3046 100644 --- a/src/main.cc +++ b/src/main.cc @@ -158,7 +158,7 @@ void setupSignalHandling() { int main(int argc, char **argv) { - FbTk::NLSInit("fluxbox.cat"); + FbTk::I18n::init("fluxbox.cat"); FluxboxCli::Options opts; int exitcode = opts.parse(argc, argv); diff --git a/util/fbsetroot.cc b/util/fbsetroot.cc index c787619..28ad26e 100644 --- a/util/fbsetroot.cc +++ b/util/fbsetroot.cc @@ -28,21 +28,9 @@ #include -#ifdef HAVE_CSTRING - #include -#else - #include -#endif -#ifdef HAVE_CSTDLIB - #include -#else - #include -#endif -#ifdef HAVE_CSTDIO - #include -#else - #include -#endif +#include +#include +#include #include using std::cout; @@ -382,7 +370,7 @@ int main(int argc, char **argv) { char *display_name = (char *) 0; int i = 1; - FbTk::NLSInit("fluxbox.cat"); + FbTk::I18n::init("fluxbox.cat"); for (; i < argc; i++) { if (!strcmp(argv[i], "-display") || !strcmp(argv[i], "--display")) { diff --git a/util/fluxbox-update_configs.cc b/util/fluxbox-update_configs.cc index 07b7102..a7d3d4a 100644 --- a/util/fluxbox-update_configs.cc +++ b/util/fluxbox-update_configs.cc @@ -565,7 +565,7 @@ int main(int argc, char **argv) { bool check = 0; pid_t fb_pid = 0; - FbTk::NLSInit("fluxbox.cat"); + FbTk::I18n::init("fluxbox.cat"); _FB_USES_NLS; for (; i < argc; i++) { -- cgit v0.11.2