From 8085efd83a437796b96a94e90fb04a9eb428dd3a Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Mon, 28 Jul 2008 11:26:46 +0000 Subject: [PATCH] Some locale related cleanup. JMarc, please when you come back, review this. It is working fine in all my testings though. * 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 | 1 + src/frontends/qt4/GuiApplication.cpp | 77 ++++++++++++++++++---------- src/frontends/qt4/GuiApplication.h | 2 + src/support/Messages.cpp | 32 +++++++++--- src/support/Messages.h | 7 +++ 5 files changed, 86 insertions(+), 33 deletions(-) diff --git a/src/LyX.cpp b/src/LyX.cpp index 35ccf17923..1c0609331c 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -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(); } diff --git a/src/frontends/qt4/GuiApplication.cpp b/src/frontends/qt4/GuiApplication.cpp index ac550abaa6..1248c08be1 100644 --- a/src/frontends/qt4/GuiApplication.cpp +++ b/src/frontends/qt4/GuiApplication.cpp @@ -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. diff --git a/src/frontends/qt4/GuiApplication.h b/src/frontends/qt4/GuiApplication.h index de343b9715..aa73e6fc00 100644 --- a/src/frontends/qt4/GuiApplication.h +++ b/src/frontends/qt4/GuiApplication.h @@ -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 diff --git a/src/support/Messages.cpp b/src/support/Messages.cpp index d203393987..d1a8d33a48 100644 --- a/src/support/Messages.cpp +++ b/src/support/Messages.cpp @@ -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); diff --git a/src/support/Messages.h b/src/support/Messages.h index cb6af76e92..12a5b40e80 100644 --- a/src/support/Messages.h +++ b/src/support/Messages.h @@ -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? -- 2.39.2