From c74bd2b9364ad2619eae5e72496b2ab7bb6041b8 Mon Sep 17 00:00:00 2001 From: Mateusz Starzycki Date: Tue, 28 Jun 2016 22:09:14 +0100 Subject: Add QT_TR*_N_NOOP() macros [ChangeLog][QtCore][Global] Added the QT_TR_N_NOOP(), QT_TRANSLATE_N_NOOP(), and QT_TRANSLATE_N_NOOP3() macros for numeral dependent delayed translation. Change-Id: I57c5b1ad4006267f49a57b0cbc40216b8e0399ff Reviewed-by: Oswald Buddenhagen Reviewed-by: Martin Smith --- .../snippets/code/src_corelib_global_qglobal.cpp | 48 +++++++++++++++ src/corelib/global/qglobal.cpp | 68 +++++++++++++++++++++- src/corelib/global/qglobal.h | 4 ++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp index 2d7b9a9ac8..9d029e5d4d 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qglobal.cpp @@ -440,6 +440,54 @@ QString global_greeting(int type) //! [36] +//! [qttrnnoop] +static const char * const StatusClass::status_strings[] = { + QT_TR_N_NOOP("There are %n new message(s)"), + QT_TR_N_NOOP("There are %n total message(s)") +}; + +QString StatusClass::status(int type, int count) +{ + return tr(status_strings[type], nullptr, count); +} +//! [qttrnnoop] + +//! [qttranslatennoop] +static const char * const greeting_strings[] = { + QT_TRANSLATE_N_NOOP("Welcome Msg", "Hello, you have %n message(s)"), + QT_TRANSLATE_N_NOOP("Welcome Msg", "Hi, you have %n message(s)") +}; + +QString global_greeting(int type, int msgcnt) +{ + return translate("Welcome Msg", greeting_strings[type], nullptr, msgcnt); +} +//! [qttranslatennoop] + +//! [qttranslatennoop3] +static { const char * const source; const char * const comment; } status_strings[] = { + QT_TRANSLATE_N_NOOP3("Message Status", "Hello, you have %n message(s)", + "A login message status"), + QT_TRANSLATE_N_NOOP3("Message status", "You have %n new message(s)", + "A new message query status") +}; + +QString FriendlyConversation::greeting(int type, int count) +{ + return tr(status_strings[type].source, + status_strings[type].comment, count); +} + +QString global_greeting(int type, int count) +{ + return qApp->translate("Message Status", + status_strings[type].source, + status_strings[type].comment, + count); +} +//! [qttranslatennoop3] + + //! [qttrid] //% "%n fooish bar(s) found.\n" //% "Do you want to continue?" diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 2ca627e851..ea62c8247a 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -650,7 +650,8 @@ Q_STATIC_ASSERT((std::is_same::value)); The remaining macros are convenience macros for larger operations: The QT_TR_NOOP(), QT_TRANSLATE_NOOP(), and QT_TRANSLATE_NOOP3() macros provide the possibility of marking strings for delayed - translation. + translation. QT_TR_N_NOOP(), QT_TRANSLATE_N_NOOP(), and + QT_TRANSLATE_N_NOOP3() are numerator dependent variants of these. The Q_ASSERT() and Q_ASSERT_X() enables warning messages of various level of refinement. The Q_FOREACH() and foreach() macros implement Qt's foreach loop. @@ -3732,6 +3733,71 @@ bool qunsetenv(const char *varName) \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), {Internationalization with Qt} */ +/*! + \macro QT_TR_N_NOOP(sourceText) + \relates + \since 5.12 + + Marks the UTF-8 encoded string literal \a sourceText for numerator + dependent delayed translation in the current context (class). + + The macro tells lupdate to collect the string, and expands to + \a sourceText itself. + + The macro expands to \a sourceText. + + Example: + + \snippet code/src_corelib_global_qglobal.cpp qttrnnoop + + \sa QT_TR_NOOP, {Internationalization with Qt} +*/ + +/*! + \macro QT_TRANSLATE_N_NOOP(context, sourceText) + \relates + \since 5.12 + + Marks the UTF-8 encoded string literal \a sourceText for numerator + dependent delayed translation in the given \a context. + The \a context is typically a class name and also needs to be + specified as a string literal. + + The macro tells lupdate to collect the string, and expands to + \a sourceText itself. + + Example: + + \snippet code/src_corelib_global_qglobal.cpp qttranslatennoop + + \sa QT_TRANSLATE_NOOP(), QT_TRANSLATE_N_NOOP3(), + {Internationalization with Qt} +*/ + +/*! + \macro QT_TRANSLATE_N_NOOP3(context, sourceText, comment) + \relates + \since 5.12 + + Marks the UTF-8 encoded string literal \a sourceText for numerator + dependent delayed translation in the given \a context with the given + \a disambiguation. + The \a context is typically a class and also needs to be specified + as a string literal. The string literal \a disambiguation should be + a short semantic tag to tell apart otherwise identical strings. + + The macro tells lupdate to collect the string, and expands to an + anonymous struct of the two string literals passed as \a sourceText + and \a disambiguation. + + Example: + + \snippet code/src_corelib_global_qglobal.cpp qttranslatennoop3 + + \sa QT_TR_NOOP(), QT_TRANSLATE_NOOP(), QT_TRANSLATE_NOOP3(), + {Internationalization with Qt} +*/ + /*! \fn QString qtTrId(const char *id, int n = -1) \relates diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index f86b62d56a..b608489576 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1064,6 +1064,10 @@ template inline auto qGetPtrHelper(const Ptr &ptr) -> decltype(pt #ifndef QT_NO_TRANSLATION // ### Qt6: This should enclose the NOOPs above +#define QT_TR_N_NOOP(x) x +#define QT_TRANSLATE_N_NOOP(scope, x) x +#define QT_TRANSLATE_N_NOOP3(scope, x, comment) {x, comment} + // Defined in qcoreapplication.cpp // The better name qTrId() is reserved for an upcoming function which would // return a much more powerful QStringFormatter instead of a QString. -- cgit v1.2.3