Question

There are two forms of the QObject::tr() function I have been using for translating strings which contain numbers:

tr("There are %n foo(s)", 0, foo.size());
tr("%1: %2").arg(QLocale().toString(bar.size())).arg(bar.name());

These account for most instances where you want a number in a string except when you want to do localisation and pluralisation in the same string. Of course you could break the translation up, but you have to make a guess about whether that string will break in the same places in all languages (a decision I'm not qualified to make most of the time).

The QLocale documentation states that...

QString::arg() uses the default locale to format a number when its position specifier in the format string contains an 'L', e.g. "%L1"

... so the second form could be rewritten as ...

tr("%L1: %2").arg(bar.size()).arg(bar.name());

... and this suggests you could rewrite the first form as...

tr("There are %Ln foo(s)", 0, foo.size());

This will compile and it shows up in Linguist as a pluralisation, but does anyone know if this will also localise the number? I can't find anything which says either way in any documentation.

Was it helpful?

Solution

Yes, it will work:

#include <QtCore>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

//    QLocale::setDefault(QLocale(QLocale::German));
    QLocale::setDefault(QLocale(QLocale::English));

    QTranslator qtTranslator;
//    qtTranslator.load("foo_de", qApp->applicationDirPath());
    qtTranslator.load("foo_en", qApp->applicationDirPath());
    a.installTranslator(&qtTranslator);

    int foo_count = 123456;

    QString str1 = QObject::tr("There are %n foo(s)", "foo", foo_count);
    QString str2 = QObject::tr("There are %Ln foo(s)", "foo", foo_count);

    qDebug() << str1;
    qDebug() << str2;

    return a.exec();
}

Output if running English:

"There are 123456 foos"
"There are 123,456 foos"

Output if running German (with apologies to any actual German speakers out there):

"Es gibt 123456 fooen"
"Es gibt 123.456 fooen"

The only weird things are:

  • The translator must keep the %Ln in both the singular and plural translations, not just the usual %n.
  • Linguist will complain for the plural form that "Translation does not contain the necessary %n place marker." This is a bug.

OTHER TIPS

The (Qt 4.x) documentation here would seem to imply that it will both pluralize and localize since it is presented under the section heading "Handling Plurals".

The later Qt 5.x documentation here seems to be a verbatim copy, which seems a shame as this is not a particularly well documented use of QObject::tr(...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top