other people's work

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10248 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2005-07-16 15:55:36 +00:00
parent 7173717c8c
commit 7e08de938a
6 changed files with 62 additions and 32 deletions

View File

@ -22,10 +22,14 @@
#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);
}

View 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;

View 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() : "";
}

View 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);

View 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_;

View 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: