#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 {
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;
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");
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);
+
}
// needs to be done before reading lyxrc
lyxrc.dpi = getDPI();
- initEncodings();
-
LoaderQueue::setPriority(10,100);
}
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;
QString const toqstr(char const * str)
{
- QTextCodec * codec = QTextCodec::codecForLocale();
-
- return codec->toUnicode(str);
+ return QString::fromAscii(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() : "";
}
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", "");
}
languagelist[lang] = Language(lang, babel, display, rtl,
- encoding, code, latex_options);
+ encoding_str, encoding, code, latex_options);
}
default_language = getLanguage(lyxrc.default_language);
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_; }
///
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_; }
///
bool rightToLeft_;
///
+ std::string encodingStr_;
+ ///
Encoding const * encoding_;
///
std::string code_;
#include <config.h>
+#include "debug.h"
#include "messages.h"
#include "support/filetools.h"
#include "support/package.h"
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() {}
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.
// 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: