Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QString QCoreApplication::translate ( const char *  context,
const char *  sourceText,
const char *  comment,
Encoding  encoding,
int  n 
) [static]

Returns the translation text for 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.

context is typically a class name (e.g., "MyDialog") and sourceText is either English text or a short identifying text.

comment is a disambiguating comment, for when the same sourceText is used in different roles within the same context. By default, it is null. encoding indicates the 8-bit encoding of character stings See the QTranslator documentation for more information about contexts and comments.

n is used in conjunction with n to support plural forms. See QObject::tr() for details.

If none of the translation files contain a translation for sourceText in context, this function returns a QString equivalent of sourceText. The encoding of sourceText is specified by encoding; it defaults to CodecForTr.

This function is not virtual. You can use alternative translation techniques by subclassing QTranslator.

Warning:
This method is reentrant only if all translators are installed before calling this method. Installing or removing translators while performing translations is not supported. Doing so will most likely result in crashes or other undesirable behavior.
See also:
QObject::tr() installTranslator() QTextCodec::codecForTr()

Definition at line 1462 of file qcoreapplication.cpp.

References QString::arg(), QTextCodec::codecForTr(), QList< T >::constEnd(), QString::fromLatin1(), QString::fromUtf8(), QString::indexOf(), QString::isEmpty(), QString::mid(), QString::replace(), QString::startsWith(), QTextCodec::toUnicode(), and QTranslator::translate().

{
    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, comment, n);
            if (!result.isEmpty())
                break;
        }
    }

    if (result.isEmpty()) {
#ifdef QT_NO_TEXTCODEC
        Q_UNUSED(encoding)
#else
        if (encoding == UnicodeUTF8)
            result = QString::fromUtf8(sourceText);
        else if (QTextCodec::codecForTr() != 0)
            result = QTextCodec::codecForTr()->toUnicode(sourceText);
        else
#endif
            result = QString::fromLatin1(sourceText);
    }

    if (n >= 0) {
        int percentPos = -1;
        while ((percentPos = result.indexOf(QLatin1Char('%'), percentPos + 1)) != -1) {
            int len = 1;
            QString fmt(QLatin1String("%1"));
            if (result.mid(percentPos + len, 1).startsWith(QLatin1Char('L'))) {
                ++len;
                fmt = QLatin1String("%L1");
            }
            if (result.mid(percentPos + len, 1).startsWith(QLatin1Char('n'))) {
                ++len;
                result.replace(percentPos, len, fmt.arg(n));
            }
        }
    }
    return result;
}


Generated by  Doxygen 1.6.0   Back to index