Changeset 622 for trunk/src


Ignore:
Timestamp:
Mar 3, 2010, 8:54:13 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

corelib: Implemented deriving the defaut locale from the process country code if LANG is not set and integration with the system locale object (System Setup -> Country Palette). Closes #123.

Location:
trunk/src/corelib/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/corelib/tools/qlocale.cpp

    r561 r622  
    6363#   include "qt_windows.h"
    6464#   include <time.h>
     65#endif
     66#if defined(Q_OS_OS2)
     67#   include "qt_os2.h"
     68#   include <unidef.h>
    6569#endif
    6670#if !defined(QWS) && defined(Q_OS_MAC)
     
    933937
    934938    return result;
     939}
     940
     941
     942#elif defined(Q_OS_OS2)
     943/******************************************************************************
     944** Wrappers for OS/2 locale system functions
     945*/
     946
     947Q_CORE_EXPORT QString qt_system_locale_name()
     948{
     949    QString locale = QString::fromLatin1(envVarLocale());
     950    if (locale.isEmpty()) {
     951        // get the locale from the system country code
     952        COUNTRYCODE code = { 0, 0 };
     953        COUNTRYINFO info;
     954        ULONG actual;
     955        APIRET arc = DosQueryCtryInfo(sizeof(info), &code, &info, &actual);
     956        if (arc == NO_ERROR) {
     957            UniChar localeName[8];
     958            if (UniMapCtryToLocale(info.country, localeName,
     959                                   sizeof(localeName)) == ULS_SUCCESS)
     960                locale = QString::fromUtf16(localeName);
     961        }
     962    }
     963
     964    return locale;
     965}
     966
     967/*!
     968    \since 4.6
     969
     970    Returns a fallback locale, that will get used for everything that
     971    is not explicitly overridden by the system locale.
     972*/
     973QLocale QSystemLocale::fallbackLocale() const
     974{
     975    return QLocale(qt_system_locale_name());
     976}
     977
     978/*!
     979    Performs a query of the given \a type in the system locale for
     980    customized values or conversion. If the method returns a null
     981    QVariant, the conversion of the fallbackLocale() will be used.
     982
     983    \a in is unused for some of the query types.
     984
     985    \sa QSystemLocale::QueryType
     986*/
     987QVariant QSystemLocale::query(QueryType type, QVariant in = QVariant()) const
     988{
     989    static LocaleItem dayNames[] = {
     990        // 0 ... 6 (Mon - Sun)
     991        LOCI_sDayName1, LOCI_sDayName2, LOCI_sDayName3, LOCI_sDayName4,
     992        LOCI_sDayName5, LOCI_sDayName6, LOCI_sDayName7,
     993        // 7 ... 13 (Mon - Sun)
     994        LOCI_sAbbrevDayName1, LOCI_sAbbrevDayName2, LOCI_sAbbrevDayName3,
     995        LOCI_sAbbrevDayName4, LOCI_sAbbrevDayName5, LOCI_sDayName6,
     996        LOCI_sAbbrevDayName7,
     997    };
     998
     999    static LocaleItem monthNames[] = {
     1000        // 0 ... 11 (Jan - Dec)
     1001        LOCI_sMonthName1, LOCI_sMonthName2, LOCI_sMonthName3, LOCI_sMonthName4,
     1002        LOCI_sMonthName5, LOCI_sMonthName6, LOCI_sMonthName7, LOCI_sMonthName8,
     1003        LOCI_sMonthName9, LOCI_sMonthName10, LOCI_sMonthName11, LOCI_sMonthName12,
     1004        // 12 ... 23 (Jan - Dec)
     1005        LOCI_sAbbrevMonthName1, LOCI_sAbbrevMonthName2, LOCI_sAbbrevMonthName3,
     1006        LOCI_sAbbrevMonthName4, LOCI_sAbbrevMonthName5, LOCI_sAbbrevMonthName6,
     1007        LOCI_sAbbrevMonthName7, LOCI_sAbbrevMonthName8, LOCI_sAbbrevMonthName9,
     1008        LOCI_sAbbrevMonthName10, LOCI_sAbbrevMonthName11, LOCI_sAbbrevMonthName12,
     1009    };
     1010
     1011    QString sysLocaleName = qt_system_locale_name();
     1012
     1013    LocaleObject lo = 0;
     1014    if (UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
     1015                              sysLocaleName.utf16(), &lo) == ULS_SUCCESS) {
     1016        int num;
     1017        tm timespec = { 0 };
     1018        bool isDateTime = false;
     1019
     1020        LocaleItem item = 0;
     1021
     1022        switch(type) {
     1023        // case LanguageId:
     1024        // case CountryId:
     1025        //     this is returned by the fallback locale
     1026        case DecimalPoint:
     1027            item = LOCI_sDecimal;
     1028            break;
     1029        case GroupSeparator:
     1030            item = LOCI_sThousand;
     1031            break;
     1032        case ZeroDigit:
     1033            item = LOCI_sNativeDigits;
     1034            break;
     1035        case NegativeSign:
     1036            item = LOCI_sNegativeSign;
     1037            break;
     1038        case PositiveSign:
     1039            item = LOCI_sPositiveSign;
     1040            break;
     1041        case DateFormatLong:
     1042            item = LOCI_wLongDate;
     1043            break;
     1044        case DateFormatShort:
     1045            item = LOCI_wShortDate;
     1046            break;
     1047        case TimeFormatLong:
     1048        case TimeFormatShort:
     1049            item = LOCI_wTimeFormat;
     1050            break;
     1051        case DayNameLong:
     1052            num = in.toInt();
     1053            if (num >= 1 && num <= 7)
     1054                item = dayNames[num - 1];
     1055            break;
     1056        case DayNameShort:
     1057            num = in.toInt();
     1058            if (num >= 1 && num <= 7)
     1059                item = dayNames[num - 1 + 7];
     1060            break;
     1061        case MonthNameLong:
     1062            num = in.toInt();
     1063            if (num >= 1 && num <= 12)
     1064                item = monthNames[num - 1];
     1065            break;
     1066        case MonthNameShort:
     1067            num = in.toInt();
     1068            if (num >= 1 && num <= 12)
     1069                item = monthNames[num - 1 + 12];
     1070            break;
     1071        case DateToStringLong:
     1072        case DateToStringShort: {
     1073            QDate date = in.toDate();
     1074            timespec.tm_mday = date.day();
     1075            timespec.tm_mon = date.month() - 1;
     1076            timespec.tm_year = date.year() - 1900;
     1077            timespec.tm_isdst = -1;
     1078            item = type == DateToStringShort ? LOCI_sShortDate : LOCI_sLongDate;
     1079            isDateTime = true;
     1080            break;
     1081        }
     1082        case TimeToStringLong:
     1083        case TimeToStringShort: {
     1084            QTime time = in.toTime();
     1085            timespec.tm_sec = time.second();
     1086            timespec.tm_min = time.minute();
     1087            timespec.tm_hour = time.hour();
     1088            timespec.tm_isdst = -1;
     1089            item = LOCI_sTimeFormat;
     1090            isDateTime = true;
     1091            break;
     1092        }
     1093        case DateTimeFormatLong:
     1094        case DateTimeFormatShort:
     1095            return
     1096                  query(type == DateTimeFormatLong ?
     1097                        DateFormatLong : DateFormatShort).toString()
     1098                + QLatin1Char(' ')
     1099                + query(type == DateTimeFormatLong ?
     1100                        TimeFormatLong : TimeFormatShort).toString();
     1101        case DateTimeToStringShort: {
     1102            QDateTime dateTime = in.toDateTime();
     1103            return
     1104                  query(DateToStringShort, dateTime.date()).toString()
     1105                + QLatin1Char(' ')
     1106                + query(TimeToStringShort, dateTime.time()).toString();
     1107        }
     1108        case DateTimeToStringLong: {
     1109            QDateTime dateTime = in.toDateTime();
     1110            timespec.tm_sec = dateTime.time().second();
     1111            timespec.tm_min = dateTime.time().minute();
     1112            timespec.tm_hour = dateTime.time().hour();
     1113            timespec.tm_mday = dateTime.date().day();
     1114            timespec.tm_mon = dateTime.date().month() - 1;
     1115            timespec.tm_wday = dateTime.date().dayOfWeek() % 7;
     1116            timespec.tm_year = dateTime.date().year() - 1900;
     1117            timespec.tm_isdst = -1;
     1118            item = LOCI_sDateTime;
     1119            isDateTime = true;
     1120            break;
     1121        }
     1122        case MeasurementSystem:
     1123            if (UniQueryLocaleValue(lo, LOCI_iMeasure, &num) == ULS_SUCCESS &&
     1124                num == 1)
     1125                return QVariant(static_cast<int>(QLocale::ImperialSystem));
     1126            return QVariant(static_cast<int>(QLocale::MetricSystem));
     1127        case AMText:
     1128            item = LOCI_s1159;
     1129            break;
     1130        case PMText:
     1131            item = LOCI_s2359;
     1132            break;
     1133        default:
     1134            break;
     1135        }
     1136
     1137        QString result;
     1138
     1139        if (item) {
     1140            if (isDateTime) {
     1141                UniChar *format = 0;
     1142                if (UniQueryLocaleItem(lo, item, &format) == ULS_SUCCESS) {
     1143                    QByteArray buf;
     1144                    for (size_t i = 1; i < 10; ++i) {
     1145                        buf.resize(i * 256 * 2);
     1146                        if (UniStrftime(lo, (UniChar *)buf.data(), buf.size() / 2,
     1147                                        format, &timespec)) {
     1148                            result = QString::fromUtf16((const ushort *)buf.data());
     1149                            break;
     1150                        }
     1151                    }
     1152                    UniFreeMem(format);
     1153                }
     1154            } else {
     1155                UniChar *value = 0;
     1156                if (UniQueryLocaleItem(lo, item, &value) == ULS_SUCCESS) {
     1157                    result = QString::fromUtf16(value);
     1158                    UniFreeMem(value);
     1159                }
     1160            }
     1161        }
     1162
     1163        UniFreeLocaleObject(lo);
     1164
     1165        // Note QLocalePrivate::updateSystemPrivate() doesn't like empty
     1166        // strings, make sure we return a null QVariant in such case
     1167        if (!result.isEmpty())
     1168            return result;
     1169    }
     1170
     1171    return QVariant();
    9351172}
    9361173
  • trunk/src/corelib/tools/qstring.cpp

    r567 r622  
    46324632        return 0;
    46334633    }
    4634 #elif defined(Q_OS_OS2) && !defined(QT_NO_TEXTCODEC)
     4634#elif defined(Q_OS_OS2)
     4635    extern Q_CORE_EXPORT QString qt_system_locale_name(); // qlocale.cpp
     4636    // UniCreateLocaleObject("") creates an "en_US" locale when LANG is not set
     4637    // but we want to get the system locale so always use the exact name as
     4638    // returned by qt_system_locale_name() (it also takes LANG into account)
    46354639#if !defined(QT_NO_THREAD)
    46364640    static QAtomicPointer<void> localeObj;
    46374641    if (!localeObj) {
    46384642        LocaleObject lo = 0;
    4639         UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &lo);
     4643        UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
     4644                              qt_system_locale_name().utf16(), &lo);
    46404645        if (!localeObj.testAndSetRelaxed(0, lo))
    46414646            UniFreeLocaleObject(lo); // we are too late
     
    46444649    static LocaleObject localeObj = 0;
    46454650    if (!localeObj)
    4646         UniCreateLocaleObject(UNI_UCS_STRING_POINTER, (UniChar *)L"", &localeObj);
     4651        UniCreateLocaleObject(UNI_UCS_STRING_POINTER,
     4652                              qt_system_locale_name().utf16(), &localeObj);
    46474653#endif
    46484654    if (localeObj) {
Note: See TracChangeset for help on using the changeset viewer.