diff options
author | Liang Qi <liang.qi@nokia.com> | 2011-03-08 16:32:24 (GMT) |
---|---|---|
committer | Liang Qi <liang.qi@nokia.com> | 2011-03-08 16:32:24 (GMT) |
commit | 09fe2cd62df80373e602d47c570ae317a60a5293 (patch) | |
tree | 494689ed4f7ae7d75d938d851a18f382eb6ef464 /src/corelib/tools | |
parent | dc2280dbcd27d3ff9cbcb230943cc1036e8a2761 (diff) | |
download | Qt-09fe2cd62df80373e602d47c570ae317a60a5293.zip Qt-09fe2cd62df80373e602d47c570ae317a60a5293.tar.gz Qt-09fe2cd62df80373e602d47c570ae317a60a5293.tar.bz2 |
Using list patterns to get a string from list in QLocale.
Reviewed-by: Denis Dzyubenko
Task-number: QTBUG-17097
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 39 | ||||
-rw-r--r-- | src/corelib/tools/qlocale.h | 4 | ||||
-rw-r--r-- | src/corelib/tools/qlocale_p.h | 4 | ||||
-rw-r--r-- | src/corelib/tools/qlocale_unix.cpp | 2 |
4 files changed, 48 insertions, 1 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index 58ac867..c3d15d8 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -820,6 +820,45 @@ QString QLocale::quoteString(const QStringRef &str, QuotationStyle style) const } /*! + \since 4.8 + + Returns a string according to the current locale. +*/ +QString QLocale::createSeparatedList(const QStringList &strl) const +{ +#ifndef QT_NO_SYSTEMLOCALE + if (d() == systemPrivate()) { + QVariant res; + res = systemLocale()->query(QSystemLocale::ListToSeparatedString, QVariant::fromValue(strl)); + + if (!res.isNull()) + return res.toString(); + } +#endif + + const int size = strl.size(); + if (size == 1) + return strl.at(0); + else if (size == 2) { + QString format = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_two_idx, d()->m_list_pattern_part_two_size); + return format.arg(strl.at(0), strl.at(1)); + } + else if (size > 2) { + QString formatStart = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_start_idx, d()->m_list_pattern_part_start_size); + QString formatMid = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_mid_idx, d()->m_list_pattern_part_mid_size); + QString formatEnd = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_end_idx, d()->m_list_pattern_part_end_size); + QString result = formatStart.arg(strl.at(0), strl.at(1)); + int i; + for (i = 2; i < size - 1; ++i) + result = formatMid.arg(result, strl.at(i)); + result = formatEnd.arg(result, strl.at(size - 1)); + return result; + } + + return QString(); +} + +/*! \nonreentrant Sets the global default locale to \a locale. These diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h index d5c41d0..4cee162 100644 --- a/src/corelib/tools/qlocale.h +++ b/src/corelib/tools/qlocale.h @@ -113,7 +113,8 @@ public: UILanguages, // QStringList StringToStandardQuotation, // QString in: QStringRef to quote StringToAlternateQuotation, // QString in: QStringRef to quote - ScriptId // uint + ScriptId, // uint + ListToSeparatedString // QString }; virtual QVariant query(QueryType type, QVariant in) const; virtual QLocale fallbackLocale() const; @@ -751,6 +752,7 @@ public: QString quoteString(const QString &str, QuotationStyle style = StandardQuotation) const; QString quoteString(const QStringRef &str, QuotationStyle style = StandardQuotation) const; + QString createSeparatedList(const QStringList &strl) const; //private: // this should be private, but can't be struct Data { quint16 index; diff --git a/src/corelib/tools/qlocale_p.h b/src/corelib/tools/qlocale_p.h index 1f73982..ab7fde6 100644 --- a/src/corelib/tools/qlocale_p.h +++ b/src/corelib/tools/qlocale_p.h @@ -180,6 +180,10 @@ public: quint16 m_quotation_start, m_quotation_end; quint16 m_alternate_quotation_start, m_alternate_quotation_end; + quint16 m_list_pattern_part_start_idx, m_list_pattern_part_start_size; + quint16 m_list_pattern_part_mid_idx, m_list_pattern_part_mid_size; + quint16 m_list_pattern_part_end_idx, m_list_pattern_part_end_size; + quint16 m_list_pattern_part_two_idx, m_list_pattern_part_two_size; quint16 m_short_date_format_idx, m_short_date_format_size; quint16 m_long_date_format_idx, m_long_date_format_size; quint16 m_short_time_format_idx, m_short_time_format_size; diff --git a/src/corelib/tools/qlocale_unix.cpp b/src/corelib/tools/qlocale_unix.cpp index 3701b2b..117e2e4 100644 --- a/src/corelib/tools/qlocale_unix.cpp +++ b/src/corelib/tools/qlocale_unix.cpp @@ -214,6 +214,8 @@ QVariant QSystemLocale::query(QueryType type, QVariant in) const return lc_messages.quoteString(in.value<QStringRef>()); case StringToAlternateQuotation: return lc_messages.quoteString(in.value<QStringRef>(), QLocale::AlternateQuotation); + case ListToSeparatedString: + return lc_messages.createSeparatedList(in.value<QStringList>()); default: break; } |