Results 1 to 3 of 3

Thread: Changing language for QDate

  1. #1
    kaszewczyk Guest

    Default Changing language for QDate

    Hello,
    I have a question about toString(QString format) method of QDate class.
    I want to get short name of current month, so I entered format toString("MMM"), everything is ok, but this method returned me short month name in my language and I want it to be short month name in English, is it possible to change language of QDate ?

    Best Regards
    kaszewczyk

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Changing language for QDate

    This is how QDate is creating the short month name:
    Qt Code:
    1. QString QDate::shortMonthName(int month, QDate::MonthNameType type)
    2. {
    3. if (month < 1 || month > 12) {
    4. month = 1;
    5. }
    6. switch (type) {
    7. case QDate::DateFormat:
    8. return QLocale::system().monthName(month, QLocale::ShortFormat);
    9. case QDate::StandaloneFormat:
    10. return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
    11. default:
    12. break;
    13. }
    14. return QString();
    15. }
    To copy to clipboard, switch view to plain text mode 
    It's using system locale. From the documentation of QLocale :: system:
    On Windows and Mac, this locale will use the decimal/grouping characters and date/time formats specified in the system configuration panel.
    So I think easiest way will be to write your own class for translatable QDate with custom toString(), and use QLocale::monthName method, something like:
    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QDate>
    4.  
    5. class MyDate
    6. {
    7. public:
    8. MyDate(const QDate& d, const QLocale& loc = QLocale()) : _date(d), _loc(loc)
    9. {}
    10. void setLocale(const QLocale& loc){
    11. this->_loc = loc;
    12. }
    13. void setDate(const QDate& d){
    14. this->_date = d;
    15. }
    16. QDate date() const{
    17. return this->_date;
    18. }
    19. QString toString() const{
    20. return QString("%1 %2 %3").arg(_date.day())
    21. .arg( _loc.monthName(_date.month(), QLocale::ShortFormat) )
    22. .arg(_date.year());
    23. }
    24. protected:
    25. QDate _date;
    26. QLocale _loc;
    27. };
    28.  
    29. int main(int argc, char **argv) {
    30. MyDate date( QDate::currentDate(), QLocale::English );
    31.  
    32. qDebug() << date.toString();
    33.  
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 
    Maybe there is easier way, but I'm not aware of it now.

  3. #3
    Join Date
    Sep 2012
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows

    Default Re: Changing language for QDate

    More then half an year has passed since this thread was last answered. But anyways here's how I did it, if its helps someone else

    Qt Code:
    1. QDate date = QDateTime::currentDateTime().date();
    2. QLocale locale = QLocale(QLocale::Swedish, QLocale::Sweden); // set the locale you want here
    3. QString swedishDate = locale.toString(date, "dddd, d MMMM yyyy");
    To copy to clipboard, switch view to plain text mode 

    cheers

  4. The following user says thank you to pataliz0r for this useful post:

    Lumbricus (17th November 2015)

Similar Threads

  1. QSlider: changing the groove without changing the handle
    By Olivier Berten in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2013, 10:02
  2. can't get Qdate
    By mmm286 in forum Newbie
    Replies: 1
    Last Post: 4th March 2010, 09:25
  3. QDate next day
    By AlexD in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2010, 22:40
  4. Replies: 2
    Last Post: 10th August 2009, 09:45
  5. QDate Help pls
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2006, 06:50

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
  •  
Qt is a trademark of The Qt Company.