From 5ff2acd9f32db9898066102e5ad49b60634b87fc Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Tue, 10 May 2011 14:32:14 +0200 Subject: [PATCH] Merge libgettext code into libi2ncommon --- configlib/Makefile.am | 2 +- configure.in | 4 - libi2ncommon.spec | 6 +- src/Makefile.am | 8 +- src/i18n.cpp | 198 +++++++++++++++++++++++++++++++++++++++++++++++++ src/i18n.h | 32 ++++++++ xmllib/Makefile.am | 2 +- 7 files changed, 239 insertions(+), 13 deletions(-) create mode 100644 src/i18n.cpp create mode 100644 src/i18n.h diff --git a/configlib/Makefile.am b/configlib/Makefile.am index 6b4d54f..b24bcc4 100644 --- a/configlib/Makefile.am +++ b/configlib/Makefile.am @@ -1,5 +1,5 @@ # set the include path found by configure -INCLUDES = -I$(top_srcdir)/src $(all_includes) @LIBGETTEXT_CFLAGS@ \ +INCLUDES = -I$(top_srcdir)/src $(all_includes) \ @LIBICONV_CFLAGS@ @BOOST_CPPFLAGS@ METASOURCES = AUTO diff --git a/configure.in b/configure.in index 2091fed..fc5145d 100644 --- a/configure.in +++ b/configure.in @@ -31,10 +31,6 @@ PKG_CHECK_MODULES(LIBXMLPP, libxml++-2.6 >= 0.2.1) AC_SUBST(LIBXMLPP_CFLAGS) AC_SUBST(LIBXMLPP_LIBS) -PKG_CHECK_MODULES(LIBGETTEXT, libgettext >= 0.0.0) -AC_SUBST(LIBGETTEXT_CFLAGS) -AC_SUBST(LIBGETTEXT_LIBS) - PKG_CHECK_MODULES(LIBICONV, libiconv >= 0.0.0) AC_SUBST(LIBICONV_CFLAGS) AC_SUBST(LIBICONV_LIBS) diff --git a/libi2ncommon.spec b/libi2ncommon.spec index 634783f..b6d78db 100644 --- a/libi2ncommon.spec +++ b/libi2ncommon.spec @@ -8,9 +8,9 @@ Vendor: Intra2net AG Source: %{name}-%{version}.tar.gz Buildroot: /tmp/%{name}-%{version}-root Prefix: /usr/intranator -Requires: libgettext libi2ncommon-utils = %{version} +Requires: libi2ncommon-utils = %{version} BuildPrereq: libtool -BuildRequires: boost-devel >= 1.32.0 libgettext +BuildRequires: boost-devel >= 1.32.0 %description @@ -19,7 +19,7 @@ library with functions common in Intra2net programs %package devel Summary: library with functions common in Intra2net programs Group: Intranator/Development -Requires: libi2ncommon = %{version} libgettext +Requires: libi2ncommon = %{version} Requires: boost-devel >= 1.32.0 diff --git a/src/Makefile.am b/src/Makefile.am index 1a7a023..7921476 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ # set the include path found by configure -INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/utils @LIBGETTEXT_CFLAGS@ @LIBICONV_CFLAGS@ @BOOST_CPPFLAGS@ $(all_includes) +INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/utils @LIBICONV_CFLAGS@ @BOOST_CPPFLAGS@ $(all_includes) # the library search path. lib_LTLIBRARIES = libi2ncommon.la @@ -8,12 +8,12 @@ include_HEADERS = cron.hpp daemonfunc.hpp exception.hxx filefunc.hxx \ i2n_configdata.hpp i2n_configfile.hpp insocketstream.hxx ip_type.hxx ipfunc.hxx \ log_macros.hpp logfunc.hpp logread.hxx oftmpstream.hxx pidfile.hpp pipestream.hxx \ pointer_func.hpp source_track_basics.hpp stringfunc.hxx timefunc.hxx tmpfstream.hpp \ - tmpfstream_impl.hpp tracefunc.hpp userfunc.hpp week.hpp tribool.hpp + tmpfstream_impl.hpp tracefunc.hpp userfunc.hpp week.hpp tribool.hpp i18n.h libi2ncommon_la_SOURCES = cron.cpp daemonfunc.cpp filefunc.cpp \ i2n_configfile.cpp ipfunc.cpp logfunc.cpp logread.cpp oftmpstream.cpp pidfile.cpp \ pointer_func.cpp source_track_basics.cpp stringfunc.cpp timefunc.cpp tmpfstream.cpp \ - tracefunc.cpp userfunc.cpp week.cpp tribool.cpp + tracefunc.cpp userfunc.cpp week.cpp tribool.cpp i18n.cpp # Note: If you specify a:b:c as the version in the next line, # the library that is made has version (a-c).c.b. In this @@ -21,4 +21,4 @@ libi2ncommon_la_SOURCES = cron.cpp daemonfunc.cpp filefunc.cpp \ libi2ncommon_la_LDFLAGS = -version-info @LIBI2NCOMMON_LIB_VERSION@ @BOOST_LDFLAGS@ -libi2ncommon_la_LIBADD = @LIBGETTEXT_LIBS@ @LIBICONV_LIBS@ @BOOST_IOSTREAMS_LIB@ +libi2ncommon_la_LIBADD = @LIBICONV_LIBS@ @BOOST_IOSTREAMS_LIB@ diff --git a/src/i18n.cpp b/src/i18n.cpp new file mode 100644 index 0000000..c495464 --- /dev/null +++ b/src/i18n.cpp @@ -0,0 +1,198 @@ +/*************************************************************************** + i18n.cpp - description + ------------------- + begin : Wed Apr 25 2001 + copyright : (C) 2001-2004 by Intra2net AG + email : info@intra2net.com + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include "i18n.h" + +#define DEFAULT_LANG "en_EN" + +using namespace std; + +void i18n_init (const string& domain, const string& path) +{ + i18n_set_language (i18n_get_default_language()); + + bindtextdomain (domain.c_str(), path.c_str()); + textdomain (domain.c_str()); +} + +void i18n_set_language (const string &lang) +{ + string target; + + if (lang.empty()) + target=DEFAULT_LANG; + else + { + if (lang=="NULL") + target=""; + else + target=lang; + } + + if (target.empty()) + setlocale(LC_ALL,"C"); + else + setlocale(LC_ALL,target.c_str()); + + // Flush gcc based gettext caching. Code is from the -GNU- + // gettext manual in the "being a gettext grok" chapter. + { + extern int _nl_msg_cat_cntr; + ++_nl_msg_cat_cntr; + } + + return; +} + +// empty string returned: no language set +string i18n_get_current_language(void) +{ + string lng; + char* env=setlocale(LC_ALL,NULL); + + if (env != NULL) + { + if (strcmp(env,"C")==0) + lng="NULL"; + else + lng=env; + } + else + lng="NULL"; + + return lng; +} + +// empty string returned: no language set +string i18n_get_default_language(void) +{ + string lang; + + ifstream in("/usr/intranator/etc/locale"); + if (in) + { + getline (in, lang); + + if (lang.empty()) + lang=DEFAULT_LANG; + in.close(); + } + + return lang; +} + +void i18n_get_string(const char *source, string &target, const vector &data) { + string src = string (source); + i18n_get_string (src, target, data); +} + +string i18n_get_string(const char *source, const vector &data) { + string target; + string src = string (source); + i18n_get_string (src, target, data); + return target; +} + +string i18n_get_string(const string &source, const vector &data) { + string target; + i18n_get_string (source, target, data); + return target; +} + +void i18n_get_string(const string &source, string &target, const vector &data) +{ + string::size_type p=0, slen=0; + string dollar="$"; + + // custom vars + string text = source; + + target=text; + + while ((p=target.find('$',p))!=string::npos) + { + const string *ins; + + if (target.size() < p) + { + ostringstream os; + os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-"; + target = os.str(); + return; + } + + // find string to insert (=ins) + if (target.at(p+1)=='$') + { + ins=$ + slen=2; + } + else + { + slen=target.find_first_not_of("0123456789",p+1); + + if (slen==string::npos) + slen=target.size(); + + if (slen==p+1) + { + ostringstream os; + os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-"; + target = os.str(); + return; + } + + slen-=p; + istringstream is(target.substr(p+1,slen-1)); + unsigned int dnr; + + is >> dnr; + + if (is.fail()) + { + ostringstream os; + os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-"; + target = os.str(); + return; + } + + if (dnr+1 > data.size()) + { + ostringstream os; + os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-"; + target = os.str(); + return; + } + + ins=&(data.at(dnr)); + } + + target.replace(p,slen,*ins); + p=p+ins->size(); + } +} + +// convert locale to language (de_DE -> de) +string i18n_locale2language(string locale) +{ + // search for "_" + string::size_type pos = locale.find("_"); + if (pos == string::npos) + return locale; + + return(locale.substr(0, pos)); +} diff --git a/src/i18n.h b/src/i18n.h new file mode 100644 index 0000000..073e202 --- /dev/null +++ b/src/i18n.h @@ -0,0 +1,32 @@ +/*************************************************************************** + i18n.h - description + ------------------- + begin : Wed Apr 25 2001 + copyright : (C) 2001-2004 by Intra2net AG + email : info@intra2net.com + ***************************************************************************/ + +#ifndef intranator_i18n_H +#define intranator_i18n_H + +#include +#include +#include + +#define i18n(String) gettext(String) +#define i18n_noop(String) (String) + +void i18n_init(const std::string& domain, const std::string& path="."); +void i18n_set_language(const std::string &lang=""); +std::string i18n_get_current_language(void); +std::string i18n_get_default_language(void); + +void i18n_get_string (const char *source, std::string &target, const std::vector &data); +void i18n_get_string (const std::string &source, std::string &target, const std::vector &data); + +std::string i18n_get_string (const char *source, const std::vector &data); +std::string i18n_get_string (const std::string &source, const std::vector &data); + +std::string i18n_locale2language(const std::string locale); + +#endif diff --git a/xmllib/Makefile.am b/xmllib/Makefile.am index 4c22119..b9aaea8 100644 --- a/xmllib/Makefile.am +++ b/xmllib/Makefile.am @@ -1,5 +1,5 @@ # set the include path found by configure -INCLUDES = -I$(top_srcdir)/src $(all_includes) @LIBXMLPP_CFLAGS@ @LIBGETTEXT_CFLAGS@ +INCLUDES = -I$(top_srcdir)/src $(all_includes) @LIBXMLPP_CFLAGS@ METASOURCES = AUTO lib_LTLIBRARIES = libi2ncommon_xml.la -- 1.7.1