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>
This commit is contained in:
parent
2d8757f879
commit
f1465c621c
@ -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;
|
||||
@ -1345,6 +1345,49 @@ QString QLocale::bcp47Name() const
|
||||
return QString::fromLatin1(d->bcp47Name());
|
||||
}
|
||||
|
||||
/*!
|
||||
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.
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user