diff options
author | Konstantin Ritt <ritt.ks@gmail.com> | 2012-11-24 04:09:34 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-11-25 04:06:51 +0100 |
commit | 9b85b81bc82f4e615b5d165425dc9f84fd2ae482 (patch) | |
tree | f41d10600f9688690153a7332cf170e820ee9a63 /src/corelib | |
parent | 4ac521354dfa575c31ecb732a0f8c1e18827c21e (diff) |
QLocale: Fix matchingLocales() behavior if script or country is not Any*
Since the documentation doesn't mention the search should be done by language only,
consider the current behavior incorrect.
As of now, it is possible to get a list of locales by Country or Script as well.
Also fix countriesForLanguage() to be in-sync with matchingLocales().
Change-Id: I6a09ca459120143565fa6099d2b823df1fed7c25
Reviewed-by: Denis Dzyubenko <denis@ddenis.info>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index f116dab401..4b4c94872e 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -1911,6 +1911,9 @@ QLocale QLocale::system() Getting a list of all locales: QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); + + Getting a list of locales suitable for Russia: + QList<QLocale> locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::Russia); */ QList<QLocale> QLocale::matchingLocales(QLocale::Language language, QLocale::Script script, @@ -1920,16 +1923,20 @@ QList<QLocale> QLocale::matchingLocales(QLocale::Language language, uint(country) > QLocale::LastCountry) return QList<QLocale>(); + if (language == QLocale::C) + return QList<QLocale>() << QLocale(QLocale::C); + QList<QLocale> result; - const QLocaleData *data = locale_data; if (language == QLocale::AnyLanguage && script == QLocale::AnyScript && country == QLocale::AnyCountry) result.reserve(locale_data_size); - if (language != QLocale::C) - data += locale_index[language]; + const QLocaleData *data = locale_data + locale_index[language]; while ( (data != locale_data + locale_data_size) && (language == QLocale::AnyLanguage || data->m_language_id == uint(language))) { - QLocale locale(*new QLocalePrivate(localeDataIndex(data))); - result.append(locale); + if ((script == QLocale::AnyScript || data->m_script_id == uint(script)) + && (country == QLocale::AnyCountry || data->m_country_id == uint(country))) { + QLocale locale(*new QLocalePrivate(localeDataIndex(data))); + result.append(locale); + } ++data; } return result; @@ -1939,7 +1946,7 @@ QList<QLocale> QLocale::matchingLocales(QLocale::Language language, \obsolete \since 4.3 - Returns the list of countries that have entires for \a language in Qt's locale + Returns the list of countries that have entries for \a language in Qt's locale database. If the result is an empty list, then \a language is not represented in Qt's locale database. @@ -1948,19 +1955,17 @@ QList<QLocale> QLocale::matchingLocales(QLocale::Language language, QList<QLocale::Country> QLocale::countriesForLanguage(Language language) { QList<Country> result; - - unsigned language_id = language; - uint idx = locale_index[language_id]; - if (language == C) { result << AnyCountry; return result; } - const QLocaleData *data = locale_data + idx; - + unsigned language_id = language; + const QLocaleData *data = locale_data + locale_index[language_id]; while (data->m_language_id == language_id) { - result << static_cast<Country>(data->m_country_id); + const QLocale::Country country = static_cast<Country>(data->m_country_id); + if (!result.contains(country)) + result.append(country); ++data; } |