Make QCoreApp::translate and related methods use UTF-8

Deprecate the Encoding enum in QCoreApplication and the
trUtf8() methods. Qt now assumes that source code is
always encoded in UTF-8 to be consistent with QString.

Change-Id: Ic62d6947046dee9be0cbd37f2d2f6976b9e572a7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Lars Knoll 2012-05-16 23:09:56 +02:00 committed by Qt by Nokia
parent 9e13d169a4
commit 83645377e9
6 changed files with 40 additions and 70 deletions

6
dist/changes-5.0.0 vendored
View File

@ -354,9 +354,9 @@ QtCore
libraries, creating uncertainty/bugs in using QString easily, and (to a lesser
extent) performance issues.
* QTextCodec::codecForTr() and QTextCodec::setCodecForTr() have been removed,
QCoreApplication::Encoding value CodecForTr is now obsolete, use
DefaultCodec instead. For reasoning, see the codecForCStrings() removal above.
* QTextCodec::codecForTr() and QTextCodec::setCodecForTr() have been removed.
QObject::trUtf8 and QCoreApplication::Encoding enum are now obsolete. Qt assumes
that the source code is encoded in UTF-8.
* QFile::setEncodingFunction and QFile::setDecodingFunction are obsolete and do
nothing in Qt 5. The QFile::encodeName and QFile::decodeName functions are now

View File

@ -1466,15 +1466,17 @@ bool QCoreApplication::event(QEvent *e)
}
/*! \enum QCoreApplication::Encoding
\obsolete
This enum type defines the 8-bit encoding of character string
arguments to translate():
This enum type used to define the 8-bit encoding of character string
arguments to translate(). This enum is now obsolete and UTF-8 will be
used in all cases.
\value UnicodeUTF8 UTF-8.
\value Latin1 Latin-1.
\value DefaultCodec Latin-1.
\value Latin1 UTF-8.
\value DefaultCodec UTF-8.
\sa QObject::tr(), QObject::trUtf8(), QString::fromUtf8()
\sa QObject::tr(), QString::fromUtf8()
*/
void QCoreApplicationPrivate::ref()
@ -1626,15 +1628,13 @@ static void replacePercentN(QString *result, int n)
/*!
\reentrant
\since 4.5
Returns the translation text for \a sourceText, by querying the
installed translation files. The translation files are searched
from the most recently installed file back to the first
installed file.
QObject::tr() and QObject::trUtf8() provide this functionality
more conveniently.
QObject::tr() provides this functionality more conveniently.
\a context is typically a class name (e.g., "MyDialog") and \a
sourceText is either English text or a short identifying text.
@ -1646,15 +1646,12 @@ static void replacePercentN(QString *result, int n)
See the \l QTranslator and \l QObject::tr() documentation for
more information about contexts, disambiguations and comments.
\a encoding indicates the 8-bit encoding of character strings.
\a n is used in conjunction with \c %n to support plural forms.
See QObject::tr() for details.
If none of the translation files contain a translation for \a
sourceText in \a context, this function returns a QString
equivalent of \a sourceText. The encoding of \a sourceText is
specified by \e encoding; it defaults to DefaultCodec.
equivalent of \a sourceText.
This function is not virtual. You can use alternative translation
techniques by subclassing \l QTranslator.
@ -1667,10 +1664,8 @@ static void replacePercentN(QString *result, int n)
\sa QObject::tr(), installTranslator()
*/
QString QCoreApplication::translate(const char *context, const char *sourceText,
const char *disambiguation, Encoding encoding, int n)
const char *disambiguation, int n)
{
QString result;
@ -1688,16 +1683,8 @@ QString QCoreApplication::translate(const char *context, const char *sourceText,
}
}
if (result.isNull()) {
#ifdef QT_NO_TEXTCODEC
Q_UNUSED(encoding)
#else
if (encoding == UnicodeUTF8)
result = QString::fromUtf8(sourceText);
else
#endif
result = QString::fromLatin1(sourceText);
}
if (result.isNull())
result = QString::fromUtf8(sourceText);
replacePercentN(&result, n);
return result;
@ -1706,7 +1693,7 @@ QString QCoreApplication::translate(const char *context, const char *sourceText,
// Declared in qglobal.h
QString qtTrId(const char *id, int n)
{
return QCoreApplication::translate(0, id, 0, QCoreApplication::UnicodeUTF8, n);
return QCoreApplication::translate(0, id, 0, n);
}
bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)

View File

@ -137,16 +137,17 @@ public:
static bool installTranslator(QTranslator * messageFile);
static bool removeTranslator(QTranslator * messageFile);
#endif
enum Encoding { UnicodeUTF8, Latin1, DefaultCodec = Latin1
#if QT_DEPRECATED_SINCE(5, 0)
, CodecForTr = Latin1
#endif
};
static QString translate(const char * context,
const char * key,
const char * disambiguation = 0,
Encoding encoding = DefaultCodec,
int n = -1);
#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,
const char * disambiguation, Encoding, int n = -1)
{ return translate(context, key, disambiguation, n); }
#endif
static void flush();
@ -213,28 +214,26 @@ inline bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *ev
{ if (event) event->spont = true; return self ? self->notifyInternal(receiver, event) : false; }
#ifdef QT_NO_TRANSLATION
// Simple versions
inline QString QCoreApplication::translate(const char *, const char *sourceText,
const char *, Encoding encoding, int)
inline QString QCoreApplication::translate(const char *, const char *sourceText, const char *, int)
{
#ifndef QT_NO_TEXTCODEC
if (encoding == UnicodeUTF8)
return QString::fromUtf8(sourceText);
#else
Q_UNUSED(encoding)
#endif
return QString::fromLatin1(sourceText);
return QString::fromUtf8(sourceText);
}
#if QT_DEPRECATED_SINCE(5, 0)
QT_DEPRECATED inline QString QCoreApplication::translate(const char *, const char *sourceText, const char *, Encoding encoding, int)
{
Q_UNUSED(encoding)
return QString::fromUtf8(sourceText);
}
#endif
#endif
#define Q_DECLARE_TR_FUNCTIONS(context) \
public: \
static inline QString tr(const char *sourceText, const char *disambiguation = 0, int n = -1) \
{ return QCoreApplication::translate(#context, sourceText, disambiguation, \
QCoreApplication::DefaultCodec, n); } \
static inline QString trUtf8(const char *sourceText, const char *disambiguation = 0, int n = -1) \
{ return QCoreApplication::translate(#context, sourceText, disambiguation, \
QCoreApplication::UnicodeUTF8, n); } \
{ return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \
QT_DEPRECATED static inline QString trUtf8(const char *sourceText, const char *disambiguation = 0, int n = -1) \
{ return QCoreApplication::translate(#context, sourceText, disambiguation, n); } \
private:
typedef void (*QtCleanUpFunction)();

View File

@ -367,15 +367,7 @@ const QObject *QMetaObject::cast(const QObject *obj) const
*/
QString QMetaObject::tr(const char *s, const char *c, int n) const
{
return QCoreApplication::translate(rawStringData(this, 0), s, c, QCoreApplication::DefaultCodec, n);
}
/*!
\internal
*/
QString QMetaObject::trUtf8(const char *s, const char *c, int n) const
{
return QCoreApplication::translate(rawStringData(this, 0), s, c, QCoreApplication::UnicodeUTF8, n);
return QCoreApplication::translate(rawStringData(this, 0), s, c, n);
}
#endif // QT_NO_TRANSLATION

View File

@ -133,9 +133,9 @@ public:
#endif
#ifdef QT_NO_TRANSLATION
static QString tr(const char *sourceText, const char * = 0, int = -1)
{ return QString::fromLatin1(sourceText); }
#ifndef QT_NO_TEXTCODEC
static QString trUtf8(const char *sourceText, const char * = 0, int = -1)
{ return QString::fromUtf8(sourceText); }
#if QT_DEPRECATED_SINCE(5, 0)
QT_DEPRECATED static QString trUtf8(const char *sourceText, const char * = 0, int = -1)
{ return QString::fromUtf8(sourceText); }
#endif
#endif //QT_NO_TRANSLATION

View File

@ -97,19 +97,12 @@ class QString;
#endif // QT_NO_META_MACROS
#ifndef QT_NO_TRANSLATION
# ifndef QT_NO_TEXTCODEC
// full set of tr functions
# define QT_TR_FUNCTIONS \
static inline QString tr(const char *s, const char *c = 0, int n = -1) \
{ return staticMetaObject.tr(s, c, n); } \
static inline QString trUtf8(const char *s, const char *c = 0, int n = -1) \
{ return staticMetaObject.trUtf8(s, c, n); }
# else
// no QTextCodec, no utf8
# define QT_TR_FUNCTIONS \
static inline QString tr(const char *s, const char *c = 0, int n = -1) \
QT_DEPRECATED static inline QString trUtf8(const char *s, const char *c = 0, int n = -1) \
{ return staticMetaObject.tr(s, c, n); }
# endif
#else
// inherit the ones from QObject
# define QT_TR_FUNCTIONS
@ -306,7 +299,6 @@ struct Q_CORE_EXPORT QMetaObject
#ifndef QT_NO_TRANSLATION
QString tr(const char *s, const char *c, int n = -1) const;
QString trUtf8(const char *s, const char *c, int n = -1) const;
#endif // QT_NO_TRANSLATION
int methodOffset() const;