]> git.lyx.org Git - features.git/commitdiff
Some locale related cleanup. JMarc, please when you come back, review this. It is...
authorAbdelrazak Younes <younes@lyx.org>
Mon, 28 Jul 2008 11:26:46 +0000 (11:26 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Mon, 28 Jul 2008 11:26:46 +0000 (11:26 +0000)
* GuiApplication: delay locale loading until setGuiLanguage() is called.

* Messages: memorize default locale for current environment.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25930 a592a061-630c-0410-9148-cb99ea01b6c8

src/LyX.cpp
src/frontends/qt4/GuiApplication.cpp
src/frontends/qt4/GuiApplication.h
src/support/Messages.cpp
src/support/Messages.h

index 35ccf17923c97ff33f77d9334aea2165fd44ef0a..1c0609331c64129eab055dcbae0e82807f8cc628 100644 (file)
@@ -245,6 +245,7 @@ void setRcGuiLanguage()
        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();
 }
 
index ac550abaa6d6d6d5826615bb4f4288616e6d4acc..1248c08be12becbcc7b08ef5d059284b36ba979b 100644 (file)
@@ -54,6 +54,7 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/lyxalgo.h" // sorted
+#include "support/Messages.h"
 #include "support/os.h"
 #include "support/Package.h"
 
@@ -661,6 +662,9 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        QCoreApplication::setOrganizationDomain("lyx.org");
        QCoreApplication::setApplicationName(app_name + "-" + lyx_version);
 
+       // Install translator for GUI elements.
+       installTranslator(&d->qt_trans_);
+
        // FIXME: quitOnLastWindowClosed is true by default. We should have a
        // lyxrc setting for this in order to let the application stay resident.
        // But then we need some kind of dock icon, at least on Windows.
@@ -672,6 +676,11 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        // FIXME: Do we need a lyxrc setting for this on Mac? This behaviour
        // seems to be the default case for applications like LyX.
        setQuitOnLastWindowClosed(false);
+
+       // This allows to translate the strings that appear in the LyX menu.
+       /// A translator suitable for the entries in the LyX menu.
+       /// Only needed with Qt/Mac.
+       installTranslator(new MenuTranslator(this));
 #endif
        
 #ifdef Q_WS_X11
@@ -682,31 +691,6 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
        QApplication::setDoubleClickInterval(300);
 #endif
 
-       // install translation file for Qt built-in dialogs
-       QString language_name = QString("qt_") + QLocale::system().name();
-       
-       // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN). 
-       // Short-named translator can be loaded from a long name, but not the
-       // opposite. Therefore, long name should be used without truncation.
-       // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
-       if (d->qt_trans_.load(language_name,
-               QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
-       {
-               installTranslator(&d->qt_trans_);
-               // even if the language calls for RtL, don't do that
-               setLayoutDirection(Qt::LeftToRight);
-               LYXERR(Debug::LOCALE, "Successfully installed Qt translations for locale "
-                       << language_name);
-       } else
-               LYXERR(Debug::LOCALE, "Could not find  Qt translations for locale "
-                       << language_name);
-
-#ifdef Q_WS_MACX
-       // This allows to translate the strings that appear in the LyX menu.
-       /// A translator suitable for the entries in the LyX menu.
-       /// Only needed with Qt/Mac.
-       installTranslator(new MenuTranslator(this));
-#endif
        connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onLastWindowClosed()));
 
        using namespace lyx::graphics;
@@ -904,7 +888,7 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
 void GuiApplication::resetGui()
 {
        // Set the language defined by the user.
-       setRcGuiLanguage();
+       setGuiLanguage();
 
        // Read menus
        if (!readUIFile(toqstr(lyxrc.ui_file)))
@@ -1039,11 +1023,50 @@ void GuiApplication::exit(int status)
 }
 
 
-void GuiApplication::execBatchCommands()
+void GuiApplication::setGuiLanguage()
 {
        // Set the language defined by the user.
        setRcGuiLanguage();
 
+       QString const default_language = toqstr(Messages::defaultLanguage());
+       LYXERR(Debug::LOCALE, "Tring to set default locale to: " << default_language);
+       QLocale const default_locale(default_language);
+       QLocale::setDefault(default_locale);
+
+       // install translation file for Qt built-in dialogs
+       QString const language_name = QString("qt_") + default_locale.name();
+
+       // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN). 
+       // Short-named translator can be loaded from a long name, but not the
+       // opposite. Therefore, long name should be used without truncation.
+       // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
+       if (!d->qt_trans_.load(language_name,
+                       QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
+               LYXERR(Debug::LOCALE, "Could not find  Qt translations for locale "
+                       << language_name);
+               return;
+       }
+
+       LYXERR(Debug::LOCALE, "Successfully installed Qt translations for locale "
+               << language_name);
+
+       switch (default_locale.language()) {
+       case QLocale::Arabic :
+       case QLocale::Hebrew :
+       case QLocale::Persian :
+       case QLocale::Urdu :
+        setLayoutDirection(Qt::RightToLeft);
+               break;
+       default:
+        setLayoutDirection(Qt::LeftToRight);
+       }
+}
+
+
+void GuiApplication::execBatchCommands()
+{
+       setGuiLanguage();
+
        // Read menus
        if (!readUIFile(toqstr(lyxrc.ui_file)))
                // Gives some error box here.
index de343b97151e4a48de1d4662cc703584312474cc..aa73e6fc00ea3d0d347c78f2e8dd3ee5ce02433b 100644 (file)
@@ -136,6 +136,8 @@ private:
        bool closeAllViews();
        /// read the given ui (menu/toolbar) file
        bool readUIFile(QString const & name, bool include = false);
+       ///
+       void setGuiLanguage();
 
        /// This LyXView is the one receiving Clipboard and Selection
        /// events
index d2033939875a12a423b0bc2f78a088f03495d056..d1a8d33a48cdba4b33960218682afb177948f27f 100644 (file)
@@ -67,6 +67,26 @@ using namespace lyx::support;
 
 namespace lyx {
 
+void Messages::setDefaultLanguage()
+{
+       char * 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!");
+}
+
+
+// Instanciate static member.
+string Messages::main_lang_;
+
+
 // This version use the traditional gettext.
 Messages::Messages(string const & l)
        : lang_(l), warned_(false)
@@ -96,6 +116,9 @@ void Messages::init()
        }
 
        textdomain(PACKAGE);
+
+       // Reset default language;
+       setDefaultLanguage();
 }
 
 
@@ -110,11 +133,8 @@ docstring const Messages::get(string const & m) const
                return it->second;
 
        // The string was not found, use gettext to generate it
-
-       static string oldLANGUAGE;
        static string oldLC_ALL;
        if (!lang_.empty()) {
-               oldLANGUAGE = getEnv("LANGUAGE");
                oldLC_ALL = getEnv("LC_ALL");
                // This GNU extension overrides any language locale
                // wrt gettext.
@@ -141,7 +161,7 @@ docstring const Messages::get(string const & m) const
        if (!trans_c)
                LYXERR(Debug::LOCALE, "Undefined result from gettext");
        else if (trans_c == m_c) {
-               LYXERR(Debug::LOCALE, "Same as entered returned");
+               //LYXERR(Debug::LOCALE, "Same as entered returned");
                trans = from_ascii(m);
        } else {
                //LYXERR(Debug::LOCALE, "We got a translation");
@@ -155,8 +175,8 @@ docstring const Messages::get(string const & m) const
        if (!lang_.empty()) {
                // Reset everything as it was.
                LYXERR(Debug::LOCALE, "restoring LANGUAGE from " << getEnv("LANGUAGE")
-                       << " to " << oldLANGUAGE);
-               if (!setEnv("LANGUAGE", oldLANGUAGE))
+                       << " to " << main_lang_);
+               if (!setEnv("LANGUAGE", main_lang_))
                        LYXERR(Debug::LOCALE, "\t... failed!");
                LYXERR(Debug::LOCALE, "restoring LC_ALL from " << getEnv("LC_ALL")
                        << " to " << oldLC_ALL);
index cb6af76e924ec3da72cf46d072407057b778ce7b..12a5b40e800d1146f82bda3a3ce406d42e8f6182 100644 (file)
@@ -28,7 +28,14 @@ public:
        docstring const get(std::string const & msg) 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?