Include likely-adjusted uiLanguages for the system locale

QLocale::uiLanguages() on the system locale uses whatever the system
locale's query(QSystemLocale::UILanguages,...) returns. On Android,
this is just a list of locales. However, for non-system locales, we
also include some results of removing likely sub-tags from the locale
name, where equivalent. Thus zh-CN would also get zh and zh-Hans-CN
added to it; however, if the system locale is zh-Hans-CN, the shorter
forms are omitted. So post-process the system locale list in the same
way, albeit tweaked to avoid duplicates and rearranged so that we can
insert likely-adjusted entries between what they adjust and what
followed it.

Added QLocalePrivate::rawName() in the process, since it looks likely
to be useful in other contexts (and I needed its value): it just joins
such tags as are non-Any. This, however, uses QByteArrayList, so added
that (it's small) to the bootstrap library and qmake.

This follows up on commit 8796e3016fae1672e727e2fa4e48f671a0c667ba.

[ChangeLog][QtCore][QLocale] The system locale's UI languages list now
includes, as for that of an ordinary locale, the results of adding
likely sub-tags from each locale name, and of removing some, where
this doesn't change which locale is specified. This gives searches for
translation files a better chance of finding a suitable file.

Fixes: QTBUG-75413
Change-Id: Iaafd79aac6a0fdd5f44aed16e445e84a2267c9da
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit 0118e2e9151ae3e1e1cd72f24e8c129eaa69dc4f)
This commit is contained in:
Edward Welbourne 2019-04-29 12:33:55 +02:00
parent f2e9505aa1
commit 1fd2124de7
5 changed files with 75 additions and 18 deletions

View File

@ -27,7 +27,7 @@ QOBJS = \
qjsonarray.o qjson.o qjsondocument.o qjsonobject.o qjsonparser.o qjsonvalue.o \ qjsonarray.o qjson.o qjsondocument.o qjsonobject.o qjsonparser.o qjsonvalue.o \
qmetatype.o qsystemerror.o qvariant.o \ qmetatype.o qsystemerror.o qvariant.o \
quuid.o \ quuid.o \
qarraydata.o qbitarray.o qbytearray.o qbytearraymatcher.o \ qarraydata.o qbitarray.o qbytearray.o qbytearraylist.o qbytearraymatcher.o \
qcryptographichash.o qdatetime.o qhash.o qlinkedlist.o qlist.o \ qcryptographichash.o qdatetime.o qhash.o qlinkedlist.o qlist.o \
qlocale.o qlocale_tools.o qmap.o qregexp.o qringbuffer.o \ qlocale.o qlocale_tools.o qmap.o qregexp.o qringbuffer.o \
qstringbuilder.o qstring_compat.o qstring.o qstringlist.o qversionnumber.o \ qstringbuilder.o qstring_compat.o qstring.o qstringlist.o qversionnumber.o \
@ -108,6 +108,7 @@ DEPEND_SRC = \
$(SOURCE_PATH)/src/corelib/tools/qarraydata.cpp \ $(SOURCE_PATH)/src/corelib/tools/qarraydata.cpp \
$(SOURCE_PATH)/src/corelib/tools/qbitarray.cpp \ $(SOURCE_PATH)/src/corelib/tools/qbitarray.cpp \
$(SOURCE_PATH)/src/corelib/tools/qbytearray.cpp\ $(SOURCE_PATH)/src/corelib/tools/qbytearray.cpp\
$(SOURCE_PATH)/src/corelib/tools/qbytearraylist.cpp \
$(SOURCE_PATH)/src/corelib/tools/qbytearraymatcher.cpp \ $(SOURCE_PATH)/src/corelib/tools/qbytearraymatcher.cpp \
$(SOURCE_PATH)/src/corelib/tools/qcryptographichash.cpp \ $(SOURCE_PATH)/src/corelib/tools/qcryptographichash.cpp \
$(SOURCE_PATH)/src/corelib/tools/qdatetime.cpp \ $(SOURCE_PATH)/src/corelib/tools/qdatetime.cpp \
@ -307,6 +308,9 @@ qarraydata.o: $(SOURCE_PATH)/src/corelib/tools/qarraydata.cpp
qbytearray.o: $(SOURCE_PATH)/src/corelib/tools/qbytearray.cpp qbytearray.o: $(SOURCE_PATH)/src/corelib/tools/qbytearray.cpp
$(CXX) -c -o $@ $(CXXFLAGS) $< $(CXX) -c -o $@ $(CXXFLAGS) $<
qbytearraylist.o: $(SOURCE_PATH)/src/corelib/tools/qbytearraylist.cpp
$(CXX) -c -o $@ $(CXXFLAGS) $<
qvsnprintf.o: $(SOURCE_PATH)/src/corelib/tools/qvsnprintf.cpp qvsnprintf.o: $(SOURCE_PATH)/src/corelib/tools/qvsnprintf.cpp
$(CXX) -c -o $@ $(CXXFLAGS) $< $(CXX) -c -o $@ $(CXXFLAGS) $<

View File

@ -71,6 +71,7 @@ QTOBJS= \
qfsfileengine_iterator.obj \ qfsfileengine_iterator.obj \
qarraydata.obj \ qarraydata.obj \
qbytearray.obj \ qbytearray.obj \
qbytearraylist.obj \
qvsnprintf.obj \ qvsnprintf.obj \
qbytearraymatcher.obj \ qbytearraymatcher.obj \
qdatetime.obj \ qdatetime.obj \

View File

@ -339,6 +339,22 @@ QByteArray QLocalePrivate::bcp47Name(char separator) const
return localeId.withLikelySubtagsRemoved().name(separator); return localeId.withLikelySubtagsRemoved().name(separator);
} }
/*!
\internal
*/
QByteArray QLocalePrivate::rawName(char separator) const
{
QByteArrayList parts;
if (m_data->m_language_id != QLocale::AnyLanguage)
parts.append(languageCode().latin1());
if (m_data->m_script_id != QLocale::AnyScript)
parts.append(scriptCode().latin1());
if (m_data->m_country_id != QLocale::AnyCountry)
parts.append(countryCode().latin1());
return parts.join(separator);
}
static const QLocaleData *findLocaleDataById(const QLocaleId &localeId) static const QLocaleData *findLocaleDataById(const QLocaleId &localeId)
{ {
const uint idx = locale_index[localeId.language_id]; const uint idx = locale_index[localeId.language_id];
@ -3973,29 +3989,63 @@ QString QLocale::formattedDataSize(qint64 bytes, int precision, DataSizeFormats
*/ */
QStringList QLocale::uiLanguages() const QStringList QLocale::uiLanguages() const
{ {
QStringList uiLanguages;
QVector<QLocale> locales;
#ifndef QT_NO_SYSTEMLOCALE #ifndef QT_NO_SYSTEMLOCALE
if (d->m_data == systemData()) { if (d->m_data == systemData()) {
QVariant res = systemLocale()->query(QSystemLocale::UILanguages, QVariant()); QVariant res = systemLocale()->query(QSystemLocale::UILanguages, QVariant());
if (!res.isNull()) { if (!res.isNull()) {
QStringList result = res.toStringList(); uiLanguages = res.toStringList();
if (!result.isEmpty()) // ... but we need to include likely-adjusted forms of each of those, too:
return result; for (const auto entry : qAsConst(uiLanguages))
locales.append(QLocale(entry));
} }
} } else
#endif #endif
QLocaleId id = QLocaleId::fromIds(d->m_data->m_language_id, d->m_data->m_script_id, d->m_data->m_country_id); {
const QLocaleId max = id.withLikelySubtagsAdded(); locales.append(*this);
const QLocaleId min = max.withLikelySubtagsRemoved(); }
for (int i = locales.size(); i-- > 0; ) {
QStringList uiLanguages; const QLocale &locale = locales.at(i);
uiLanguages.append(QString::fromLatin1(min.name())); int j;
if (id.script_id) { QByteArray prior;
id.script_id = 0; if (i < uiLanguages.size()) {
if (id != min && id.withLikelySubtagsAdded() == max) // Adding likely-adjusted forms to system locale's list.
uiLanguages.append(QString::fromLatin1(id.name())); // Name the locale is derived from:
const QString &name = uiLanguages.at(i);
prior = name.toLatin1();
// Don't try to likely-adjust if construction's likely-adjustments
// were so drastic the result doesn't match the prior name:
if (locale.name() != name && locale.d->rawName() != prior)
continue;
// Insert just after prior:
j = i + 1;
} else {
// Plain locale, not system locale; just append.
j = uiLanguages.size();
}
const auto data = locale.d->m_data;
QLocaleId id
= QLocaleId::fromIds(data->m_language_id, data->m_script_id, data->m_country_id);
const QLocaleId max = id.withLikelySubtagsAdded();
const QLocaleId min = max.withLikelySubtagsRemoved();
id.script_id = 0; // For re-use as script-less variant.
// Include version with all likely sub-tags (last) if distinct from the rest:
if (max != min && max != id && max.name() != prior)
uiLanguages.insert(j, QString::fromLatin1(max.name()));
// Include scriptless version if likely-equivalent and distinct:
if (data->m_script_id && id != min && id.name() != prior
&& id.withLikelySubtagsAdded() == max) {
uiLanguages.insert(j, QString::fromLatin1(id.name()));
}
// Include minimal version (first) unless it's what our locale is derived from:
if (min.name() != prior)
uiLanguages.insert(j, QString::fromLatin1(min.name()));
} }
if (max != min && max != id)
uiLanguages.append(QString::fromLatin1(max.name()));
return uiLanguages; return uiLanguages;
} }

View File

@ -355,6 +355,7 @@ public:
quint16 countryId() const { return m_data->m_country_id; } quint16 countryId() const { return m_data->m_country_id; }
QByteArray bcp47Name(char separator = '-') const; QByteArray bcp47Name(char separator = '-') const;
QByteArray rawName(char separator = '-') const;
inline QLatin1String languageCode() const { return QLocalePrivate::languageToCode(QLocale::Language(m_data->m_language_id)); } inline QLatin1String languageCode() const { return QLocalePrivate::languageToCode(QLocale::Language(m_data->m_language_id)); }
inline QLatin1String scriptCode() const { return QLocalePrivate::scriptToCode(QLocale::Script(m_data->m_script_id)); } inline QLatin1String scriptCode() const { return QLocalePrivate::scriptToCode(QLocale::Script(m_data->m_script_id)); }

View File

@ -70,8 +70,9 @@ SOURCES += \
../../corelib/serialization/qxmlutils.cpp \ ../../corelib/serialization/qxmlutils.cpp \
../../corelib/serialization/qxmlstream.cpp \ ../../corelib/serialization/qxmlstream.cpp \
../../corelib/tools/qbitarray.cpp \ ../../corelib/tools/qbitarray.cpp \
../../corelib/tools/qbytearray.cpp \
../../corelib/tools/qarraydata.cpp \ ../../corelib/tools/qarraydata.cpp \
../../corelib/tools/qbytearray.cpp \
../../corelib/tools/qbytearraylist.cpp \
../../corelib/tools/qbytearraymatcher.cpp \ ../../corelib/tools/qbytearraymatcher.cpp \
../../corelib/tools/qcommandlineparser.cpp \ ../../corelib/tools/qcommandlineparser.cpp \
../../corelib/tools/qcommandlineoption.cpp \ ../../corelib/tools/qcommandlineoption.cpp \