From: Jean-Marc Lasgouttes Date: Thu, 23 Aug 2012 13:08:21 +0000 (+0200) Subject: Tracking correctly available translations (take 2) X-Git-Tag: 2.0.5~68 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=2512e1f085a6ad044d6d0ff68f75390ed1d34c75;p=features.git Tracking correctly available translations (take 2) The previous scheme of loading all possible translations and checking whether the work is a bit too much "brute force" and causes problems on Mac OS X (documents loaded with the wrong language). Now there is an helper static method in Messages class that checks whether a readable .mo file exist for the language. There should be an API in gettext for doing that, but alas it is not possible. As a consequence the method Language::translated() has been removed, along with its cache. --- diff --git a/src/Language.cpp b/src/Language.cpp index 8f984325d8..f02d19611c 100644 --- a/src/Language.cpp +++ b/src/Language.cpp @@ -186,10 +186,6 @@ bool Language::read(Lexer & lex) encoding_ = encodings.fromLyXName("iso8859-1"); LYXERR0("Unknown encoding " << encodingStr_); } - // cache translation status. Calling getMessages() directly in - // PrefLanguage::PrefLanguage() did only work if the gui language - // was set to auto (otherwise all languages would be marked as available). - translated_ = getMessages(code()).available(); return true; } diff --git a/src/Language.h b/src/Language.h index 537c7f76fa..46e667d81f 100644 --- a/src/Language.h +++ b/src/Language.h @@ -31,7 +31,7 @@ class Lexer; class Language { public: /// - Language() : rightToLeft_(false), translated_(false) {} + Language() : rightToLeft_(false) {} /// LyX language name std::string const & lang() const { return lang_; } /// Babel language name @@ -44,8 +44,6 @@ public: std::string const & display() const { return display_; } /// is this a RTL language? bool rightToLeft() const { return rightToLeft_; } - /// Is an (at least partial) translation of this language available? - bool translated() const { return translated_; } /** * Translate a string from the layout files that appears in the output. * It takes the translations from lib/layouttranslations instead of @@ -113,8 +111,6 @@ private: /// bool as_babel_options_; /// - bool translated_; - /// TranslationMap layoutTranslations_; }; diff --git a/src/frontends/qt4/GuiPrefs.cpp b/src/frontends/qt4/GuiPrefs.cpp index 1413ada3bd..fe69c3cb2d 100644 --- a/src/frontends/qt4/GuiPrefs.cpp +++ b/src/frontends/qt4/GuiPrefs.cpp @@ -45,6 +45,7 @@ #include "support/foreach.h" #include "support/gettext.h" #include "support/lstrings.h" +#include "support/Messages.h" #include "support/os.h" #include "support/Package.h" @@ -2288,9 +2289,13 @@ PrefLanguage::PrefLanguage(GuiPreferences * form) // each language code only once string const name = fromqstr(index.data(Qt::UserRole).toString()); Language const * lang = languages.getLanguage(name); + if (!lang) + continue; // never remove the currently selected language - if (lang && name != form->rc().gui_language && name != lyxrc.gui_language) - if (!lang->translated() || added.find(lang->code()) != added.end()) + if (name != form->rc().gui_language + && name != lyxrc.gui_language + && (!Messages::available(lang->code()) + || added.find(lang->code()) != added.end())) continue; added.insert(lang->code()); uiLanguageCO->addItem(index.data(Qt::DisplayRole).toString(), diff --git a/src/support/Messages.cpp b/src/support/Messages.cpp index 5056952b04..085cf9ed8d 100644 --- a/src/support/Messages.cpp +++ b/src/support/Messages.cpp @@ -115,9 +115,22 @@ string Messages::language() const } -bool Messages::available() const +bool Messages::available(string const & c) { - return !language().empty(); + static string locale_dir = package().locale_dir().toFilesystemEncoding(); + string code = c; + // this loops at most twice + while (true) { + string const filen = locale_dir + "/" + code + + "/LC_MESSAGES/"PACKAGE".mo"; + if (FileName(filen).isReadableFile()) + return true; + if (contains(code, '_')) + code = token(code, '_', 0); + else return false; + } + return false; + } diff --git a/src/support/Messages.h b/src/support/Messages.h index 60ce07f9c8..905532d4b0 100644 --- a/src/support/Messages.h +++ b/src/support/Messages.h @@ -28,8 +28,8 @@ public: 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; + /// Is an (at least partial) translation of language with code \p c available? + static bool available(std::string const & c); /// static void init();