]> git.lyx.org Git - features.git/commitdiff
other people's work
authorAndré Pönitz <poenitz@gmx.net>
Sat, 16 Jul 2005 15:55:36 +0000 (15:55 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Sat, 16 Jul 2005 15:55:36 +0000 (15:55 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10248 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt2/QLyXKeySym.C
src/frontends/qt2/lyx_gui.C
src/frontends/qt2/qt_helpers.C
src/language.C
src/language.h
src/messages.C

index 09b1797453e5d04d9e7329705b5ca456d6e29989..430e4c81c79bf43f3694d4f1a8fb57863695ef59 100644 (file)
 #include <qtextcodec.h>
 
 #include <map>
+#include "support/lstrings.h"
+#include "encoding.h"
+#include "language.h"
 
 using std::endl;
 using std::string;
 using std::map;
+using lyx::support::contains;
 
 
 namespace {
@@ -42,18 +46,11 @@ char const encode(string const & encoding, QString const & str)
                if (lyxerr.debugging())
                        lyxerr[Debug::KEY] << "Unrecognised encoding "
                                << encoding << endl;
-               codec = QTextCodec::codecForLocale();
+               codec = encoding_map.find("")->second;
        } else {
                codec = cit->second;
        }
 
-       if (!codec) {
-               if (lyxerr.debugging())
-                       lyxerr[Debug::KEY] << "No codec exists for encoding "
-                               << encoding << endl;
-               codec = QTextCodec::codecForLocale();
-       }
-
        if (lyxerr.debugging())
                lyxerr[Debug::KEY] << "Using codec " << fromqstr(codec->name()) << endl;
 
@@ -73,9 +70,15 @@ char const encode(string const & encoding, QString const & str)
 
 void initEncodings()
 {
-       // when no document open
-       encoding_map[""] = QTextCodec::codecForLocale();
-
+       const char * c = QTextCodec::locale();
+       string s = c;
+       if (contains(c, "UTF") || contains(c, "utf")) 
+               lyxerr << "Warning: this system's locale uses Unicode." << endl;
+
+       // strip off any encoding suffix
+       string::size_type i = s.find(".");
+       s = s.substr(0, i);
+                                                                                      
        encoding_map["iso8859-1"] = QTextCodec::codecForName("ISO 8859-1");
        encoding_map["iso8859-2"] = QTextCodec::codecForName("ISO 8859-2");
        encoding_map["iso8859-3"] = QTextCodec::codecForName("ISO 8859-3");
@@ -95,6 +98,23 @@ void initEncodings()
        encoding_map["pt154"] = 0;
 
        // There are lots more codecs in Qt too ...
+       
+       // when no document open
+       // use the appropriate encoding for the system language
+       for (Languages::const_iterator it=languages.begin(); it != languages.end(); ++it) {
+               lyxerr << it->second.code() << ":" << it->second.encodingStr() << ":" << it->second.encoding() << endl;
+               if (it->second.code() == s)
+                       {
+                               s = it->second.encodingStr();
+                               break;
+                       }
+       }
+       lyxerr << "Setting new locale for Qt:" << s << endl;
+       QTextCodec * defaultCodec = encoding_map[s];
+       encoding_map[""] = defaultCodec;
+
+       QTextCodec::setCodecForCStrings(defaultCodec);
+
 }
 
 
index ba0408dab36c3f57143e16c56c70171f4750288c..d72e5946b66cf061e2f5e83bf29909f59fb95bcc 100644 (file)
@@ -212,8 +212,6 @@ void parse_init(int & argc, char * argv[])
        // needs to be done before reading lyxrc
        lyxrc.dpi = getDPI();
 
-       initEncodings();
-
        LoaderQueue::setPriority(10,100);
 }
 
@@ -224,6 +222,9 @@ void parse_lyxrc()
 
 void start(string const & batch, vector<string> const & files)
 {
+       // this can't be done before because it needs the Languages object
+       initEncodings();
+
        // initial geometry
        unsigned int width = 690;
        unsigned int height = 510;
index 1eadd8e1d449a2a2daf77e554670b1a940b859e9..9dfa68efad3b4c2848897b3472a07aa760504a32 100644 (file)
@@ -116,9 +116,7 @@ void lengthToWidgets(QLineEdit * input, LengthCombo * combo,
 
 QString const toqstr(char const * str)
 {
-       QTextCodec * codec = QTextCodec::codecForLocale();
-
-       return codec->toUnicode(str);
+       return QString::fromAscii(str);
 }
 
 
@@ -142,9 +140,9 @@ QString const qt_(string const & str)
 
 string const fromqstr(QString const & str)
 {
-       QTextCodec const * const codec = QTextCodec::codecForLocale();
-       QCString const tmpstr = codec->fromUnicode(str);
-       return tmpstr.isEmpty() ? string() : string(tmpstr);
+       //return str;
+
+       return str.ascii() ? str.ascii() : "";
 }
 
 
index d19ca03c5b85b0a29e17c08ab304a19a2a02708f..34e08854199483f00fa1aa0e91c99b50a7f7eb12 100644 (file)
@@ -26,16 +26,16 @@ using std::string;
 Languages languages;
 Language const * english_language;
 Language const * default_language;
-Language ignore_lang("ignore", "ignore", "Ignore", false, 0, "ignore", "");
+Language ignore_lang("ignore", "ignore", "Ignore", false, "", 0, "ignore", "");
 Language const * ignore_language = &ignore_lang;
-Language latex_lang("latex", "latex", "Latex", false, 0, "latex", "");
+Language latex_lang("latex", "latex", "Latex", false, "", 0, "latex", "");
 Language const * latex_language = &latex_lang;
 
 
 void Languages::read(string const & filename)
 {
        // We need to set the encoding of latex_lang
-       latex_lang = Language("latex", "latex", "Latex", false,
+       latex_lang = Language("latex", "latex", "Latex", false, "iso8859-1",
                              encodings.getEncoding("iso8859-1"),
                              "latex", "");
 
@@ -76,7 +76,7 @@ void Languages::read(string const & filename)
                }
 
                languagelist[lang] = Language(lang, babel, display, rtl,
-                                             encoding, code, latex_options);
+                                             encoding_str, encoding, code, latex_options);
        }
 
        default_language = getLanguage(lyxrc.default_language);
index 5e75174820de5b1ee5f2600235d467bd80cc264c..66d1d5066446e65fa21e922afcd5f960b67f377d 100644 (file)
@@ -28,10 +28,10 @@ public:
        Language() : rightToLeft_(false) {}
        ///
        Language(std::string const & l, std::string const & b, std::string const & d,
-                bool rtl, Encoding const * e, std::string const & c,
+                bool rtl, std::string const & es, Encoding const * e, std::string const & c,
                 std::string const & o)
                : lang_(l), babel_(b), display_(d), rightToLeft_(rtl),
-                 encoding_(e), code_(c), latex_options_(o)
+                 encodingStr_(es), encoding_(e), code_(c), latex_options_(o)
                {}
        ///
        std::string const & lang() const { return lang_; }
@@ -44,6 +44,8 @@ public:
        ///
        Encoding const * encoding() const { return encoding_; }
        ///
+       std::string const & encodingStr() const { return encodingStr_; }
+       ///
        std::string const & code() const { return code_; }
        ///
        std::string const & latex_options() const { return latex_options_; }
@@ -57,6 +59,8 @@ private:
        ///
        bool rightToLeft_;
        ///
+       std::string encodingStr_;
+       ///
        Encoding const * encoding_;
        ///
        std::string code_;
index d523af76262f059b4da67a05c4cb241d8e0dc51d..50b5d9ca750ec26077051aa3eb67e14dae3b50f0 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <config.h>
 
+#include "debug.h"
 #include "messages.h"
 #include "support/filetools.h"
 #include "support/package.h"
@@ -82,9 +83,12 @@ public:
        Pimpl(string const & l)
                : lang_(l)
        {
-               //lyxerr << "Messages: language(" << l
-               //       << ") in dir(" << dir << ")" << std::endl;
-
+               if ( lang_.empty() )
+                       lang_ = setlocale(LC_MESSAGES, NULL);
+               lyxerr << "Messages: language(" << lang_
+               //       << ") in dir(" << dir 
+                      << ")" << std::endl;
+               
        }
 
        ~Pimpl() {}
@@ -94,11 +98,15 @@ public:
                if (m.empty())
                        return m;
 
-               char * old = strdup(setlocale(LC_ALL, 0));
-               char * n = setlocale(LC_ALL, lang_.c_str());
+               string oldMSG = setlocale(LC_MESSAGES, NULL);
+               bool works = setlocale(LC_MESSAGES, lang_.c_str());
+               // CTYPE controls what getmessage thinks what encoding the po file uses
+               string oldCTYPE = setlocale(LC_CTYPE, NULL);
+               setlocale(LC_CTYPE, lang_.c_str());
                bindtextdomain(PACKAGE, package().locale_dir().c_str());
                textdomain(PACKAGE);
                const char* msg = gettext(m.c_str());
+               string translated(works ? msg : m);
                // Some english words have different translations, depending
                // on context. In these cases the original string is
                // augmented by context information (e.g.
@@ -109,13 +117,12 @@ public:
                // otherwise the user sees bogus messages.
                // If we are unable to honour the request we just
                // return what we got in.
-               string translated(n ? msg : m);
                static boost::regex const reg("^([^\\[]*)\\[\\[[^\\]]*\\]\\]$");
                boost::smatch sub;
                if (regex_match(translated, sub, reg))
                        translated = sub.str(1);
-               setlocale(LC_ALL, old);
-               free(old);
+               setlocale(LC_MESSAGES, oldMSG.c_str());
+               setlocale(LC_CTYPE, oldCTYPE.c_str());
                return translated;
        }
 private: