Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
windows:mingw:win32
mingw32-libqt5-qtbase
0001-Add-support-for-plurals-with-floating-valu...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0001-Add-support-for-plurals-with-floating-values.patch of Package mingw32-libqt5-qtbase
From 9dc6c88925c86519160c75263a856f503719144f Mon Sep 17 00:00:00 2001 From: Ralf Habacker <ralf.habacker@freenet.de> Date: Thu, 23 Aug 2018 18:30:50 +0200 Subject: [PATCH] Add support for plurals with floating values Change-Id: I1f54c8401d060f97396fe647f25bd9a052b096bd --- src/corelib/kernel/qcoreapplication.cpp | 50 +++++++++++++++ src/corelib/kernel/qcoreapplication.h | 6 ++ src/corelib/kernel/qobject.h | 4 ++ src/corelib/kernel/qtranslator.cpp | 72 ++++++++++++++++------ src/corelib/kernel/qtranslator.h | 2 + .../corelib/kernel/qtranslator/tst_qtranslator.cpp | 8 +++ 6 files changed, 124 insertions(+), 18 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index c8b130bbb9..d9c5a9d1e0 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -2005,6 +2005,30 @@ static void replacePercentN(QString *result, int n) } } +static void replacePercentN(QString *result, qreal n) +{ + if (!qIsNaN(n)) { + int percentPos = 0; + int len = 0; + while ((percentPos = result->indexOf(QLatin1Char('%'), percentPos + len)) != -1) { + len = 1; + QString fmt; + if (result->at(percentPos + len) == QLatin1Char('L')) { + ++len; + fmt = QLatin1String("%L1"); + } else { + fmt = QLatin1String("%1"); + } + if (result->at(percentPos + len) == QLatin1Char('n')) { + fmt = fmt.arg(n); + ++len; + result->replace(percentPos, len, fmt); + len = fmt.length(); + } + } + } +} + /*! \reentrant @@ -2069,6 +2093,32 @@ QString QCoreApplication::translate(const char *context, const char *sourceText, return result; } +QString QCoreApplication::translate(const char *context, const char *sourceText, + const char *disambiguation, qreal n) +{ + QString result; + + if (!sourceText) + return result; + + if (self && !self->d_func()->translators.isEmpty()) { + QList<QTranslator*>::ConstIterator it; + QTranslator *translationFile; + for (it = self->d_func()->translators.constBegin(); it != self->d_func()->translators.constEnd(); ++it) { + translationFile = *it; + result = translationFile->translate(context, sourceText, disambiguation, n); + if (!result.isNull()) + break; + } + } + + if (result.isNull()) + result = QString::fromUtf8(sourceText); + + replacePercentN(&result, n); + return result; +} + /*! \fn static QString QCoreApplication::translate(const char *context, const char *key, const char *disambiguation, Encoding encoding, int n = -1) \obsolete diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h index 0fee7b3de8..02d7ea7d68 100644 --- a/src/corelib/kernel/qcoreapplication.h +++ b/src/corelib/kernel/qcoreapplication.h @@ -157,6 +157,10 @@ public: const char * key, const char * disambiguation = Q_NULLPTR, int n = -1); + static QString translate(const char * context, + const char * key, + const char * disambiguation, + qreal n); #if QT_DEPRECATED_SINCE(5, 0) enum Encoding { UnicodeUTF8, Latin1, DefaultCodec = UnicodeUTF8, CodecForTr = UnicodeUTF8 }; QT_DEPRECATED static inline QString translate(const char * context, const char * key, @@ -248,6 +252,8 @@ inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *ev public: \ static inline QString tr(const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) \ { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \ + static inline QString tr(const char *sourceText, const char *disambiguation, qreal n) \ + { return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \ QT_DECLARE_DEPRECATED_TR_FUNCTIONS(context) \ private: diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index dea17ed530..a10d9d8193 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -130,12 +130,16 @@ public: #ifdef Q_QDOC static QString tr(const char *sourceText, const char *comment = Q_NULLPTR, int n = -1); static QString trUtf8(const char *sourceText, const char *comment = Q_NULLPTR, int n = -1); + static QString tr(const char *sourceText, const char *comment, qreal n); + static QString trUtf8(const char *sourceText, const char *comment, qreal n); virtual const QMetaObject *metaObject() const; static const QMetaObject staticMetaObject; #endif #ifdef QT_NO_TRANSLATION static QString tr(const char *sourceText, const char * = Q_NULLPTR, int = -1) { return QString::fromUtf8(sourceText); } + static QString tr(const char *sourceText, const char *, qreal) + { return QString::fromUtf8(sourceText); } #if QT_DEPRECATED_SINCE(5, 0) QT_DEPRECATED static QString trUtf8(const char *sourceText, const char * = Q_NULLPTR, int = -1) { return QString::fromUtf8(sourceText); } diff --git a/src/corelib/kernel/qtranslator.cpp b/src/corelib/kernel/qtranslator.cpp index 2232e24a25..41d0318d7c 100644 --- a/src/corelib/kernel/qtranslator.cpp +++ b/src/corelib/kernel/qtranslator.cpp @@ -324,6 +324,8 @@ public: bool do_load(const uchar *data, int len, const QString &directory); QString do_translate(const char *context, const char *sourceText, const char *comment, int n) const; + QString do_translate(const char *context, const char *sourceText, const char *comment, + qreal n) const; void clear(); }; @@ -963,22 +965,12 @@ end: return str; } -QString QTranslatorPrivate::do_translate(const char *context, const char *sourceText, - const char *comment, int n) const +QString QTranslatorPrivate::do_translate2(const char *context, const char *sourceText, + const char *comment) const { - if (context == 0) - context = ""; - if (sourceText == 0) - sourceText = ""; - if (comment == 0) - comment = ""; - - uint numerus = 0; + uint numerus = offsetLength / (2 * sizeof(quint32)); size_t numItems = 0; - if (!offsetLength) - goto searchDependencies; - /* Check if the context belongs to this QTranslator. If many translators are installed, this step is necessary. @@ -1004,10 +996,6 @@ QString QTranslatorPrivate::do_translate(const char *context, const char *source } } - numItems = offsetLength / (2 * sizeof(quint32)); - if (!numItems) - goto searchDependencies; - if (n >= 0) numerus = numerusHelper(n, numerusRulesArray, numerusRulesLength); @@ -1054,8 +1042,49 @@ QString QTranslatorPrivate::do_translate(const char *context, const char *source break; comment = ""; } + return QString(); +} + +QString QTranslatorPrivate::do_translate(const char *context, const char *sourceText, + const char *comment, int n) const +{ + if (context == 0) + context = ""; + if (sourceText == 0) + sourceText = ""; + if (comment == 0) + comment = ""; + + if (offsetLength) { + QString result = do_translate2(context, sourceText, comment); + if (!result.isEmpty()) + return result; + } + + for (QTranslator *translator : subTranslators) { + QString tn = translator->translate(context, sourceText, comment, n); + if (!tn.isNull()) + return tn; + } + return QString(); +} + +QString QTranslatorPrivate::do_translate(const char *context, const char *sourceText, + const char *comment, qreal n) const +{ + if (context == 0) + context = ""; + if (sourceText == 0) + sourceText = ""; + if (comment == 0) + comment = ""; + + if (offsetLength) { + QString result = do_translate2(context, sourceText, comment); + if (!result.isEmpty()) + return result; + } -searchDependencies: for (QTranslator *translator : subTranslators) { QString tn = translator->translate(context, sourceText, comment, n); if (!tn.isNull()) @@ -1130,6 +1159,13 @@ QString QTranslator::translate(const char *context, const char *sourceText, cons return d->do_translate(context, sourceText, disambiguation, n); } +QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, + qreal n) const +{ + Q_D(const QTranslator); + return d->do_translate(context, sourceText, disambiguation, n); +} + /*! Returns \c true if this translator is empty, otherwise returns \c false. This function works with stripped and unstripped translation files. diff --git a/src/corelib/kernel/qtranslator.h b/src/corelib/kernel/qtranslator.h index 97be16bc90..a8209c89c4 100644 --- a/src/corelib/kernel/qtranslator.h +++ b/src/corelib/kernel/qtranslator.h @@ -60,6 +60,8 @@ public: virtual QString translate(const char *context, const char *sourceText, const char *disambiguation = Q_NULLPTR, int n = -1) const; + virtual QString translate(const char *context, const char *sourceText, + const char *disambiguation, qreal n) const; virtual bool isEmpty() const; diff --git a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp index e3eee6dbdd..7f716bb6ad 100644 --- a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp +++ b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp @@ -219,6 +219,10 @@ void tst_QTranslator::plural() QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 0), QLatin1String("Hallo 0 Welten!")); QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 1), QLatin1String("Hallo 1 Welt!")); QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 2), QLatin1String("Hallo 2 Welten!")); + + QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 0.0), QLatin1String("Hallo 0.0 Welten!")); + QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 1.0), QLatin1String("Hallo 1.0 Welt!")); + QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 2.0), QLatin1String("Hallo 2.0 Welten!")); } void tst_QTranslator::translate_qm_file_generated_with_msgfmt() @@ -272,6 +276,10 @@ void tst_QTranslator::dependencies() QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 1), QLatin1String("Hallo 1 Welt!")); QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 2), QLatin1String("Hallo 2 Welten!")); + QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 0.0), QLatin1String("Hallo 0.0 Welten!")); + QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 1.0), QLatin1String("Hallo 1.0 Welt!")); + QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, 2.0), QLatin1String("Hallo 2.0 Welten!")); + // pick up translation from the file with dependencies QCOMPARE(tor.translate("QPushButton", "It's a small world"), QLatin1String("Es ist eine kleine Welt")); } -- 2.13.7
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor