Results 1 to 7 of 7

Thread: QLocale confusion :(

  1. #1
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QLocale confusion :(

    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:
    Qt Code:
    1. QLocale locale(QLocale::German, QLocale::AnyCountry); // Docs say AnyCountry takes the most appropriate country ... I DONT WANT THIS
    2. QString code = locale.name(); // this returns "de_DE" ... nice
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. Country c = country();
    2. if (c == AnyCountry)
    3. return result;
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLocale confusion :(


  3. #3
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLocale confusion :(

    Quote Originally Posted by wysota View Post
    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()?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLocale confusion :(

    Using split() here is nothing wrong.

  5. #5
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLocale confusion :(

    Simple grab the first two toLower letter ... run on all os....


    Qt Code:
    1. static inline QString UserLanguage()
    2. {
    3. QString languser,languagesistem,langqt;
    4. QLocale loci = QLocale::system();
    5. languser = getenv("LANG");
    6. languser = languser.toLower();
    7. languagesistem = loci.name();
    8. languagesistem = languagesistem.toLower();
    9. languagesistem = languagesistem.left(2);
    10. /* Window XP Prof MUI Multiuser && Multilanguage == stay only "c" language && user setting setenv !!! */
    11. if (languagesistem == "c") {
    12. if (languser.size() > 2 && languser.contains("_")) {
    13. languagesistem = languser.left(2);
    14. }
    15. }
    16. return languagesistem;
    17. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2007
    Location
    Augsburg, Germany
    Posts
    75
    Thanks
    4
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QLocale confusion :(

    Quote Originally Posted by patrik08 View Post
    Simple grab the first two toLower letter ... run on all os....


    Qt Code:
    1. static inline QString UserLanguage()
    2. {
    3. QString languser,languagesistem,langqt;
    4. QLocale loci = QLocale::system();
    5. languser = getenv("LANG");
    6. languser = languser.toLower();
    7. languagesistem = loci.name();
    8. languagesistem = languagesistem.toLower();
    9. languagesistem = languagesistem.left(2);
    10. /* Window XP Prof MUI Multiuser && Multilanguage == stay only "c" language && user setting setenv !!! */
    11. if (languagesistem == "c") {
    12. if (languser.size() > 2 && languser.contains("_")) {
    13. languagesistem = languser.left(2);
    14. }
    15. }
    16. return languagesistem;
    17. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QString Application::languageToCode(QLocale::Language language) const
    2. {
    3. return QLocale(language).name().split('_').first();
    4. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QLocale confusion :(

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QProgressBar confusion
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 23rd November 2006, 09:20
  2. Qt3 to Qt4 printer setup confusion
    By impeteperry in forum Qt Programming
    Replies: 6
    Last Post: 5th May 2006, 12:40
  3. QSortFilterProxyModel signal and selection confusion
    By pascal123 in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2006, 16:25
  4. Plugins confusion
    By Paul Drummond in forum Qt Programming
    Replies: 11
    Last Post: 15th February 2006, 09:46
  5. Confusion with QPoint
    By therealjag in forum Qt Programming
    Replies: 9
    Last Post: 14th February 2006, 17:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.