PDA

View Full Version : QLocale confusion :(



gri
13th June 2007, 15:20
Hello there,

I'm confusing myself with QLocale, maybe someone of you can help me :)
I simply want to get the language code for a language. "languageToCode()" would be exactly what I want to have but this is only an internal function by qlocale.cpp.

So I tried by using this:

QLocale locale(QLocale::German, QLocale::AnyCountry); // Docs say AnyCountry takes the most appropriate country ... I DONT WANT THIS
QString code = locale.name(); // this returns "de_DE" ... nice
My problem is, I only wanted the "de" in front and I don't like to split the string cause this is unpretty!

Then I looked at the QLocale::name() which has the following code fragment:

Country c = country();
if (c == AnyCountry)
return result;
Wow. If the constructor wouldnt change AnyCountry to German I would get what I want!

There is no way to set the country outside of the constructor of QLocale ... so what now?!

Thanks, gri

PS: So much time about a simple thing :( :mad:

wysota
13th June 2007, 18:53
Did you try QLocale::countryToString()?

gri
14th June 2007, 11:24
Did you try QLocale::countryToString()?

This returns "Germany". I know that QLocale::name() constructs the "de_DE" of two functions (as seen in source) but why is there no way to get these values without using QString::split()? :(

wysota
14th June 2007, 11:32
Using split() here is nothing wrong.

patrik08
14th June 2007, 17:32
Simple grab the first two toLower letter ... run on all os....




static inline QString UserLanguage()
{
QString languser,languagesistem,langqt;
QLocale loci = QLocale::system();
languser = getenv("LANG");
languser = languser.toLower();
languagesistem = loci.name();
languagesistem = languagesistem.toLower();
languagesistem = languagesistem.left(2);
/* Window XP Prof MUI Multiuser && Multilanguage == stay only "c" language && user setting setenv !!! */
if (languagesistem == "c") {
if (languser.size() > 2 && languser.contains("_")) {
languagesistem = languser.left(2);
}
}
return languagesistem;
}

gri
15th June 2007, 13:57
Simple grab the first two toLower letter ... run on all os....




static inline QString UserLanguage()
{
QString languser,languagesistem,langqt;
QLocale loci = QLocale::system();
languser = getenv("LANG");
languser = languser.toLower();
languagesistem = loci.name();
languagesistem = languagesistem.toLower();
languagesistem = languagesistem.left(2);
/* Window XP Prof MUI Multiuser && Multilanguage == stay only "c" language && user setting setenv !!! */
if (languagesistem == "c") {
if (languser.size() > 2 && languser.contains("_")) {
languagesistem = languser.left(2);
}
}
return languagesistem;
}



Nice (and much) code. I already knew how to get at the first two letters from "de_DE" but my problem was that QLocale::name() constructs the returned string from two parts and I thought it would be very silly to let it add these strings together and later I split them again. Much work around nothing?!

My ugly solution:

QString Application::languageToCode(QLocale::Language language) const
{
return QLocale(language).name().split('_').first();
}

high_flyer
15th June 2007, 14:09
Much work around nothing?!
why about nothing?
the language code IS de_DE, since german german and swiss german for exmaple are not the same, oherwise there would have been no distinction.
Qt conforms to the standard in the this regard.
If all you care about is that it is German, but not which German it is, then its specific to you, and you will have to add the extra code for that.