From baa6723a406e2c8b44e1bb815839beba3290ba9c Mon Sep 17 00:00:00 2001 From: Martin Vermeer Date: Sun, 17 Jul 2005 11:58:54 +0000 Subject: [PATCH] Second Locale patch. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10275 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt2/QLyXKeySym.C | 23 ++++++++++++++++---- src/gettext.C | 5 ++++- src/messages.C | 38 +++++++++++++++++++++++++++------- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/frontends/qt2/QLyXKeySym.C b/src/frontends/qt2/QLyXKeySym.C index 430e4c81c7..b64ae1bc37 100644 --- a/src/frontends/qt2/QLyXKeySym.C +++ b/src/frontends/qt2/QLyXKeySym.C @@ -23,6 +23,7 @@ #include #include "support/lstrings.h" +#include "support/environment.h" #include "encoding.h" #include "language.h" @@ -30,6 +31,7 @@ using std::endl; using std::string; using std::map; using lyx::support::contains; +using lyx::support::getEnv; namespace { @@ -70,9 +72,21 @@ char const encode(string const & encoding, QString const & str) void initEncodings() { - const char * c = QTextCodec::locale(); - string s = c; - if (contains(c, "UTF") || contains(c, "utf")) + //const char * c = QTextCodec::locale(); + //string s = c ? c : ""; + // In this order, see support/filetools.C + string s = getEnv("LC_ALL"); + if (s.empty()) { + s = getEnv("LC_MESSAGES"); + if (s.empty()) { + s = getEnv("LANG"); + if (s.empty()) + s = "C"; + } + } + + if (s.find("UTF") != string::npos || s.find("utf") != string::npos) + //if (contains(c, "UTF") || contains(c, "utf")) lyxerr << "Warning: this system's locale uses Unicode." << endl; // strip off any encoding suffix @@ -101,8 +115,9 @@ void initEncodings() // when no document open // use the appropriate encoding for the system language + lyxerr << "Language code:" << s << endl; for (Languages::const_iterator it=languages.begin(); it != languages.end(); ++it) { - lyxerr << it->second.code() << ":" << it->second.encodingStr() << ":" << it->second.encoding() << endl; + //lyxerr << it->second.code() << ":" << it->second.encodingStr() << ":" << it->second.encoding() << endl; if (it->second.code() == s) { s = it->second.encodingStr(); diff --git a/src/gettext.C b/src/gettext.C index 2f08de5038..aae9bc7f5c 100644 --- a/src/gettext.C +++ b/src/gettext.C @@ -13,13 +13,14 @@ #include "gettext.h" #include "messages.h" - +#include "support/environment.h" #ifdef HAVE_LOCALE_H # include #endif using std::string; +using lyx::support::setEnv; namespace { @@ -44,6 +45,8 @@ string const _(string const & str) void locale_init() { + // Disable, as otherwise it overrides everything else incl. the doc language + setEnv("LANGUAGE", ""); # ifdef HAVE_LC_MESSAGES setlocale(LC_MESSAGES, ""); # endif diff --git a/src/messages.C b/src/messages.C index 50b5d9ca75..9c7a8deb59 100644 --- a/src/messages.C +++ b/src/messages.C @@ -12,11 +12,14 @@ #include "debug.h" #include "messages.h" #include "support/filetools.h" +#include "support/environment.h" #include "support/package.h" #include using lyx::support::package; +using lyx::support::getEnv; +using lyx::support::setEnv; using std::string; @@ -85,10 +88,10 @@ public: { if ( lang_.empty() ) lang_ = setlocale(LC_MESSAGES, NULL); - lyxerr << "Messages: language(" << lang_ - // << ") in dir(" << dir - << ")" << std::endl; - + // strip off any encoding suffix, i.e., assume 8-bit po files + string::size_type i = lang_.find("."); + lang_ = lang_.substr(0, i); + lyxerr << "Messages: language(" << lang_ << ")" << std::endl; } ~Pimpl() {} @@ -98,12 +101,31 @@ public: if (m.empty()) return m; - string oldMSG = setlocale(LC_MESSAGES, NULL); - bool works = setlocale(LC_MESSAGES, lang_.c_str()); + //string oldMSG = setlocale(LC_MESSAGES, NULL); + // In this order, see support/filetools.C: + string lang = getEnv("LC_ALL"); + if (lang.empty()) { + lang = getEnv("LC_MESSAGES"); + if (lang.empty()) { + lang = getEnv("LANG"); + if (lang.empty()) + lang = "C"; + } + } + + char const * works = setlocale(LC_MESSAGES, lang_.c_str()); // CTYPE controls what getmessage thinks what encoding the po file uses string oldCTYPE = setlocale(LC_CTYPE, NULL); setlocale(LC_CTYPE, lang_.c_str()); - bindtextdomain(PACKAGE, package().locale_dir().c_str()); + errno = 0; + char const * c = bindtextdomain(PACKAGE, package().locale_dir().c_str()); + int e = errno; + if (e) { + lyxerr << "Error code: " << errno << std::endl; + lyxerr << "Lang, mess: " << lang_ << " " << m << std::endl; + lyxerr << "Directory: " << package().locale_dir() << std::endl; + lyxerr << "Rtn value: " << c << std::endl; + } textdomain(PACKAGE); const char* msg = gettext(m.c_str()); string translated(works ? msg : m); @@ -121,7 +143,7 @@ public: boost::smatch sub; if (regex_match(translated, sub, reg)) translated = sub.str(1); - setlocale(LC_MESSAGES, oldMSG.c_str()); + setlocale(LC_MESSAGES, lang.c_str()); setlocale(LC_CTYPE, oldCTYPE.c_str()); return translated; } -- 2.39.5