[SOLVED] QLocale gets the wrong system locale on Windows 7



  • Hello,

    I am trying to get the system locale using the following code:

    @
    #include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QLocale thisLoc = QLocale::system&#40;&#41;;
    QString locale = thisLoc.name(&#41;;
    qDebug(&#41; << "locale : " << locale;
    qDebug() << "Country : " << thisLoc.countryToString(thisLoc.country());
    
    MainWindow w;
    w.show();
    
    return a.exec(&#41;;
    

    }
    @

    This is just a standard Qt GUI application created with Qt Creator.
    I run it on a Japanese Windows 7 box and it always reports en_US for the local name and UnitedStates for the country no matter what I set the actual system local to.

    locale : "en_US"
    Country : "UnitedStates"

    Is this a known bug or do I need to do something special to make it work?

    BTW. It works as expected on Mac OS X.

    Thanks.



  • Well... I made a work around using the standard C library functions that gets the correct locale from my system. Being Windows it does not return the name in the ISO form but then, that is Windows for ya!, no big deal just an annoyance.

    Now, I guess my beef is why the QLocale is not getting the correct system locale! I was looking forward to having Qt take care of all this platform dependency but I am not seeing that in this case.

    So, to you good folks out there, anyone have any suggestions as to what might be going wrong here and what can be done to fix it so I can get rid of my little work around.

    A big thanks to any kind soul out there with any useful suggestions to offer

    (O_O)/


  • Lifetime Qt Champion

    Hi,

    This might be a bug, did you already checked if a "report":bugreports.qt-project.org/ has been made on the subject ?

    Otherwise you could make one (posting your workaround code would be great if someone else encounters the same problem)



  • Hmm, strange behavior you describe! Strange it doesn't work, because it works for me:
    @
    QString defaultLocale = QLocale::system().name(); // e.g. "de_DE"
    @
    this just works fine resulting in a nl_NL (dutch) language setting.
    I'm using it in the MainWindow to set the selected language.
    Might there be an issue with your code anyway?



  • Hmm,
    Think I found it, the StringToLanguage is a static function. It shouldn't be called on a object, but as class static function.
    So:
    @
    qDebug() << "Country : " << thisLoc.countryToString(thisLoc.country());
    // Should be:
    qDebug() << "Country: " << QLocale::countryToString(thisLoc.country());
    @
    You probably will get the US country because that will be set as the default language/country in Qt as stated in an empty constructor.
    Hope this helps.
    Greetz



  • Hello,

    Thank you for you reply.

    I did see some related stuff dating back to several years ago but did not see anything in known issues for the latest releases.

    I can't believe that this would still be a bug in this day and age of global application development. Which is why I thought it may be something to with me and my installation. I think maybe a rebuild of my Qt is called for on this one.

    For what it is worth and for anyone interested I get the current locale using the standard library call like this.

    @
    ...
    #include <string.h>
    #include <locale.h>
    ...
    char *locale = setlocale( LC_ALL, "" );
    ...
    @

    Which gives me the name the locale is currently set to in the environment the program is running in, i.e. what the user has set it to, which works for me. I only wanted the name of the locale in this case so I parse what I need out of the string, but I am sure the rest of the standard features are working.

    Someone please let me know if this is still a known issue with QLocale because I don't know where else to look.

    Cheers



  • Hi,
    Did you tried my second post????? It's not Qt, it's a C++ thing! The same issues comes along now and then with timers using the static functions!
    The QLocale is not to blame!
    Greetz



  • Thank you Jeroentje@home,

    I tried your suggestions but I get the same result as before.

    locale : "en_US"
    Country : "UnitedStates"

    Cheers.



  • Hmm, strange, it works for me.



  • Yes it is indeed strange.

    I guess all I can do is rebuild Qt and look carefully at the warning messages to see if there is some subtle problem happening.

    Thanks to you all for your suggestions.



  • [SOLVED] Hello again,

    I found the cause of this problem.

    There is a an environment variable called LANG that is used by the Designer app in standalone mode (i.e. when it is not running plugged into Creator) to set its current GUI language.

    The Qt locale object is getting it's locale setting from this variable when it is set. If it is not set the Locale information comes from the OS setting. Since my OS is a Japanese version of windows I set this env var when I want to use Designer standalone in a language other than Japanese. It so happened that I had left it set to en_US when I was doing this code to get the Locale.

    So there you have it, I am pleased to find that it is not a fundamental problem with QT Locale... or is it???

    Personally I think the Qt library itself should not be using the LANG environment variable for getting the "system" locale in the first instance, if at all. I can understand Designer using it for it own purposes but not the base library...!!

    It is a bit inconvenient and I did not see any mention of that in the QLocale docs.

    What does anyone anyone else think about this?

    Thanks again to those who helped me out.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.