diff options
-rw-r--r-- | src/corelib/text/qlocale.cpp | 47 | ||||
-rw-r--r-- | src/corelib/text/qlocale.h | 4 | ||||
-rw-r--r-- | tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 16 |
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" |