Enrico Forestieri <[EMAIL PROTECTED]> writes:

> and it turns out that "Chapter" gets translated when LANGUAGES==en_US.
> So, the problem seems being that LANGUAGES is set to the wrong value in
> some circumstances.

Could you try the following version. I even think that I know how it
works :)

I tested it on linux with various env variable settings. I'll try it
on the mac tonight if nobody beats me to it.

JMarc

svndiff src/gettext.cpp src/support/environment.cpp src/Messages.h src/Messages.cpp

Index: src/gettext.cpp
===================================================================
--- src/gettext.cpp	(révision 19343)
+++ src/gettext.cpp	(copie de travail)
@@ -14,7 +14,6 @@
 #include "gettext.h"
 #include "Messages.h"
 
-#include "support/environment.h"
 #include "support/lstrings.h"
 
 #ifdef HAVE_LOCALE_H
@@ -26,36 +25,24 @@ using std::string;
 
 namespace lyx {
 
-using support::setEnv;
-
-
 docstring const _(string const & str)
 {
 	return getGuiMessages().get(str);
 }
 
 
-#ifdef ENABLE_NLS
-
 void locale_init()
 {
-	// Disable, as otherwise it overrides everything else incl. the doc language
-	setEnv("LANGUAGE", "");
+#if 0
+#ifdef ENABLE_NLS
 #  ifdef HAVE_LC_MESSAGES
 	setlocale(LC_MESSAGES, "");
 #  endif
 	setlocale(LC_CTYPE, "");
+#endif
 	setlocale(LC_NUMERIC, "C");
-}
-
-#else // ENABLE_NLS
-
-void locale_init()
-{
-	setlocale(LC_NUMERIC, "C");
-}
-
 #endif
+}
 
 
 docstring const translateIfPossible(docstring const & name)
Index: src/support/environment.cpp
===================================================================
--- src/support/environment.cpp	(révision 19343)
+++ src/support/environment.cpp	(copie de travail)
@@ -12,6 +12,7 @@
 
 #include <config.h>
 
+#include "debug.h"
 #include "support/environment.h"
 #include "support/os.h"
 
@@ -57,6 +58,7 @@ vector<string> const getEnvPath(string c
 
 bool setEnv(string const & name, string const & value)
 {
+	lyxerr << "Setting "<< name << " to " << value << std::endl;
 	// CHECK Look at and fix this.
 	// f.ex. what about error checking?
 
Index: src/Messages.h
===================================================================
--- src/Messages.h	(révision 19343)
+++ src/Messages.h	(copie de travail)
@@ -23,9 +23,11 @@ class Messages {
 public:
 	/// messages in the language \p l.
 	/// If \p l is empty, the language will be defined by the environment.
-	Messages(std::string const & l = "");
+	Messages(std::string const & l = std::string());
 	///
 	docstring const get(std::string const & msg) const;
+	///
+	static void init();
 private:
 	///
 	std::string lang_;
Index: src/Messages.cpp
===================================================================
--- src/Messages.cpp	(révision 19343)
+++ src/Messages.cpp	(copie de travail)
@@ -56,13 +56,6 @@ using support::setEnv;
 Messages::Messages(string const & l)
 	: lang_(l), warned_(false)
 {
-	if ( lang_.empty() ) {
-		char const * lc_msgs = 0;
-#ifdef HAVE_LC_MESSAGES
-		lc_msgs = setlocale(LC_MESSAGES, NULL);
-#endif
-		lang_ = lc_msgs ? lc_msgs : "";
-	}
 	// strip off any encoding suffix, i.e., assume 8-bit po files
 	string::size_type i = lang_.find(".");
 	lang_ = lang_.substr(0, i);
@@ -71,68 +64,65 @@ Messages::Messages(string const & l)
 }
 
 
-docstring const Messages::get(string const & m) const
+void Messages::init()
 {
-	if (m.empty())
-		return docstring();
-
-	// Look for the translated string in the cache.
-	TranslationCache::iterator it = cache_.find(m);
-	if (it != cache_.end())
-		return it->second;
-	// The string was not found, use gettext to generate it:
-
-	// In this order, see support/filetools.cpp:
-	string lang = getEnv("LC_ALL");
-	if (lang.empty()) {
-		lang = getEnv("LC_MESSAGES");
-		if (lang.empty()) {
-			lang = getEnv("LANG");
-			if (lang.empty())
-				lang = "C";
-		}
-	}
-#ifdef HAVE_LC_MESSAGES
-	char const * lc_msgs = setlocale(LC_MESSAGES, lang_.c_str());
-#endif
-	// setlocale fails (returns NULL) if the corresponding locale
-	// is not installed.
-	// On windows (mingw and cygwin) it always returns NULL.
-	// Since this method gets called for every translatable
-	// buffer string like e.g. "Figure:" we warn only once.
-#if !defined(_WIN32) && !defined(__CYGWIN__)
-	if (!warned_ && !lc_msgs) {
-		warned_ = true;
-		lyxerr << "Locale " << lang_ << " could not be set" << endl;
-	}
-#endif
-	// CTYPE controls what getmessage thinks what encoding the po file uses
-	char const * lc_ctype = setlocale(LC_CTYPE, NULL);
-	string oldCTYPE = lc_ctype ? lc_ctype : "";
-
-	setlocale(LC_CTYPE, lang_.c_str());
 	errno = 0;
 	string const locale_dir = package().locale_dir().toFilesystemEncoding();
 	char const * c = bindtextdomain(PACKAGE, locale_dir.c_str());
 	int e = errno;
 	if (e) {
 		LYXERR(Debug::DEBUG)
-		<< BOOST_CURRENT_FUNCTION << '\n'
+			<< BOOST_CURRENT_FUNCTION << '\n'
 			<< "Error code: " << errno << '\n'
-			<< "Lang, mess: " << lang_ << " " << m << '\n'
 			<< "Directory : " << package().locale_dir().absFilename() << '\n'
 			<< "Rtn value : " << c << endl;
 	}
 
 	if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
 		LYXERR(Debug::DEBUG)
-		<< BOOST_CURRENT_FUNCTION << '\n'
+			<< BOOST_CURRENT_FUNCTION << '\n'
 			<< "Error code: " << errno << '\n'
 			<< "Codeset   : " << ucs4_codeset << '\n'
 			<< endl;
 	}
 
 	textdomain(PACKAGE);
+}
+
+
+docstring const Messages::get(string const & m) const
+{
+	if (m.empty())
+		return docstring();
+
+	// Look for the translated string in the cache.
+	TranslationCache::iterator it = cache_.find(m);
+	if (it != cache_.end())
+		return it->second;
+
+	// The string was not found, use gettext to generate it
+
+	//Reinit the machinery. FIXME: this should only be done once!
+	init();
+
+	string const oldLANGUAGE = getEnv("LANGUAGE");
+	string const oldLC_ALL = getEnv("LC_ALL");
+	if (!lang_.empty()) {
+		// This GNU extension overrides any language locale
+		// wrt gettext.
+		setEnv("LANGUAGE", lang_);
+		// However, setting LANGUAGE does nothing when the
+		// locale is "C". Therefore we set the locale to
+		// something that is believed to exist on most
+		// systems. The idea is that one should be able to
+		// load German documents even without having de_DE
+		// installed.
+		setEnv("LC_ALL", "en_US");
+#ifdef HAVE_LC_MESSAGES
+		setlocale(LC_MESSAGES, "");
+#endif
+	}
+
 	char const * tmp = m.c_str();
 	char const * msg = gettext(tmp);
 	docstring translated;
@@ -158,13 +148,18 @@ docstring const Messages::get(string con
 			translated = from_ascii(tmp);
 	} else {
 		LYXERR(Debug::DEBUG) << "We got a translation" << endl;
-		char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
-		translated = ucs4;
+		// m is actually not a char const * but ucs4 data
+		translated = reinterpret_cast<char_type const *>(msg);
 	}
+
+	if (!lang_.empty()) {
+		// Reset everything as it was.
+		setEnv("LANGUAGE", oldLANGUAGE);
+		setEnv("LC_ALL", oldLC_ALL);
 #ifdef HAVE_LC_MESSAGES
-	setlocale(LC_MESSAGES, lang.c_str());
+		setlocale(LC_MESSAGES, "");
 #endif
-	setlocale(LC_CTYPE, oldCTYPE.c_str());
+	}
 
 	std::pair<TranslationCache::iterator, bool> result =
 		cache_.insert(std::make_pair(m, translated));
@@ -188,6 +183,10 @@ namespace lyx {
 
 Messages::Messages(string const & l) {}
 
+void Messages::init()
+{
+}
+
 
 docstring const Messages::get(string const & m) const
 {

Reply via email to