Qt-interest Archive, January 2008
QObject::tr() and float values
Message 1 in thread
The documentation for tr() documents the method for inflecting the
translated phrase properly for the number the user passes in - the
tr(char const*, char const*, int) version.
I have a situation where I must handle a floating-point number - imagine
a string that reports "20.5 degrees Celsius", for instance. My quandary
is that in English, "1.5 apples" gets a plural, but I am far from sure
that this is true for all languages - much less how to deal with
languages which have more numerical classes than singular and plural
(i.e., Arabic).
Has anybody out there had to deal with this? Could this, perhaps, get
added to the tr() function in a future version of Qt?
Thanks much!
--
[ signature omitted ]
Message 2 in thread
On January 25, 2008 08:40:55 am Gordon Schumacher wrote:
> The documentation for tr() documents the method for inflecting the
> translated phrase properly for the number the user passes in - the
> tr(char const*, char const*, int) version.
>
> I have a situation where I must handle a floating-point number - imagine
> a string that reports "20.5 degrees Celsius", for instance. My quandary
> is that in English, "1.5 apples" gets a plural, but I am far from sure
> that this is true for all languages - much less how to deal with
> languages which have more numerical classes than singular and plural
> (i.e., Arabic).
>
> Has anybody out there had to deal with this? Could this, perhaps, get
> added to the tr() function in a future version of Qt?
>
> Thanks much!
I'm not sure whether 1.5 would be plural in all languages. If it is NOT,
though, you could do the following (floating point comparisons may not work):
QString foo;
if (temp == 1.0)
foo = tr("1 apple", "exactly 1");
else if ((temp > 1.0) && (temp < 2.0))
foo = tr("%1 apples", "between 1 and 2, maybe plural").arg(temp);
else
foo = tr("%1 apples", "definitely plural").arg(temp);
That is, make use of the QObject::tr(...) second parameter to indicate to the
person doing translations that this may or may not be plural. Not exactly
pretty, but if you have a lot of these examples, you could create a little
function to tidy things up a bit.
--
[ signature omitted ]
Message 3 in thread
Chris Thompson wrote:
> I'm not sure whether 1.5 would be plural in all languages.
> If it is NOT,
> though, you could do the following (floating point
> comparisons may not work):
>
> QString foo;
> if (temp == 1.0)
> foo = tr("1 apple", "exactly 1");
> else if ((temp > 1.0) && (temp < 2.0))
> foo = tr("%1 apples", "between 1 and 2, maybe plural").arg(temp);
> else
> foo = tr("%1 apples", "definitely plural").arg(temp);
>
> That is, make use of the QObject::tr(...) second parameter to
> indicate to the
> person doing translations that this may or may not be plural.
> Not exactly
> pretty, but if you have a lot of these examples, you could
> create a little
> function to tidy things up a bit.
Hi there,
i think it's not as easy as this, because there is not only "singular"
and "plural".
In some languages there are more special cases to handle, for example
- "dual" (n=2) in Irish and other languages.
- "paucal" (n = 2-4, 22-24, 32-34, 42-44 etc.) in Polish.
- for more examples see http://doc.trolltech.com/qq/qq19-plurals.html
every of these languages will probably have rules how to apply their
special cases to floating point numbers.
so i think Trolltech is really the only one who is able to solve this
problem correctly.
Cheers,
Peter
--
[ signature omitted ]
Message 4 in thread
On Jan 25, 2008 4:54 PM, Chris Thompson <cthompson@xxxxxxxxxxxxxxx> wrote:
> QString foo;
> if (temp == 1.0)
> foo = tr("1 apple", "exactly 1");
> else if ((temp > 1.0) && (temp < 2.0))
> foo = tr("%1 apples", "between 1 and 2, maybe plural").arg(temp);
> else
> foo = tr("%1 apples", "definitely plural").arg(temp);
I do this too, it's completly ugly. I would really like to have tr()
behave like the ngettext function wrt plural forms.
--
[ signature omitted ]
Message 5 in thread
Robin Helgelin wrote:
> <cthompson@xxxxxxxxxxxxxxx> wrote:
> > QString foo;
> > if (temp == 1.0)
> > foo = tr("1 apple", "exactly 1");
> > else if ((temp > 1.0) && (temp < 2.0))
> > foo = tr("%1 apples", "between 1 and 2, maybe plural").arg(temp);
> > else
> > foo = tr("%1 apples", "definitely plural").arg(temp);
>
> I do this too, it's completly ugly. I would really like to have tr()
> behave like the ngettext function wrt plural forms.
You might have missed it, but tr() has plural support built in since
version 4.2:
http://doc.trolltech.com/4.3/qobject.html#tr
The only "downside" with it is that in order to use it, you have to
supply a translation file, even if you only want to support 1 language.
so for example:
tr("%n message(s) saved", "", 1) // -> becomes "1 message saved"
tr("%n message(s) saved", "", 2) // -> becomes "2 messages saved"
Cheers,
Peter
--
[ signature omitted ]
Message 6 in thread
On January 25, 2008 09:38:09 am Peter Prade wrote:
> Robin Helgelin wrote:
> > <cthompson@xxxxxxxxxxxxxxx> wrote:
> > > QString foo;
> > > if (temp == 1.0)
> > > foo = tr("1 apple", "exactly 1");
> > > else if ((temp > 1.0) && (temp < 2.0))
> > > foo = tr("%1 apples", "between 1 and 2, maybe plural").arg(temp);
> > > else
> > > foo = tr("%1 apples", "definitely plural").arg(temp);
> >
> > I do this too, it's completly ugly. I would really like to have tr()
> > behave like the ngettext function wrt plural forms.
>
> You might have missed it, but tr() has plural support built in since
> version 4.2:
> http://doc.trolltech.com/4.3/qobject.html#tr
>
> The only "downside" with it is that in order to use it, you have to
> supply a translation file, even if you only want to support 1 language.
>
> so for example:
> tr("%n message(s) saved", "", 1) // -> becomes "1 message saved"
> tr("%n message(s) saved", "", 2) // -> becomes "2 messages saved"
I had indeed missed this, even when looking at QObject::tr() today. Thanks,
everyone, for pointing this out. It still doesn't take floating point
numbers so it looks like it won't solve Gordon's problem. After all, in
English, we have 1.5 apples so clearly just casting to int won't work.
--
[ signature omitted ]
Message 7 in thread
On Jan 25, 2008 5:38 PM, Peter Prade <prade@xxxxxxxxxxx> wrote:
> You might have missed it, but tr() has plural support built in since
> version 4.2:
> http://doc.trolltech.com/4.3/qobject.html#tr
Yep, missed it :)
> The only "downside" with it is that in order to use it, you have to
> supply a translation file, even if you only want to support 1 language.
>
> so for example:
> tr("%n message(s) saved", "", 1) // -> becomes "1 message saved"
> tr("%n message(s) saved", "", 2) // -> becomes "2 messages saved"
Thanks for the heads up on this, will solve a few ugly pieces of code
in our applications. :)
--
[ signature omitted ]