[i18n] Confused about using plural forms

Bug #1184810 reported by Renato Araujo Oliveira Filho
24
This bug affects 4 people
Affects Status Importance Assigned to Milestone
ubuntu-ui-toolkit (Ubuntu)
Fix Released
Medium
Tim Peeters

Bug Description

In the i18n component documentation the plural form should be used as:

 * Translate the given input string \a singular or \a plural (depending on the number of items \a n)
 * using gettext. Should be called like this:
 * tr("%n file", "%n files", count)

But this does not work as expected:

The result string from this expression is: ("%n file" or "%n files" with the "%n" instead the value)

checking at the unit2d code I saw that the tr function automatically replaces the %n with the count value, but I am not sure if this is the correct solution.

For now I am using "tr("%1 file", "%1 files", count).arg(count)"

Related branches

Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit:
assignee: nobody → Tim Peeters (tpeeters)
Revision history for this message
Tim Peeters (tpeeters) wrote :

Even when using gettext the parameter needs to be passed twice:

An example for the us of this function is:

            printf (ngettext ("%d file removed", "%d files removed", n), n);

Please note that the numeric value n has to be passed to the printf function as well. It is not sufficient to pass it only to ngettext.

So we need to update our docs to match the way you are using it, or change the implementation of tr() to do that automatically. Still need to evaluate the consequences of the latter solution.

Revision history for this message
Tim Peeters (tpeeters) wrote :
Revision history for this message
David Planella (dpm) wrote :

I actually noticed this a few days ago, and I ended up doing what Renato did.

I'd suggest going for the more intuitive i18n.tr("%1 hour", "%1 hours", numHours), which is also similar to what the gettext API for Python does: http://docs.python.org/2/library/gettext.html

Changed in ubuntu-ui-toolkit:
status: New → Confirmed
Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit:
status: Confirmed → In Progress
Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit:
status: In Progress → Confirmed
Changed in ubuntu-ui-toolkit:
importance: High → Medium
Revision history for this message
Albert Astals Cid (aacid) wrote :

I do think that it should do the substitution automatically, what's the point in asking the user to do it?

Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit:
assignee: Tim Peeters (tpeeters) → nobody
Zoltan Balogh (bzoltan)
Changed in ubuntu-ui-toolkit (Ubuntu):
importance: Undecided → Medium
status: New → Confirmed
Zoltan Balogh (bzoltan)
no longer affects: ubuntu-ui-toolkit
Revision history for this message
dobey (dobey) wrote :

I think the documentation might need to be fixed, but the implementation seems fine to me. For the general case, it's not possible to automatically insert the "count", as the string may have multiple format specifiers where the numeric value is not the first argument. For instance, the following example would break, if the substitution was done automatically:

tr("Folder %1 has %2 file", "Folder %1 has %2 files", count)

In this case, %1 is the name of the folder, while %2 is the count. If the implementation were to return the result of the gettext call, as a QString with .arg(count) applied to it first, the result would be incorrect.

Maybe it should be made more explicit in the documentation that the API follows the standard QString API expectations here, and does not automatically insert count or contextual values in the string result of tr() calls, but I don't think the implementation should be changed.

summary: - [i18n] Plural form does not work as documented
+ [i18n] Confused about using plural forms
Revision history for this message
dobey (dobey) wrote :

I changed the title of this bug, because the implementation and documentation are aligned. There is nothing in the documentation implying that the "count" argument for plural forms will be automatically replaced in a format string. It is up to the programmer to finalize formatting of their strings properly.

Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit (Ubuntu):
assignee: nobody → Tim Peeters (tpeeters)
Revision history for this message
Tim Peeters (tpeeters) wrote :

It works like this:

import QtQuick 2.4
import Ubuntu.Components 1.3

Text {
    text: i18n.tr("singular: %1", "plural: %1", 1).arg("1") + "<br>" +
          i18n.tr("singular: %1", "plural: %1", 2).arg(2)
}

I will update documentation.

Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit (Ubuntu):
status: Confirmed → In Progress
Tim Peeters (tpeeters)
Changed in ubuntu-ui-toolkit (Ubuntu):
status: In Progress → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package ubuntu-ui-toolkit - 1.3.2060+16.10.20160814

---------------
ubuntu-ui-toolkit (1.3.2060+16.10.20160814) yakkety; urgency=medium

  [ Zoltan Balogh ]
  * Fix OptionSelectorTestCase tests.
  * Fix more OptionSelector autopilot tests

  [ Timo Jyrinki ]
  * Disable documentation building for GLES builds. Fixes LP: #1606222.
  * Replace abs with qFabs due to GCC6 breakage. Fixes LP: #1610943.
  * Limit s390x dependencies more due to upstart/s390x problems and removed
    packages in archives. Fixes LP: #1610951

  [ Andrea Bernabei ]
  * ListItemLayout doc: add elide mode change example and add section about
    labels default properties values. Fixes LP: #1603450.
  * ListItemLayout doc: add explicit note about the need to bind ListItem's
    height to layout's height.
  * More Scrollbar optimizations: 20% faster creation time. Fixes LP: #1606451
  * Scrollbar: fix wrong thumb color on tap/mouse release and increase coverage
    of hover states unit tests. Fixes LP: #1608897

  [ Zsombor Egri ]
  * Fix null pointer property initializer used with 1.3 PageWrapper.
    Fixes LP: #1604780.
  * Fix BottomEdge content URL preloading. Fixes LP: #1604509.

  [Albert Astals Cid ]
  * UCUnits::resolveResource: Prefer image path if it exists. This saves
    searching the disk for @gu images, which is a big speedup. According to
    callgrind loading 100 images from a folder that contains 380 images goes
    from around 3 million instructions per UCUnits::resolveResource call down
    to around 10 thousand. This optimization is ok since it is not correct to
    ship both image.png and image@20.png. You either have to ship gu-enabled
    files or not, but mixing them is not allowed. Searched for cases in which
    that may be happening in my phone and found none. Fixes LP: #1604029.

  [ Tim Peeters ]
  * Configure colors of the buttons in the ActionBar and PageHeader through
    their Styles. Fixes LP: #1597774.
  * Fix assigning of constant values to a grouped property in StyleHints.
    Fixes LP: #1602836.
  * Fix list view keyboard navigation for RightToLeft and BottomToTop
    directions. Fixes LP: #1605634
  * Use external QML files instead of embedded QML strings for autopilot tests.
    Fixes LP: #1578319
  * Fix failing autopilot header tests.
  * Fix ActionSelectionPopover Autopilot CPO tests.

  [Christian Dywan ]
  * Include indexes in "offline" docs and filter out link errors.
    Fixes LP: #1603937.
  * Increase focus ring thickness from 1dp to 2dp. Fixes LP: #1602690.
  * Add a snippet about tests to the toplevel README
  * Enter/Return to trigger, Space to expand ComboButton. Fixes LP: #1523817.
  * Initialize engine variable before using it in the _engine case.
  * Use Qt.rgba instead of #0000 checking ListItem default color.
    Fixes LP: #1560004
  * Don't use a different .desktop file in the gallery tests.
    Fixes LP: #1578319
  * Update scaling docs, set QT_SCALE_FACTOR and unset GRID_UNIT_PX.
    Fixes LP: #1610208

  [ Florian Boucault ]
  * Sections: load Icons asynchronously.

 -- Zoltán Balogh <email address hidden> Sun, 14 Aug 2016 09:06:46 +0000

Changed in ubuntu-ui-toolkit (Ubuntu):
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.