]> git.lyx.org Git - features.git/commitdiff
Tracking correctly available translations (take 2)
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 23 Aug 2012 13:08:21 +0000 (15:08 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Wed, 12 Sep 2012 08:20:05 +0000 (10:20 +0200)
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.

src/Language.cpp
src/Language.h
src/frontends/qt4/GuiPrefs.cpp
src/support/Messages.cpp
src/support/Messages.h

index 8f984325d8448e4c252e2a0efde5f0e8f90ba79b..f02d19611c18cd7d6b78c215653dcda7245d976c 100644 (file)
@@ -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;
 }
 
index 537c7f76fa0efdcc2739999e38ffb2ea8e069edd..46e667d81f427b61e5c03361ae9de7615c084aa6 100644 (file)
@@ -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_;
 };
 
index 1413ada3bd4aa85be3534bbef74cbae2e14b515c..fe69c3cb2dd87f0e1a11e9b7460930134fd87133 100644 (file)
@@ -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(),
index 5056952b04a1c292aed61c36c4e56b57f3b6be78..085cf9ed8da7fddb77d694a3fb6a4965e680592b 100644 (file)
@@ -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;
+
 }
 
 
index 60ce07f9c82e968c469adff1c0d489c496cc0e54..905532d4b09911962607063d84de1d45f7f92b7f 100644 (file)
@@ -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();