Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QLocale gets the wrong system locale on Windows 7
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 8.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kenchan
    wrote on 25 Jun 2013, 14:03 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on 26 Jun 2013, 08:10 last edited by
      #2

      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)/

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 26 Jun 2013, 08:17 last edited by
        #3

        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)

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jeroentjehome
          wrote on 26 Jun 2013, 08:52 last edited by
          #4

          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?

          Greetz, Jeroen

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jeroentjehome
            wrote on 26 Jun 2013, 09:02 last edited by
            #5

            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

            Greetz, Jeroen

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kenchan
              wrote on 26 Jun 2013, 09:14 last edited by
              #6

              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

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jeroentjehome
                wrote on 26 Jun 2013, 09:26 last edited by
                #7

                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

                Greetz, Jeroen

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kenchan
                  wrote on 26 Jun 2013, 09:53 last edited by
                  #8

                  Thank you Jeroentje@home,

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

                  locale : "en_US"
                  Country : "UnitedStates"

                  Cheers.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jeroentjehome
                    wrote on 26 Jun 2013, 10:00 last edited by
                    #9

                    Hmm, strange, it works for me.

                    Greetz, Jeroen

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kenchan
                      wrote on 26 Jun 2013, 10:06 last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kenchan
                        wrote on 30 Jun 2013, 22:28 last edited by
                        #11

                        [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.

                        1 Reply Last reply
                        0

                        3/11

                        26 Jun 2013, 08:17

                        topic:navigator.unread, 8
                        • Login

                        • Login or register to search.
                        3 out of 11
                        • First post
                          3/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved