diff options
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 41 | ||||
-rw-r--r-- | src/corelib/tools/qlocale.h | 5 | ||||
-rw-r--r-- | src/corelib/tools/qlocale_p.h | 1 | ||||
-rwxr-xr-x | util/local_database/cldr2qlocalexml.py | 37 | ||||
-rwxr-xr-x | util/local_database/qlocalexml2cpp.py | 10 |
5 files changed, 91 insertions, 3 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index fcfa8f0..3f19c27 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -647,6 +647,17 @@ static QString winSystemPMText() return QString(); } +static quint8 winSystemFirstDayOfWeek() +{ + LCID id = GetUserDefaultLCID(); + wchar_t output[4]; // maximum length including terminating zero character for Win2003+ + + if (GetLocaleInfo(id, LOCALE_IFIRSTDAYOFWEEK, output, 4)) + return QString::fromWCharArray(output).toUInt()+1; + + return 1; +} + /*! \since 4.6 Returns the fallback locale obtained from the system. @@ -736,6 +747,8 @@ QVariant QSystemLocale::query(QueryType type, QVariant in = QVariant()) const return QVariant(winSystemAMText()); case PMText: return QVariant(winSystemPMText()); + case FirstDayOfWeek: + return QVariant(winSystemFirstDayOfWeek()); default: break; } @@ -1162,6 +1175,15 @@ static QLocale::MeasurementSystem macMeasurementSystem() } } +static quint8 macFirstDayOfWeek() +{ + QCFType<CFCalendarRef> calendar = CFCalendarCopyCurrent(); + quint8 day = static_cast<quint8>(CFCalendarGetFirstWeekday(calendar))-1; + if (day == 0) + day = 7; + return day; +} + static void getMacPreferredLanguageAndCountry(QString *language, QString *country) { QCFType<CFArrayRef> languages = (CFArrayRef)CFPreferencesCopyValue( @@ -1243,6 +1265,8 @@ QVariant QSystemLocale::query(QueryType type, QVariant in = QVariant()) const case AMText: case PMText: break; + case FirstDayOfWeek: + return QVariant(macFirstDayOfWeek()); default: break; } @@ -3449,6 +3473,23 @@ QString QLocale::standaloneDayName(int day, FormatType type) const } /*! + \since 4.8 + + Returns the first day of the week according to the current locale. +*/ +Qt::DayOfWeek QLocale::firstDayOfWeek() const +{ +#ifndef QT_NO_SYSTEMLOCALE + if (d() == systemPrivate()) { + QVariant res = systemLocale()->query(QSystemLocale::FirstDayOfWeek, QVariant()); + if (!res.isNull()) + return static_cast<Qt::DayOfWeek>(res.toUInt()); + } +#endif + return static_cast<Qt::DayOfWeek>(d()->m_first_day_of_week); +} + +/*! \since 4.4 Returns the measurement system for the locale. diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h index 6bb9f79..be58faf 100644 --- a/src/corelib/tools/qlocale.h +++ b/src/corelib/tools/qlocale.h @@ -94,7 +94,8 @@ public: MeasurementSystem, // uint PositiveSign, // QString AMText, // QString - PMText // QString + PMText, // QString + FirstDayOfWeek // Qt::DayOfWeek }; virtual QVariant query(QueryType type, QVariant in) const; virtual QLocale fallbackLocale() const; @@ -661,6 +662,8 @@ public: QString dayName(int, FormatType format = LongFormat) const; QString standaloneDayName(int, FormatType format = LongFormat) const; + Qt::DayOfWeek firstDayOfWeek() const; + QString amText() const; QString pmText() const; diff --git a/src/corelib/tools/qlocale_p.h b/src/corelib/tools/qlocale_p.h index d310d9b..b2c86d8 100644 --- a/src/corelib/tools/qlocale_p.h +++ b/src/corelib/tools/qlocale_p.h @@ -167,6 +167,7 @@ public: quint16 m_narrow_day_names_idx, m_narrow_day_names_size; quint16 m_am_idx, m_am_size; quint16 m_pm_idx, m_pm_size; + quint8 m_first_day_of_week : 3; }; inline char QLocalePrivate::digitToCLocale(const QChar &in) const diff --git a/util/local_database/cldr2qlocalexml.py b/util/local_database/cldr2qlocalexml.py index 2567a89..8b5ec16 100755 --- a/util/local_database/cldr2qlocalexml.py +++ b/util/local_database/cldr2qlocalexml.py @@ -322,6 +322,40 @@ def usage(): print "Usage: cldr2qlocalexml.py <path-to-cldr-main>" sys.exit() +def integrateWeekData(filePath): + if not filePath.endswith(".xml"): + return {} + monFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=mon]", attribute="territories")[0].split(" ") + tueFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=tue]", attribute="territories")[0].split(" ") + wedFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=wed]", attribute="territories")[0].split(" ") + thuFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=thu]", attribute="territories")[0].split(" ") + friFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=fri]", attribute="territories")[0].split(" ") + satFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=sat]", attribute="territories")[0].split(" ") + sunFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=sun]", attribute="territories")[0].split(" ") + + firstDayByCountryCode = {} + for countryCode in monFirstDayIn: + firstDayByCountryCode[countryCode] = "mon" + for countryCode in tueFirstDayIn: + firstDayByCountryCode[countryCode] = "tue" + for countryCode in wedFirstDayIn: + firstDayByCountryCode[countryCode] = "wed" + for countryCode in thuFirstDayIn: + firstDayByCountryCode[countryCode] = "thu" + for countryCode in friFirstDayIn: + firstDayByCountryCode[countryCode] = "fri" + for countryCode in satFirstDayIn: + firstDayByCountryCode[countryCode] = "sat" + for countryCode in sunFirstDayIn: + firstDayByCountryCode[countryCode] = "sun" + + for (key,locale) in locale_database.iteritems(): + countryCode = locale['country_code'] + if countryCode in firstDayByCountryCode: + locale_database[key]['firstDayOfWeek'] = firstDayByCountryCode[countryCode] + else: + locale_database[key]['firstDayOfWeek'] = firstDayByCountryCode["001"] + if len(sys.argv) != 2: usage() @@ -341,6 +375,7 @@ for file in cldr_files: locale_database[(l['language_id'], l['country_id'], l['script_code'], l['variant_code'])] = l +integrateWeekData(cldr_dir+"/../supplemental/supplementalData.xml") locale_keys = locale_database.keys() locale_keys.sort() @@ -484,6 +519,7 @@ print \ <exp>101</exp>\n\ <am>AM</am>\n\ <pm>PM</pm>\n\ + <firstDayOfWeek>mon</firstDayOfWeek>\n\ <longDateFormat>EEEE, d MMMM yyyy</longDateFormat>\n\ <shortDateFormat>d MMM yyyy</shortDateFormat>\n\ <longTimeFormat>HH:mm:ss z</longTimeFormat>\n\ @@ -520,6 +556,7 @@ for key in locale_keys: print " <exp>" + fixOrdStrExp(l['exp']) + "</exp>" print " <am>" + l['am'].encode('utf-8') + "</am>" print " <pm>" + l['pm'].encode('utf-8') + "</pm>" + print " <firstDayOfWeek>" + l['firstDayOfWeek'].encode('utf-8') + "</firstDayOfWeek>" print " <longDateFormat>" + l['longDateFormat'].encode('utf-8') + "</longDateFormat>" print " <shortDateFormat>" + l['shortDateFormat'].encode('utf-8') + "</shortDateFormat>" print " <longTimeFormat>" + l['longTimeFormat'].encode('utf-8') + "</longTimeFormat>" diff --git a/util/local_database/qlocalexml2cpp.py b/util/local_database/qlocalexml2cpp.py index 9251e1f..9bc3c7e 100755 --- a/util/local_database/qlocalexml2cpp.py +++ b/util/local_database/qlocalexml2cpp.py @@ -188,6 +188,10 @@ def convertFormat(format): return result +def convertToQtDayOfWeek(firstDay): + qtDayOfWeek = {"mon":1, "tue":2, "wed":3, "thu":4, "fri":5, "sat":6, "sun":7} + return qtDayOfWeek[firstDay] + class Locale: def __init__(self, elt): self.language = eltText(firstChildElt(elt, "language")) @@ -202,6 +206,7 @@ class Locale: self.exp = int(eltText(firstChildElt(elt, "exp"))) self.am = eltText(firstChildElt(elt, "am")) self.pm = eltText(firstChildElt(elt, "pm")) + self.firstDayOfWeek = convertToQtDayOfWeek(eltText(firstChildElt(elt, "firstDayOfWeek"))) self.longDateFormat = convertFormat(eltText(firstChildElt(elt, "longDateFormat"))) self.shortDateFormat = convertFormat(eltText(firstChildElt(elt, "shortDateFormat"))) self.longTimeFormat = convertFormat(eltText(firstChildElt(elt, "longTimeFormat"))) @@ -397,7 +402,7 @@ def main(): for key in locale_keys: l = locale_map[key] - print " { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s }, // %s/%s" \ + print " { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%6d }, // %s/%s" \ % (key[0], key[1], l.decimal, l.group, @@ -425,9 +430,10 @@ def main(): days_data.append(l.narrowDays), am_data.append(l.am), pm_data.append(l.pm), + l.firstDayOfWeek, l.language, l.country) - print " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 } // trailing 0s" + print " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0 } // trailing 0s" print "};" print |