]> git.lyx.org Git - features.git/commitdiff
Fix bug #7741: incorrect locale when starting R from LyX?
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 22 Aug 2012 10:32:22 +0000 (12:32 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 22 Aug 2012 10:36:22 +0000 (12:36 +0200)
The goal here is to get rid of the old code that modified variables
LANGUAGE and LC_ALL, therefore creating the problems mentionned
in the ticket.

In the new system, there is no explicit "GUI" message handler, that
needs to be reset at each language change. Instead, getGuiMessages
calls getMessages with the correct parameter. This allows to simplify
greatly the code and to remove a lot of old cruft.

14 files changed:
src/LyX.cpp
src/LyX.h
src/client/Makefile.am
src/client/Messages.cpp [deleted file]
src/client/Messages.h [deleted file]
src/client/client.cpp
src/client/gettext.cpp [deleted file]
src/frontends/qt4/GuiApplication.cpp
src/support/Messages.cpp
src/support/Messages.h
src/support/filetools.cpp
src/support/lstrings.cpp
src/support/lstrings.h
status.20x

index 6f3b2c642b4f18ea890463a2d282eefcb092ff77..be6868ca8d960e853a7a70784d1a886540596531 100644 (file)
@@ -142,10 +142,6 @@ struct LyX::Impl
 {
        Impl() : spell_checker_(0), apple_spell_checker_(0), aspell_checker_(0), enchant_checker_(0), hunspell_checker_(0)
        {
-               // Set the default User Interface language as soon as possible.
-               // The language used will be derived from the environment
-               // variables.
-               messages_["GUI"] = Messages();
        }
 
        ~Impl()
@@ -267,25 +263,6 @@ Messages & LyX::messages(string const & language)
 }
 
 
-void setRcGuiLanguage()
-{
-       LASSERT(singleton_, /**/);
-       if (lyxrc.gui_language == "auto")
-               return;
-       Language const * language = languages.getLanguage(lyxrc.gui_language);
-       if (language) {
-               LYXERR(Debug::LOCALE, "Setting LANGUAGE to " << language->code());
-               if (!setEnv("LANGUAGE", language->code()))
-                       LYXERR(Debug::LOCALE, "\t... failed!");
-       }
-       LYXERR(Debug::LOCALE, "Setting LC_ALL to en_US");
-       if (!setEnv("LC_ALL", "en_US"))
-               LYXERR(Debug::LOCALE, "\t... failed!");
-       Messages::init();
-       singleton_->pimpl_->messages_["GUI"] = Messages();
-}
-
-
 int LyX::exec(int & argc, char * argv[])
 {
        // Minimal setting of locale before parsing command line
@@ -758,9 +735,6 @@ bool LyX::init()
        if (!readRcFile("lyxrc.dist"))
                return false;
 
-       // Set the language defined by the distributor.
-       setRcGuiLanguage();
-
        // Set the PATH correctly.
 #if !defined (USE_POSIX_PACKAGING)
        // Add the directory containing the LyX executable to the path
@@ -801,9 +775,6 @@ bool LyX::init()
        // Read lyxrc.dist again to be able to override viewer auto-detection.
        readRcFile("lyxrc.dist");
 
-       // Set again the language defined by the distributor.
-       setRcGuiLanguage();
-
        system_lyxrc = lyxrc;
        system_formats = formats;
        pimpl_->system_converters_ = pimpl_->converters_;
@@ -819,9 +790,6 @@ bool LyX::init()
        if (!readLanguagesFile("languages"))
                return false;
 
-       // Set the language defined by the user.
-       setRcGuiLanguage();
-
        LYXERR(Debug::INIT, "Reading layouts...");
        // Load the layouts
        LayoutFileList::get().read();
@@ -1378,7 +1346,19 @@ Messages const & getMessages(string const & language)
 Messages const & getGuiMessages()
 {
        LASSERT(singleton_, /**/);
-       return singleton_->pimpl_->messages_["GUI"];
+       // A cache to translate full language name to language code
+       static string last_language = "auto";
+       static string code;
+       if (lyxrc.gui_language != last_language) {
+               if (lyxrc.gui_language == "auto")
+                       code.clear();
+               else {
+                       Language const * l = languages.getLanguage(lyxrc.gui_language);
+                       code = l ? l->code() : string();
+               }
+               last_language = lyxrc.gui_language;
+       }
+       return singleton_->messages(code);
 }
 
 
index 4e66f2c19624ed1ccbfa31b0a691494a710c5c6a..a5729fad645abef097eefcac6b509519425df5bd 100644 (file)
--- a/src/LyX.h
+++ b/src/LyX.h
@@ -156,7 +156,6 @@ private:
        friend CmdDef & theTopLevelCmdDef();
        friend SpellChecker * theSpellChecker();
        friend void setSpellChecker();
-       friend void setRcGuiLanguage();
        friend void emergencyCleanup();
        friend void execBatchCommands();
        friend void lyx_exit(int exit_code);
@@ -169,8 +168,6 @@ void emergencyCleanup();
 /// \p exit_code is 0 by default, if a non zero value is passed,
 /// emergencyCleanup() will be called before exiting.
 void lyx_exit(int exit_code);
-/// Set the language defined by the user.
-void setRcGuiLanguage();
 /// Execute batch commands if available.
 void execBatchCommands();
 
index e68cad20d4a8c64f30540e292b1e6a483cfa974b..f69d63ee6222939699a98ec52b85c4e8605e235e 100644 (file)
@@ -29,9 +29,7 @@ endif
 
 SOURCEFILES = \
   boost.cpp \
-  client.cpp \
-  gettext.cpp \
-  Messages.cpp
+  client.cpp
 
 HEADERFILES = \
   Messages.h
diff --git a/src/client/Messages.cpp b/src/client/Messages.cpp
deleted file mode 100644 (file)
index a470360..0000000
+++ /dev/null
@@ -1,190 +0,0 @@
-/* \file Messages.cpp
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "Messages.h"
-#include "support/debug.h"
-#include "support/filetools.h"
-#include "support/Package.h"
-#include "support/unicode.h"
-
-#include <cerrno>
-#include <ostream>
-
-using namespace std;
-using namespace lyx::support;
-
-namespace lyx {
-
-
-#ifdef ENABLE_NLS
-
-#if 0
-
-#include <locale>
-
-// This version of the Pimpl utilizes the message capability of
-// libstdc++ that is distributed with GNU G++.
-class Messages::Pimpl {
-public:
-       typedef messages<char>::catalog catalog;
-
-       Pimpl(string const & l)
-               : lang_(l),
-                 loc_gl(lang_.c_str()),
-                 mssg_gl(use_facet<messages<char> >(loc_gl))
-       {
-               //lyxerr << "Messages: language(" << l
-               //       << ") in dir(" << dir << ")" << endl;
-
-               string const locale_dir = package().locale_dir().toFilesystemEncoding();
-               cat_gl = mssg_gl.open(PACKAGE, loc_gl, locale_dir.c_str());
-
-       }
-
-       ~Pimpl()
-       {
-               mssg_gl.close(cat_gl);
-       }
-
-       string const get(string const & msg) const
-       {
-               return mssg_gl.get(cat_gl, 0, 0, msg);
-       }
-private:
-       ///
-       string lang_;
-       ///
-       locale loc_gl;
-       ///
-       messages<char> const & mssg_gl;
-       ///
-       catalog cat_gl;
-};
-#else
-
-#ifdef HAVE_LOCALE_H
-#  include <locale.h>
-#endif
-
-#  if HAVE_GETTEXT
-#    include <libintl.h>      // use the header already in the system *EK*
-#  else
-#    include "../../intl/libintl.h"
-#  endif
-
-// This is a more traditional variant.
-class Messages::Pimpl {
-public:
-       Pimpl(string const & l)
-               : lang_(l)
-       {
-               //lyxerr << "Messages: language(" << l
-               //       << ") in dir(" << dir << ")" << endl;
-
-       }
-
-       ~Pimpl() {}
-
-       docstring const get(string const & m) const
-       {
-               if (m.empty())
-                       return from_ascii(m);
-
-               char * o = setlocale(LC_ALL, 0);
-               string old;
-               if (o)
-                       old = o;
-               char * n = setlocale(LC_ALL, lang_.c_str());
-               if (!n)
-                       // If we are unable to honour the request we just
-                       // return what we got in.
-                       return from_ascii(m);
-               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, "Messages::get()" << '\n'
-                               << "Error code: " << errno << '\n'
-                               << "Lang, mess: " << lang_ << " " << m << '\n'
-                               << "Directory : " << package().locale_dir().absFileName() << '\n'
-                               << "Rtn value : " << c);
-               }
-
-               if (!bind_textdomain_codeset(PACKAGE, ucs4_codeset)) {
-                       LYXERR(Debug::DEBUG, "Messages::get()" << '\n'
-                               << "Error code: " << errno << '\n'
-                               << "Codeset   : " << ucs4_codeset << '\n');
-               }
-
-               textdomain(PACKAGE);
-
-               char const * tmp = m.c_str();
-               char const * msg = gettext(tmp);
-               docstring translated;
-               if (!msg) {
-                       lyxerr << "Undefined result from gettext" << endl;
-                       translated = from_ascii(tmp);
-               } else if (msg == tmp) {
-                       //lyxerr << "Same as entered returned" << endl;
-                       translated = from_ascii(tmp);
-               } else {
-                       LYXERR(Debug::DEBUG, "We got a translation");
-                       char_type const * ucs4 = reinterpret_cast<char_type const *>(msg);
-                       translated = ucs4;
-               }
-               setlocale(LC_ALL, old.c_str());
-               return translated;
-       }
-private:
-       ///
-       string lang_;
-};
-#endif
-
-#else // ENABLE_NLS
-// This is the dummy variant.
-class Messages::Pimpl {
-public:
-       Pimpl(string const &) {}
-
-       ~Pimpl() {}
-
-       docstring const get(string const & m) const
-       {
-               return from_ascii(m);
-       }
-};
-#endif
-
-
-Messages::Messages()
-       : pimpl_(new Pimpl(""))
-{}
-
-
-Messages::Messages(string const & l)
-       : pimpl_(new Pimpl(l))
-{}
-
-
-// We need this for the sake of scoped_ptr
-Messages::~Messages()
-{}
-
-
-docstring const Messages::get(string const & msg) const
-{
-       return pimpl_->get(msg);
-}
-
-
-} // namespace lyx
diff --git a/src/client/Messages.h b/src/client/Messages.h
deleted file mode 100644 (file)
index 5832f91..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-// -*- C++ -*-
-/* \file Messages.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#ifndef MESSAGES_H
-#define MESSAGES_H
-
-#include "support/docstring.h"
-
-#include <boost/scoped_ptr.hpp>
-
-
-namespace lyx {
-
-///
-class Messages {
-public:
-       ///
-       Messages();
-       ///
-       Messages(std::string const & l);
-       ///
-       ~Messages();
-       ///
-       docstring const get(std::string const & msg) const;
-private:
-       class Pimpl;
-       ::boost::scoped_ptr<Pimpl> pimpl_;
-};
-
-
-} // namespace lyx
-
-#endif
index 538f75d48e66d3606c853471ec50ddccc96ec69e..b18d5e58618d573545e9e7cdf23a0c9019b4bc44 100644 (file)
@@ -16,6 +16,7 @@
 #include "support/FileName.h"
 #include "support/FileNameList.h"
 #include "support/lstrings.h"
+#include "support/Messages.h"
 #include "support/unicode.h"
 
 #include <boost/scoped_ptr.hpp>
@@ -74,6 +75,21 @@ struct LyXRC {
 void lyx_exit(int)
 {}
 
+// Dummy language support
+Messages const & getGuiMessages()
+{
+       static Messages lyx_messages;
+
+       return lyx_messages;
+}
+
+
+Messages const & getMessages(string const &)
+{
+       return getGuiMessages();
+}
+
+
 namespace support {
 
 string itoa(unsigned int i)
diff --git a/src/client/gettext.cpp b/src/client/gettext.cpp
deleted file mode 100644 (file)
index d749602..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * \file src/gettext.cpp
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Lars Gullik Bjønnes
- * \author Jean-Marc Lasgouttes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "support/gettext.h"
-#include "Messages.h"
-
-using namespace std;
-
-namespace lyx {
-
-
-#ifdef HAVE_LOCALE_H
-#  include <locale.h>
-#endif
-
-
-namespace {
-
-Messages const & getLyXMessages()
-{
-       static Messages lyx_messages;
-
-       return lyx_messages;
-}
-
-} // anon namespace
-
-
-docstring const _(string const & str)
-{
-       return getLyXMessages().get(str);
-}
-
-
-#ifdef ENABLE_NLS
-
-void locale_init()
-{
-#  ifdef HAVE_LC_MESSAGES
-       setlocale(LC_MESSAGES, "");
-#  endif
-       setlocale(LC_CTYPE, "");
-       setlocale(LC_NUMERIC, "C");
-}
-
-#else // ENABLE_NLS
-
-void locale_init()
-{
-       setlocale(LC_NUMERIC, "C");
-}
-
-#endif
-
-
-} // namespace lyx
index cdf532c29ab35ad275b3ad0273922678686ba5bc..d82d0152ca0dc13224ec22716f261447e427a729 100644 (file)
@@ -1959,10 +1959,7 @@ void GuiApplication::exit(int status)
 
 void GuiApplication::setGuiLanguage()
 {
-       // Set the language defined by the user.
-       setRcGuiLanguage();
-
-       QString const default_language = toqstr(Messages::defaultLanguage());
+       QString const default_language = toqstr(getGuiMessages().language());
        LYXERR(Debug::LOCALE, "Trying to set default locale to: " << default_language);
        QLocale const default_locale(default_language);
        QLocale::setDefault(default_locale);
index 17ba0c56961db70f7698496fdd7012330c3a42c1..5056952b04a1c292aed61c36c4e56b57f3b6be78 100644 (file)
 
 #include <cerrno>
 
+#  define N_(str) (str)              // for marking strings to be translated
+
 using namespace std;
 
 namespace lyx {
 
-// Instanciate static member.
-string Messages::main_lang_;
-
 namespace {
 
 void cleanTranslation(docstring & trans) 
@@ -71,22 +70,6 @@ using namespace lyx::support;
 
 namespace lyx {
 
-void Messages::setDefaultLanguage()
-{
-       char const * env_lang[5] = {"LANGUAGE", "LC_ALL", "LC_MESSAGES",
-               "LC_MESSAGE", "LANG"};
-       for (size_t i = 0; i != 5; ++i) {
-               string const lang = getEnv(env_lang[i]);
-               if (lang.empty())
-                       continue;
-               Messages::main_lang_ = lang;
-               return;
-       }
-       // Not found!
-       LYXERR(Debug::LOCALE, "Default language not found!");
-}
-
-
 // This version use the traditional gettext.
 Messages::Messages(string const & l)
        : lang_(l), warned_(false)
@@ -116,17 +99,25 @@ void Messages::init()
        }
 
        textdomain(PACKAGE);
+}
+
 
-       // Reset default language;
-       setDefaultLanguage();
+string Messages::language() const
+{
+       // get the language from the gmo file
+       string const test = N_("[[Replace with the code of your language]]");
+       string const trans = to_utf8(get(test));
+       if (trans == test) {
+               LYXERR0("Something is weird.");
+               return string();
+       } else
+               return trans;
 }
 
 
 bool Messages::available() const
 {
-       string const test = languageTestString();
-       string const trans = to_utf8(get(test));
-       return !trans.empty() && trans != test;
+       return !language().empty();
 }
 
 
index 3b3dce8304be7d29a84dc1382e75bd2feea53489..60ce07f9c82e968c469adff1c0d489c496cc0e54 100644 (file)
@@ -26,18 +26,14 @@ public:
        Messages(std::string const & l = std::string());
        ///
        docstring const get(std::string const & msg) const;
+       /// What is the language associated with this translation?
+       std::string language() const;
        /// Is an (at least partial) translation of this language available?
        bool available() const;
        ///
        static void init();
-       ///
-       static std::string const & defaultLanguage() { return main_lang_; }
 
 private:
-       ///
-       static void setDefaultLanguage();
-       ///
-       static std::string main_lang_;
        ///
        std::string lang_;
        /// Did we warn about unavailable locale already?
index 10a9d8dbc51dceacc6a911b6a6ead395d9891fb9..5b44d039720dcc8e511ff8cadf539bf3fcb3bd0d 100644 (file)
@@ -30,6 +30,7 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/os.h"
+#include "support/Messages.h"
 #include "support/Package.h"
 #include "support/Path.h"
 #include "support/Systemcall.h"
@@ -271,7 +272,7 @@ FileName const i18nLibFileSearch(string const & dir, string const & name,
           each po file is able to tell us its name. (JMarc)
        */
 
-       string lang = to_ascii(_(languageTestString()));
+       string lang = getGuiMessages().language();
        string const language = getEnv("LANGUAGE");
        if (!lang.empty() && !language.empty())
                lang = language;
index 7fe06a8d44764ff69f6335650d55b48c53b31142..1066077360a0705de8b554a74ca5aa966050f960 100644 (file)
@@ -1295,12 +1295,6 @@ int findToken(char const * const str[], string const & search_token)
 }
 
 
-string const languageTestString()
-{
-       return N_("[[Replace with the code of your language]]");
-}
-
-
 template<>
 docstring bformat(docstring const & fmt, int arg1)
 {
index d0f74803658fdadeafb6161ebcf6b40516a1be14..52b6ee35969f79630ae9f3bea166eae8f8d3b423 100644 (file)
@@ -285,9 +285,6 @@ docstring const getStringFromVector(std::vector<docstring> const & vec,
 /// found, else -1. The last item in \p str must be "".
 int findToken(char const * const str[], std::string const & search_token);
 
-/// A test string that is supposed to be translated into the gettext code
-std::string const languageTestString();
-
 template <class Arg1>
 docstring bformat(docstring const & fmt, Arg1);
 
index f3725817cde7e02f605566a9fc97227f9bf911f0..b4efefbe5fe2476c479ec81ec912cc58855199ae 100644 (file)
@@ -135,6 +135,9 @@ What's new
 
 - Fix display of file names that contain many non-ascii characters.
 
+- When using a non-default language for the GUI, do not change locale
+  settings of child proceses (bug 7741).
+
 
 * DOCUMENTATION AND LOCALIZATION