Skip to content

Commit

Permalink
Android: Add support for getting the UiLanguages
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
AndyShawQt committed Feb 19, 2019
1 parent 11111c5 commit 8796e30
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/plugins/platforms/android/qandroidsystemlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 8796e30

Please sign in to comment.