summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKai Köhne <kai.koehne@qt.io>2020-12-11 16:40:37 +0100
committerKai Köhne <kai.koehne@qt.io>2021-01-18 15:52:31 +0100
commitf1465c621c11712991fabe5b8b81ccbb6eea4bb9 (patch)
tree638ea8c9f7e1cf9989736db30ad411a67939c815
parent2d8757f879d8f410319aae41ff902ba5a679d9dd (diff)
QLocale: Allow direct conversion to language, country, and script codes
Currently the codes are only exposed in aggregated form, i.e. through name(), bcp47Name(). There are use cases though where you are only interested in either language, country, or script codes. One example is in Qt Linguist. This patch therefore exposes the static languageToCode(), countryToCode(), scriptToCode() methods that were so far only available in the private API also in the public API. [ChangeLog][QtCore][QLocale] Added static languageToCode(), countryToCode() scriptToCode() methods that convert enum values to the respective ISO code strings. Fixes: QTBUG-39542 Fixes: QTBUG-64942 Change-Id: Ib1d5c3293e2f53245ba4c1fc8159275bcb290080 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r--src/corelib/text/qlocale.cpp47
-rw-r--r--src/corelib/text/qlocale.h4
-rw-r--r--tests/auto/corelib/text/qlocale/tst_qlocale.cpp16
3 files changed, 65 insertions, 2 deletions
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index aabdb3b213..faa49ee4bc 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -175,7 +175,7 @@ QLocale::Country QLocalePrivate::codeToCountry(QStringView code) noexcept
QLatin1String QLocalePrivate::languageToCode(QLocale::Language language)
{
- if (language == QLocale::AnyLanguage)
+ if (language == QLocale::AnyLanguage || language > QLocale::LastLanguage)
return QLatin1String();
if (language == QLocale::C)
return QLatin1String("C");
@@ -194,7 +194,7 @@ QLatin1String QLocalePrivate::scriptToCode(QLocale::Script script)
QLatin1String QLocalePrivate::countryToCode(QLocale::Country country)
{
- if (country == QLocale::AnyCountry)
+ if (country == QLocale::AnyCountry || country > QLocale::LastCountry)
return QLatin1String();
const unsigned char *c = country_code_list + 3 * country;
@@ -1346,6 +1346,49 @@ QString QLocale::bcp47Name() const
}
/*!
+ Returns the two- or three-letter language code for \a language, as defined
+ in the ISO 639 standards.
+
+ \note For \c{QLocale::C} the function returns \c{"C"}.
+ For \c QLocale::AnyLanguage an empty string is returned.
+
+ \since 6.1
+ \sa language(), name(), bcp47Name(), countryToCode(), scriptToCode()
+*/
+QString QLocale::languageToCode(Language language)
+{
+ return QLocalePrivate::languageToCode(language);
+}
+
+/*!
+ Returns the two-letter country code for \a country, as defined
+ in the ISO 3166 standard.
+
+ \note For \c{QLocale::AnyCountry} an empty string is returned.
+
+ \since 6.1
+ \sa country(), name(), bcp47Name(), languageToCode(), scriptToCode()
+*/
+QString QLocale::countryToCode(Country country)
+{
+ return QLocalePrivate::countryToCode(country);
+}
+
+/*!
+ Returns the four-letter script code for \a script, as defined in the
+ ISO 15924 standard.
+
+ \note For \c{QLocale::AnyScript} an empty string is returned.
+
+ \since 6.1
+ \sa script(), name(), bcp47Name(), languageToCode(), countryToCode()
+*/
+QString QLocale::scriptToCode(Script script)
+{
+ return QLocalePrivate::scriptToCode(script);
+}
+
+/*!
Returns a QString containing the name of \a language.
\sa countryToString(), scriptToString(), bcp47Name()
diff --git a/src/corelib/text/qlocale.h b/src/corelib/text/qlocale.h
index 2f9070bdd1..a05196946f 100644
--- a/src/corelib/text/qlocale.h
+++ b/src/corelib/text/qlocale.h
@@ -1067,6 +1067,10 @@ public:
QStringList uiLanguages() const;
+ static QString languageToCode(Language language);
+ static QString countryToCode(Country country);
+ static QString scriptToCode(Script script);
+
static QString languageToString(Language language);
static QString countryToString(Country country);
static QString scriptToString(Script script);
diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
index 327cb7d714..04c54b903b 100644
--- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
+++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp
@@ -150,6 +150,8 @@ private slots:
void numberGroupingIndia();
void numberFormatChakma();
+ void lcsToCode();
+
// *** ORDER-DEPENDENCY *** (This Is Bad.)
// Test order is determined by order of declaration here: *all* tests that
// QLocale::setDefault() *must* appear *after* all other tests !
@@ -3224,5 +3226,19 @@ void tst_QLocale::numberFormatChakma()
QCOMPARE(chakma.toULongLong(strResult64), uint64);
}
+void tst_QLocale::lcsToCode()
+{
+ QCOMPARE(QLocale::languageToCode(QLocale::AnyLanguage), QString());
+ QCOMPARE(QLocale::languageToCode(QLocale::C), QString("C"));
+ QCOMPARE(QLocale::languageToCode(QLocale::English), QString("en"));
+
+ QCOMPARE(QLocale::countryToCode(QLocale::AnyCountry), QString());
+ QCOMPARE(QLocale::countryToCode(QLocale::UnitedStates), QString("US"));
+ QCOMPARE(QLocale::countryToCode(QLocale::EuropeanUnion), QString("EU"));
+
+ QCOMPARE(QLocale::scriptToCode(QLocale::AnyScript), QString());
+ QCOMPARE(QLocale::scriptToCode(QLocale::SimplifiedHanScript), QString("Hans"));
+}
+
QTEST_MAIN(tst_QLocale)
#include "tst_qlocale.moc"