Android: Add support for getting the UiLanguages

From API 24 it is possible to get the UiLanguages correctly from
Android so if API 24 or later is available we should use this. If it is
not available, then it will fallback to the original behavior of using
the system language.

Fixes: QTBUG-68019
Change-Id: I4cfbc2b807b361c08da56a74100ba59abf5f2d0f
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Andy Shaw 2019-02-11 12:50:03 +01:00
parent 11111c5a7d
commit 8796e3016f

View File

@ -40,6 +40,7 @@
#include "qandroidsystemlocale.h"
#include "androidjnimain.h"
#include <QtCore/private/qjni_p.h>
#include <QtCore/private/qjnihelpers_p.h>
#include "qdatetime.h"
#include "qstringlist.h"
#include "qvariant.h"
@ -162,6 +163,23 @@ QVariant QAndroidSystemLocale::query(QueryType type, QVariant in) const
return m_locale.createSeparatedList(in.value<QStringList>());
case LocaleChanged:
Q_ASSERT_X(false, Q_FUNC_INFO, "This can't happen.");
case UILanguages: {
if (QtAndroidPrivate::androidSdkVersion() >= 24) {
QJNIObjectPrivate localeListObject =
QJNIObjectPrivate::callStaticObjectMethod("android/os/LocaleList", "getDefault",
"()Landroid/os/LocaleList;");
if (localeListObject.isValid()) {
QString lang = localeListObject.callObjectMethod("toLanguageTags",
"()Ljava/lang/String;").toString();
// Some devices return with it enclosed in []'s so check if both exists before
// removing to ensure it is formatted correctly
if (lang.startsWith(QChar('[')) && lang.endsWith(QChar(']')))
lang = lang.mid(1, lang.length() - 2);
return lang.split(QChar(','));
}
}
return QVariant();
}
default:
break;
}