Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qandroidsystemlocale.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "androidjnimain.h"
6#include "qdatetime.h"
7#include "qstringlist.h"
8#include "qvariant.h"
9
10#include <QtCore/private/qjnihelpers_p.h>
11#include <QtCore/QJniObject>
12
14
15Q_DECLARE_JNI_CLASS(Locale, "java/util/Locale")
16Q_DECLARE_JNI_CLASS(Resources, "android/content/res/Resources")
17Q_DECLARE_JNI_CLASS(Configuration, "android/content/res/Configuration")
18Q_DECLARE_JNI_CLASS(LocaleList, "android/os/LocaleList")
19
20using namespace QtJniTypes;
21
25
26void QAndroidSystemLocale::getLocaleFromJava() const
27{
28 const Locale javaLocaleObject = []{
29 const QJniObject javaContext = QtAndroidPrivate::context();
30 if (javaContext.isValid()) {
31 const QJniObject resources = javaContext.callMethod<Resources>("getResources");
32 const QJniObject configuration = resources.callMethod<Configuration>("getConfiguration");
33 return configuration.getField<Locale>("locale");
34 } else {
35 return Locale::callStaticMethod<Locale>("getDefault");
36 }
37 }();
38
39 const QString languageCode = javaLocaleObject.callMethod<QString>("getLanguage");
40 const QString countryCode = javaLocaleObject.callMethod<QString>("getCountry");
41
42 QWriteLocker locker(&m_lock);
43 m_locale = QLocale(languageCode + u'_' + countryCode);
44}
45
47{
48 if (type == LocaleChanged) {
49 getLocaleFromJava();
50 return QVariant();
51 }
52
53 QReadLocker locker(&m_lock);
54
55 switch (type) {
56 case DecimalPoint:
57 return m_locale.decimalPoint();
58 case GroupSeparator:
59 return m_locale.groupSeparator();
60 case ZeroDigit:
61 return m_locale.zeroDigit();
62 case NegativeSign:
63 return m_locale.negativeSign();
64 case DateFormatLong:
65 return m_locale.dateFormat(QLocale::LongFormat);
66 case DateFormatShort:
67 return m_locale.dateFormat(QLocale::ShortFormat);
68 case TimeFormatLong:
69 return m_locale.timeFormat(QLocale::LongFormat);
70 case TimeFormatShort:
71 return m_locale.timeFormat(QLocale::ShortFormat);
72 case DayNameLong:
73 return m_locale.dayName(in.toInt(), QLocale::LongFormat);
74 case DayNameShort:
75 return m_locale.dayName(in.toInt(), QLocale::ShortFormat);
76 case DayNameNarrow:
77 return m_locale.dayName(in.toInt(), QLocale::NarrowFormat);
79 return m_locale.standaloneDayName(in.toInt(), QLocale::LongFormat);
81 return m_locale.standaloneDayName(in.toInt(), QLocale::ShortFormat);
83 return m_locale.standaloneDayName(in.toInt(), QLocale::NarrowFormat);
84 case MonthNameLong:
85 return m_locale.monthName(in.toInt(), QLocale::LongFormat);
86 case MonthNameShort:
87 return m_locale.monthName(in.toInt(), QLocale::ShortFormat);
88 case MonthNameNarrow:
89 return m_locale.monthName(in.toInt(), QLocale::NarrowFormat);
91 return m_locale.standaloneMonthName(in.toInt(), QLocale::LongFormat);
93 return m_locale.standaloneMonthName(in.toInt(), QLocale::ShortFormat);
95 return m_locale.standaloneMonthName(in.toInt(), QLocale::NarrowFormat);
97 return m_locale.toString(in.toDate(), QLocale::LongFormat);
99 return m_locale.toString(in.toDate(), QLocale::ShortFormat);
100 case TimeToStringLong:
101 return m_locale.toString(in.toTime(), QLocale::LongFormat);
103 return m_locale.toString(in.toTime(), QLocale::ShortFormat);
105 return m_locale.dateTimeFormat(QLocale::LongFormat);
107 return m_locale.dateTimeFormat(QLocale::ShortFormat);
109 return m_locale.toString(in.toDateTime(), QLocale::LongFormat);
111 return m_locale.toString(in.toDateTime(), QLocale::ShortFormat);
112 case PositiveSign:
113 return m_locale.positiveSign();
114 case AMText:
115 return m_locale.amText();
116 case PMText:
117 return m_locale.pmText();
118 case FirstDayOfWeek:
119 return m_locale.firstDayOfWeek();
120 case CurrencySymbol:
121 return m_locale .currencySymbol(QLocale::CurrencySymbolFormat(in.toUInt()));
122 case CurrencyToString: {
123 switch (in.metaType().id()) {
124 case QMetaType::Int:
125 return m_locale .toCurrencyString(in.toInt());
126 case QMetaType::UInt:
127 return m_locale .toCurrencyString(in.toUInt());
128 case QMetaType::Double:
129 return m_locale .toCurrencyString(in.toDouble());
130 case QMetaType::LongLong:
131 return m_locale .toCurrencyString(in.toLongLong());
132 case QMetaType::ULongLong:
133 return m_locale .toCurrencyString(in.toULongLong());
134 default:
135 break;
136 }
137 return QString();
138 }
140 return m_locale.quoteString(in.value<QStringView>());
142 return m_locale.quoteString(in.value<QStringView>(), QLocale::AlternateQuotation);
144 return m_locale.createSeparatedList(in.value<QStringList>());
145 case LocaleChanged:
146 Q_ASSERT_X(false, Q_FUNC_INFO, "This can't happen.");
147 case UILanguages: {
149 LocaleList localeListObject = LocaleList::callStaticMethod<LocaleList>("getDefault");
150 if (localeListObject.isValid()) {
151 QString lang = localeListObject.callMethod<QString>("toLanguageTags");
152 // Some devices return with it enclosed in []'s so check if both exists before
153 // removing to ensure it is formatted correctly
154 if (lang.startsWith(QChar('[')) && lang.endsWith(QChar(']')))
155 lang = lang.mid(1, lang.length() - 2);
156 return lang.split(QChar(','));
157 }
158 }
159 return QVariant();
160 }
161 default:
162 break;
163 }
164 return QVariant();
165}
166
168{
169 QReadLocker locker(&m_lock);
170 return m_locale;
171}
172
QVariant query(QueryType type, QVariant &&in) const override
QLocale fallbackLocale() const override
\inmodule QtCore
\inmodule QtCore
QString decimalPoint() const
Definition qlocale.cpp:2655
QString quoteString(const QString &str, QuotationStyle style=StandardQuotation) const
Definition qlocale.h:1161
QString dateTimeFormat(FormatType format=LongFormat) const
Definition qlocale.cpp:2396
QString zeroDigit() const
Definition qlocale.cpp:2710
QString dateFormat(FormatType format=LongFormat) const
Definition qlocale.cpp:2334
QString negativeSign() const
Definition qlocale.cpp:2727
Qt::DayOfWeek firstDayOfWeek() const
Definition qlocale.cpp:3272
@ AlternateQuotation
Definition qlocale.h:1159
QString dayName(int, FormatType format=LongFormat) const
Definition qlocale.cpp:2997
CurrencySymbolFormat
Definition qlocale.h:896
@ LongFormat
Definition qlocale.h:875
@ NarrowFormat
Definition qlocale.h:875
@ ShortFormat
Definition qlocale.h:875
QString toCurrencyString(qlonglong, const QString &symbol=QString()) const
Definition qlocale.cpp:4580
QString timeFormat(FormatType format=LongFormat) const
Definition qlocale.cpp:2365
QString groupSeparator() const
Definition qlocale.cpp:2674
QString createSeparatedList(const QStringList &strl) const
Definition qlocale.cpp:1246
QString monthName(int, FormatType format=LongFormat) const
Definition qlocale.cpp:2963
QString amText() const
Definition qlocale.cpp:3439
QString pmText() const
Definition qlocale.cpp:3459
QString positiveSign() const
Definition qlocale.cpp:2744
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition qlocale.cpp:2052
QString standaloneMonthName(int, FormatType format=LongFormat) const
Definition qlocale.cpp:2979
QString standaloneDayName(int, FormatType format=LongFormat) const
Definition qlocale.cpp:3014
QString currencySymbol(CurrencySymbolFormat=CurrencySymbol) const
Definition qlocale.cpp:4548
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
QStringList split(const QString &sep, Qt::SplitBehavior behavior=Qt::KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
Definition qstring.cpp:8218
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
qsizetype length() const noexcept
Returns the number of characters in this string.
Definition qstring.h:191
@ StringToAlternateQuotation
Definition qlocale_p.h:160
@ DateTimeToStringShort
Definition qlocale_p.h:148
@ StandaloneMonthNameLong
Definition qlocale_p.h:166
@ ListToSeparatedString
Definition qlocale_p.h:162
@ StandaloneDayNameNarrow
Definition qlocale_p.h:171
@ StandaloneMonthNameNarrow
Definition qlocale_p.h:168
@ StringToStandardQuotation
Definition qlocale_p.h:159
@ StandaloneDayNameShort
Definition qlocale_p.h:170
@ StandaloneDayNameLong
Definition qlocale_p.h:169
@ StandaloneMonthNameShort
Definition qlocale_p.h:167
\inmodule QtCore
Definition qvariant.h:65
\inmodule QtCore
Combined button and popup list for selecting options.
Q_CORE_EXPORT jint androidSdkVersion()
Q_CORE_EXPORT QtJniTypes::Context context()
#define Q_FUNC_INFO
GLenum type
GLuint res
GLuint in
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
QT_BEGIN_NAMESPACE Q_DECLARE_JNI_CLASS(Environment, "android/os/Environment")