PDA

View Full Version : Translating Date and Month Names



Jimmy2775
1st November 2006, 18:17
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

jacek
1st November 2006, 19:54
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.

Jimmy2775
1st November 2006, 20:15
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?

jacek
1st November 2006, 22:49
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.

Jimmy2775
1st November 2006, 23:14
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:

QLocale::setDefault (QLocale( "fr_FR" ) );
QLocale iLocale;
QString longFormat = iLocale.dateFormat( QLocale::LongFormat );
edit = new QTextEdit;
edit->setPlainText(QDate::currentDate().toString( longFormat ));

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.

jacek
1st November 2006, 23:43
I don't think that's correct.
You're right, QDate always uses QLocale::system(), but still you can do:

QLocale::setDefault (QLocale( "fr_FR" ) );
...
edit->setPlainText( QLocale().toString( QDate::currentDate() ) );
and everything should be fine.

Jimmy2775
1st November 2006, 23:59
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?

jacek
2nd November 2006, 00:28
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.

Jimmy2775
2nd November 2006, 16:35
OK I will give that a shot and see how it works. Thanks for your help jacek.

Jimmy