PDA

View Full Version : Qt4 and system locale



L.Marvell
4th May 2006, 13:14
I need to know system locale (en, uk, ru etc.) to load appropriate translation. In Qt3 there was QTextCodec::locale() method for this but Qt4 assistant says:

The following class members are part of the Qt 3 support layer. They are provided to help you port old code to Qt 4. We advise against using them in new code.
Also there is QLocale class that has country(), language() and name() methods in Qt4. But there is no locale().
On my Linux machine countryToString() gives me Default and language() - C, but locale is koi8-u. So how can I get system locale?

L.Marvell
4th May 2006, 13:52
Sorry, already did it under win. Will try under Linux at home.

jacek
4th May 2006, 14:02
Also there is QLocale class that has country(), language() and name() methods in Qt4. But there is no locale().
QLocale::name() looks promising:

QString QLocale::name () const
Returns the language and country of this locale as a string of the form "language_country", where language is a lowercase, two-letter ISO 639 language code, and country is an uppercase, two-letter ISO 3166 country code.
See also language() and country().


but locale is koi8-u.
IMO that's the encoding, not locale.

L.Marvell
4th May 2006, 15:01
Yes, exactly QLocale::name() helped. I had to look through examples before post question. :o

Sergey
12th January 2007, 11:08
Hi, all!

I have similar problem in qt4 under win.
I want to get system encoding.

So I do:


QTextCodec * localCodec = QTextCodec::codecForLocale();
QByteArray name = localCodec->name();


variable name contain value "System". But I want to get value "cp1251" or "koi8-r" or something else depending of value current local encoding... Value "System" is not informatively.

Anybody know how to get name of current system encoding?

jacek
12th January 2007, 11:25
Anybody know how to get name of current system encoding?
Have you tried QTextCodec::aliases()?

Sergey
12th January 2007, 12:05
Yes.
it returns NULL.

jacek
12th January 2007, 13:16
Then maybe if you use QTextCodec::mibEnum() first and then get a new codec using QTextCodec::codecForMib() it will have a proper name?

Sergey
15th January 2007, 09:05
I wrote next code:


QTextCodec * localCodec = QTextCodec::codecForLocale();
int mib = localCodec->mibEnum();
QTextCodec * codec = QTextCodec::codecForMib(mib);
QByteArray n = codec->name();


variable n contain value "System"...

Maybe this is a bug in QTextCodec?

jacek
15th January 2007, 13:00
Maybe this is a bug in QTextCodec?
I think that rather OS doesn't specify what encoding it uses currently.