This is how QDate is creating the short month name:
{
if (month < 1 || month > 12) {
month = 1;
}
switch (type) {
case QDate::StandaloneFormat: return QLocale::system().
standaloneMonthName(month,
QLocale::ShortFormat);
default:
break;
}
}
QString QDate::shortMonthName(int month, QDate::MonthNameType type)
{
if (month < 1 || month > 12) {
month = 1;
}
switch (type) {
case QDate::DateFormat:
return QLocale::system().monthName(month, QLocale::ShortFormat);
case QDate::StandaloneFormat:
return QLocale::system().standaloneMonthName(month, QLocale::ShortFormat);
default:
break;
}
return QString();
}
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:
#include <QApplication>
#include <QDebug>
#include <QDate>
class MyDate
{
public:
MyDate
(const QDate
& d,
const QLocale
& loc
= QLocale()) : _date
(d
), _loc
(loc
) {}
void setLocale(const QLocale& loc){
this->_loc = loc;
}
void setDate(const QDate& d){
this->_date = d;
}
return this->_date;
}
return QString("%1 %2 %3").
arg(_date.
day()) .
arg( _loc.
monthName(_date.
month(),
QLocale::ShortFormat) ) .arg(_date.year());
}
protected:
};
int main(int argc, char **argv) {
qDebug() << date.toString();
return 0;
}
#include <QApplication>
#include <QDebug>
#include <QDate>
class MyDate
{
public:
MyDate(const QDate& d, const QLocale& loc = QLocale()) : _date(d), _loc(loc)
{}
void setLocale(const QLocale& loc){
this->_loc = loc;
}
void setDate(const QDate& d){
this->_date = d;
}
QDate date() const{
return this->_date;
}
QString toString() const{
return QString("%1 %2 %3").arg(_date.day())
.arg( _loc.monthName(_date.month(), QLocale::ShortFormat) )
.arg(_date.year());
}
protected:
QDate _date;
QLocale _loc;
};
int main(int argc, char **argv) {
MyDate date( QDate::currentDate(), QLocale::English );
qDebug() << date.toString();
return 0;
}
To copy to clipboard, switch view to plain text mode
Maybe there is easier way, but I'm not aware of it now.
Bookmarks