On Sat, 16 Jul 2005 02:17:32 +0300 (EEST) Martin Vermeer <[EMAIL PROTECTED]>
wrote:

> On Fri, 15 Jul 2005 13:05:03 +0200 Georg Baum
> <[EMAIL PROTECTED]>
> wrote:
> 
> > Lars Gullik Bjønnes wrote:
> > 
> > > Martin Vermeer <[EMAIL PROTECTED]> writes:
> > 

...

> > 
> > Georg
> 
> Thanks Georg, this helped us forward.
> 
> Attached a tentative patch mainly by Andreas with moral support from me
> which
> solves the problem for label, ref, index, URL. In order to see the
> error, you
> need to have a locale of type ll_CC.UTF-8. Set LC_CTYPE to this locale.
> 
> Please folks try this in different environments.
> 
> - Martin
> 

Here is the "final" patch to be applied to current CVS, which already contains
the accidentally committed preliminary patch.

With this patch, the locale definitions used by LyX and by Qt should be
identical: taken from LC_ALL, LC_MESSAGES, or LANG, in that order, see
support/filetools.C.

LANGUAGE should not be used; this patch disables it if it is accidentally set.

With this patch, one can have a document language (specified within LyX) that
differs from the message string language. Lars implemented this some time ago
and now it will work irrespective of locale string confusage.

With this patch, the code expects the po files to be 8-bit. it is robust against
users configuring locale strings like de_DE.UTF-8. You shouldn't see messed-up
menu strings (or inset button texts, TOC/navigation menu entries ety.) anymore.
If you see them, that is a bug in this patch ;-)

- Martin

Index: messages.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/messages.C,v
retrieving revision 1.20
diff -u -p -r1.20 messages.C
--- messages.C	16 Jul 2005 15:55:36 -0000	1.20
+++ messages.C	17 Jul 2005 07:40:57 -0000
@@ -12,11 +12,14 @@
 #include "debug.h"
 #include "messages.h"
 #include "support/filetools.h"
+#include "support/environment.h"
 #include "support/package.h"
 
 #include <boost/regex.hpp>
 
 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,32 @@ 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";
+			}
+		}
+		setEnv("LANGUAGE", "");
+		
+		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 +144,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;
 	}
Index: frontends/qt2/QLyXKeySym.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLyXKeySym.C,v
retrieving revision 1.33
diff -u -p -r1.33 QLyXKeySym.C
--- frontends/qt2/QLyXKeySym.C	16 Jul 2005 15:55:36 -0000	1.33
+++ frontends/qt2/QLyXKeySym.C	17 Jul 2005 07:40:57 -0000
@@ -23,6 +23,7 @@
 
 #include <map>
 #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 & encodin
 
 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();

Reply via email to