QLocale: Update the system private on QLocale default constructor if needed

When first starting an Android app we have invocation order issue, to
load the platform plugin we create the default QLocale (needed by the
resource locator code to see if :/qt/etc/qt.conf exists) so when the
android platform plugin loads and creates its own QSystemLocale, the
QLocale defaultLocalePrivate is already created and pointing to
globalLocaleData which means that systemData won't be called and thus
the code that triggers the call to QLocalePrivate::updateSystemPrivate
won't be called when calling QLocale().

I thought of two ways of fixing this, one was calling
QLocalePrivate::updateSystemPrivatea() from the QAndroidSystemLocale
constructor, but giving the responsibility to not break things to the
plugin seems a little fragile, so making the check on QLocale()
seems better.

Without this patch an Android app doing
  QApplication app(argc, argv);
  qDebug() << QLocale().name();
  qDebug() << QLocale().name();
  qDebug() << QLocale::system().name();
  qDebug() << QLocale().name();
would print
  ""
  ""
  "ca_ES"
  "ca_ES"
now it correctly prints "ca_ES" the four times.

Task-number: QTBUG-41385
Change-Id: I2cf419f59aa008fa3aca11295fe7d42c40bcc32e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Albert Astals Cid 2018-01-02 10:48:34 +01:00
parent fdddb3a481
commit 78e92997ed
2 changed files with 48 additions and 0 deletions

View File

@ -852,6 +852,8 @@ QLocale::QLocale(const QString &name)
QLocale::QLocale()
: d(*defaultLocalePrivate)
{
// Make sure system data is up to date
systemData();
}
/*!

View File

@ -141,6 +141,8 @@ private slots:
void formattedDataSize();
void bcp47Name();
void systemLocale();
private:
QString m_decimal, m_thousand, m_sdate, m_ldate, m_time;
QString m_sysapp;
@ -2641,5 +2643,49 @@ void tst_QLocale::bcp47Name()
QCOMPARE(QLocale("sr_Latn_HR").bcp47Name(), QStringLiteral("sr-Latn"));
}
class MySystemLocale : public QSystemLocale
{
public:
MySystemLocale(const QLocale &locale) : m_locale(locale)
{
}
QVariant query(QueryType /*type*/, QVariant /*in*/) const override
{
return QVariant();
}
QLocale fallbackUiLocale() const override
{
return m_locale;
}
private:
const QLocale m_locale;
};
void tst_QLocale::systemLocale()
{
QLocale originalLocale;
MySystemLocale *sLocale = new MySystemLocale(QLocale("uk"));
QCOMPARE(QLocale().language(), QLocale::Ukrainian);
QCOMPARE(QLocale::system().language(), QLocale::Ukrainian);
delete sLocale;
sLocale = new MySystemLocale(QLocale("ca"));
QCOMPARE(QLocale().language(), QLocale::Catalan);
QCOMPARE(QLocale::system().language(), QLocale::Catalan);
delete sLocale;
sLocale = new MySystemLocale(QLocale("de"));
QCOMPARE(QLocale().language(), QLocale::German);
QCOMPARE(QLocale::system().language(), QLocale::German);
delete sLocale;
QCOMPARE(QLocale(), originalLocale);
QCOMPARE(QLocale::system(), originalLocale);
}
QTEST_MAIN(tst_QLocale)
#include "tst_qlocale.moc"