Results 1 to 9 of 9

Thread: Translating Date and Month Names

  1. #1
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Translating Date and Month Names

    Qt gets its month and day names from the O/S, if I understand right. In the app I'm working on there are some cases when the user might be running in a language different than the language used by the O/S. I would like to be able to add month and day names to the translation file then so that the language is consistant throughout the application. Is there any way I can do that short of rewriting QDateTime and QDateTimeEdit?

    Thanks in advance.

    Jimmy

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Translating Date and Month Names

    Maybe QLocale will help? You can either make your application use some particular locale, for example C, or format your dates using QLocale::c().toString() et al.

  3. #3
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Translating Date and Month Names

    I'm not sure I understand how that could help. If my application is using a Chinese translation, but the O/S is English, my objective is to have the dates appear in Chinese. If I use the Chinese locale, won't the day and month names still appear in English as they are coming from the O/S?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Translating Date and Month Names

    Quote Originally Posted by Jimmy2775 View Post
    If I use the Chinese locale, won't the day and month names still appear in English as they are coming from the O/S?
    What do you exactly mean by "day and month names coming from the O/S"? QDate and QDateTime will use day and month names from the current locale, if you set it to Chinese, they should be in Chinese, if you set it to C, they should be in English --- regardless from the underlying system.

  5. #5
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Translating Date and Month Names

    Quote Originally Posted by jacek View Post
    QDate and QDateTime will use day and month names from the current locale, if you set it to Chinese, they should be in Chinese, if you set it to C, they should be in English --- regardless from the underlying system.
    I don't think that's correct.

    Try this code:
    Qt Code:
    1. QLocale::setDefault (QLocale( "fr_FR" ) );
    2. QLocale iLocale;
    3. QString longFormat = iLocale.dateFormat( QLocale::LongFormat );
    4. edit = new QTextEdit;
    5. edit->setPlainText(QDate::currentDate().toString( longFormat ));
    To copy to clipboard, switch view to plain text mode 

    If I run it as is, it outputs "Wednesday 1 November 2006".

    If I change my Windows system settings to the fr_FR locale and run it again, I get "mercredi 1 novembre 2006".

    It would appear that the language for the day and month have nothing to do with the current locale and everything to do with the operating system settings.

    To resolve this I want to be able to use day/month names from a translation file but that seems like it'd be pretty tough to do.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Translating Date and Month Names

    Quote Originally Posted by Jimmy2775 View Post
    I don't think that's correct.
    You're right, QDate always uses QLocale::system(), but still you can do:
    Qt Code:
    1. QLocale::setDefault (QLocale( "fr_FR" ) );
    2. ...
    3. edit->setPlainText( QLocale().toString( QDate::currentDate() ) );
    To copy to clipboard, switch view to plain text mode 
    and everything should be fine.

  7. The following 2 users say thank you to jacek for this useful post:

    Jimmy2775 (2nd November 2006), sarefo (7th April 2008)

  8. #7
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Translating Date and Month Names

    Yes that works - excellent. Thanks jacek.

    That will solve half my problem: all the dates that are converted to strings. Is there something similar I can do to QDateTimeEdit so that it will display values in the appropriate language as well?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Translating Date and Month Names

    Quote Originally Posted by Jimmy2775 View Post
    Is there something similar I can do to QDateTimeEdit so that it will display values in the appropriate language as well?
    You could try to subclass QDateTimeEdit and reimplement QDateTimeEdit::dateTimeFromText() (this might be tricky) and QDateTimeEdit::textFromDateTime(). You will have to update your .ui files, but I guess that "promote to custom widget" should be enough.

  10. #9
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Translating Date and Month Names

    OK I will give that a shot and see how it works. Thanks for your help jacek.

    Jimmy

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.