qttranslationqmltr

Qt qsTr() handling plurals


Im developing and applicacion and want to translate some text, the problem is when i want to handle in QML the plurals.

In c++, the way to handle the plurals is as simple as:

int n = messages.count();
showMessage(tr("%n message(s) saved", 0, n));

and will translate without problems

source: https://doc.qt.io/qt-5/i18n-source-translation.html#handling-plurals

When I try to do the same with QML doesnt work. After carefull review of some literature, and some comments I found out a "solution", that it's actually people reporting a bug.

var second = qsTr("%b radios", "0", map.radio).arg(map.radio)

source:https://bugreports.qt.io/browse/QTBUG-11579

When I lupdate, in the QtLinguistic it appears the two fields for plural and singular form, but in the application does not work.

I tried several modifications such as:

    var a = map.totalSongs;
    var first = qsTr("%a songs", "0", parseInt(a))
    var second = qsTr("%b radios", "0", map.radio)

    var first = qsTr("%a songs", "0", parseInt(a)).arg(map.totalSongs)
    var second = qsTr("%b radios", "0", map.radio).arg(map.radio)

    var first = qsTr("%a songs", "0", a)
    var second = qsTr("%b radios", "0", b)

In QtLinguistic im writting the translation:

%b radio - Singular
%b radios - Plural

Any modification fails to work.

Can some one tell me how to use qstr() to handle the plurals?

Other question related: Lets say I want to have a text "%1 songs - %2 radios", where in spanish should result in

//As example
if(%1 = 10 && %2 = 10) => "10 canciones -  radios"
else if(%1 = 1 && %2 = 10) => "1 cancion - 10 radios"
else if(%1 = 10 && %2 = 1) => "10 canciones - 1 radio"

How to do it? I think either qstr() or tr() can not handle this situation., but just want to verify it with you guys :D

Thanks in advance


Solution

  • I couldnt accept that was not working so I went a little further and find a solution, maybe seems obvious, but I dont think its so.

    Doesnt work

        var a = map.totalSongs;
        var first = qsTr("%a songs", "0", a)
    

    Works because we use the variable N

        var n = map.radio;
        var first = qsTr("%n songs", "0", n)