QLocale: Allow direct conversion from language, country, and script codes

This complements patch a148c7b5d71d244, where languageToCode(),
countryToCode() scriptToCode() methods were introduced, with matching
codeToLanguage(), codeToCountry(), and codeToScript() methods.

This allows us to remove the use of private Qt Core API in Qt Linguist.

[ChangeLog][QtCore][QLocale] Added static codeToLanguage(),
codeToCountry(), codeToScript() methods that convert ISO code strings
to the respective enum values.

Change-Id: If5c0843a718c006ade086a6f74ceb86ac6e0fce4
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Kai Köhne 2021-01-22 10:36:20 +01:00
parent aa3b42d634
commit e7c028bb9c
4 changed files with 78 additions and 7 deletions

View File

@ -102,7 +102,7 @@ QLocale::Language QLocalePrivate::codeToLanguage(QStringView code) noexcept
{ {
const auto len = code.size(); const auto len = code.size();
if (len != 2 && len != 3) if (len != 2 && len != 3)
return QLocale::C; return QLocale::AnyLanguage;
char16_t uc1 = code[0].toLower().unicode(); char16_t uc1 = code[0].toLower().unicode();
char16_t uc2 = code[1].toLower().unicode(); char16_t uc2 = code[1].toLower().unicode();
char16_t uc3 = len > 2 ? code[2].toLower().unicode() : 0; char16_t uc3 = len > 2 ? code[2].toLower().unicode() : 0;
@ -131,7 +131,7 @@ QLocale::Language QLocalePrivate::codeToLanguage(QStringView code) noexcept
if (uc1 == 'j' && uc2 == 'i') // ji -> yi if (uc1 == 'j' && uc2 == 'i') // ji -> yi
return QLocale::Yiddish; return QLocale::Yiddish;
} }
return QLocale::C; return QLocale::AnyLanguage;
} }
QLocale::Script QLocalePrivate::codeToScript(QStringView code) noexcept QLocale::Script QLocalePrivate::codeToScript(QStringView code) noexcept
@ -574,8 +574,8 @@ QLocaleId QLocaleId::fromName(const QString &name)
return { QLocale::C, 0, 0 }; return { QLocale::C, 0, 0 };
QLocale::Language langId = QLocalePrivate::codeToLanguage(lang); QLocale::Language langId = QLocalePrivate::codeToLanguage(lang);
if (langId == QLocale::C) if (langId == QLocale::AnyLanguage)
return QLocaleId { langId, 0, 0 }; return { QLocale::C, 0, 0 };
return { langId, QLocalePrivate::codeToScript(script), QLocalePrivate::codeToCountry(land) }; return { langId, QLocalePrivate::codeToScript(script), QLocalePrivate::codeToCountry(land) };
} }
@ -1353,13 +1353,27 @@ QString QLocale::bcp47Name() const
For \c QLocale::AnyLanguage an empty string is returned. For \c QLocale::AnyLanguage an empty string is returned.
\since 6.1 \since 6.1
\sa language(), name(), bcp47Name(), countryToCode(), scriptToCode() \sa codeToLanguage(), language(), name(), bcp47Name(), countryToCode(), scriptToCode()
*/ */
QString QLocale::languageToCode(Language language) QString QLocale::languageToCode(Language language)
{ {
return QLocalePrivate::languageToCode(language); return QLocalePrivate::languageToCode(language);
} }
/*!
Returns the QLocale::Language enum corresponding to the two- or three-letter
\a languageCode, as defined in the ISO 639 standards.
If the code is invalid or not known QLocale::AnyLanguage is returned.
\since 6.1
\sa languageToCode(), codeToCountry(), codeToScript()
*/
QLocale::Language QLocale::codeToLanguage(QStringView languageCode) noexcept
{
return QLocalePrivate::codeToLanguage(languageCode);
}
/*! /*!
Returns the two-letter country code for \a country, as defined Returns the two-letter country code for \a country, as defined
in the ISO 3166 standard. in the ISO 3166 standard.
@ -1367,13 +1381,27 @@ QString QLocale::languageToCode(Language language)
\note For \c{QLocale::AnyCountry} an empty string is returned. \note For \c{QLocale::AnyCountry} an empty string is returned.
\since 6.1 \since 6.1
\sa country(), name(), bcp47Name(), languageToCode(), scriptToCode() \sa codeToCountry(), country(), name(), bcp47Name(), languageToCode(), scriptToCode()
*/ */
QString QLocale::countryToCode(Country country) QString QLocale::countryToCode(Country country)
{ {
return QLocalePrivate::countryToCode(country); return QLocalePrivate::countryToCode(country);
} }
/*!
Returns the QLocale::Country enum corresponding to the two-letter or
three-digit \a countryCode, as defined in the ISO 3166 standard.
If the code is invalid or not known QLocale::AnyCountry is returned.
\since 6.1
\sa countryToCode(), codeToLanguage(), codeToScript()
*/
QLocale::Country QLocale::codeToCountry(QStringView countryCode) noexcept
{
return QLocalePrivate::codeToCountry(countryCode);
}
/*! /*!
Returns the four-letter script code for \a script, as defined in the Returns the four-letter script code for \a script, as defined in the
ISO 15924 standard. ISO 15924 standard.
@ -1388,6 +1416,20 @@ QString QLocale::scriptToCode(Script script)
return QLocalePrivate::scriptToCode(script); return QLocalePrivate::scriptToCode(script);
} }
/*!
Returns the QLocale::Script enum corresponding to the four-letter script
\a scriptCode, as defined in the ISO 15924 standard.
If the code is invalid or not known QLocale::AnyScript is returned.
\since 6.1
\sa scriptToCode(), codeToLanguage(), codeToCountry()
*/
QLocale::Script QLocale::codeToScript(QStringView scriptCode) noexcept
{
return QLocalePrivate::codeToScript(scriptCode);
}
/*! /*!
Returns a QString containing the name of \a language. Returns a QString containing the name of \a language.

View File

@ -1068,8 +1068,11 @@ public:
QStringList uiLanguages() const; QStringList uiLanguages() const;
static QString languageToCode(Language language); static QString languageToCode(Language language);
static Language codeToLanguage(QStringView languageCode) noexcept;
static QString countryToCode(Country country); static QString countryToCode(Country country);
static Country codeToCountry(QStringView countryCode) noexcept;
static QString scriptToCode(Script script); static QString scriptToCode(Script script);
static Script codeToScript(QStringView scriptCode) noexcept;
static QString languageToString(Language language); static QString languageToString(Language language);
static QString countryToString(Country country); static QString countryToString(Country country);

View File

@ -148,7 +148,6 @@ namespace QIcu {
struct QLocaleId struct QLocaleId
{ {
// ### Also used by Translator::languageAndCountry() in qttools:
Q_CORE_EXPORT static QLocaleId fromName(const QString &name); Q_CORE_EXPORT static QLocaleId fromName(const QString &name);
inline bool operator==(QLocaleId other) const inline bool operator==(QLocaleId other) const
{ return language_id == other.language_id && script_id == other.script_id && country_id == other.country_id; } { return language_id == other.language_id && script_id == other.script_id && country_id == other.country_id; }

View File

@ -151,6 +151,7 @@ private slots:
void numberFormatChakma(); void numberFormatChakma();
void lcsToCode(); void lcsToCode();
void codeToLcs();
// *** ORDER-DEPENDENCY *** (This Is Bad.) // *** ORDER-DEPENDENCY *** (This Is Bad.)
// Test order is determined by order of declaration here: *all* tests that // Test order is determined by order of declaration here: *all* tests that
@ -3240,5 +3241,31 @@ void tst_QLocale::lcsToCode()
QCOMPARE(QLocale::scriptToCode(QLocale::SimplifiedHanScript), QString("Hans")); QCOMPARE(QLocale::scriptToCode(QLocale::SimplifiedHanScript), QString("Hans"));
} }
void tst_QLocale::codeToLcs()
{
QCOMPARE(QLocale::codeToLanguage(QString()), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString(" ")), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString("und")), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString("e")), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString("en")), QLocale::English);
QCOMPARE(QLocale::codeToLanguage(QString("EN")), QLocale::English);
QCOMPARE(QLocale::codeToLanguage(QString("eng")), QLocale::AnyLanguage);
QCOMPARE(QLocale::codeToLanguage(QString("ha")), QLocale::Hausa);
QCOMPARE(QLocale::codeToLanguage(QString("haw")), QLocale::Hawaiian);
QCOMPARE(QLocale::codeToCountry(QString()), QLocale::AnyCountry);
QCOMPARE(QLocale::codeToCountry(QString("ZZ")), QLocale::AnyCountry);
QCOMPARE(QLocale::codeToCountry(QString("US")), QLocale::UnitedStates);
QCOMPARE(QLocale::codeToCountry(QString("us")), QLocale::UnitedStates);
QCOMPARE(QLocale::codeToCountry(QString("USA")), QLocale::AnyCountry);
QCOMPARE(QLocale::codeToCountry(QString("EU")), QLocale::EuropeanUnion);
QCOMPARE(QLocale::codeToCountry(QString("001")), QLocale::World);
QCOMPARE(QLocale::codeToCountry(QString("150")), QLocale::Europe);
QCOMPARE(QLocale::codeToScript(QString()), QLocale::AnyScript);
QCOMPARE(QLocale::codeToScript(QString("Zzzz")), QLocale::AnyScript);
QCOMPARE(QLocale::codeToScript(QString("Hans")), QLocale::SimplifiedHanScript);
}
QTEST_MAIN(tst_QLocale) QTEST_MAIN(tst_QLocale)
#include "tst_qlocale.moc" #include "tst_qlocale.moc"