Plural Form(s) in Translation(s)

zebreus1 pts0 comments

Plural Form(s) in Translation(s)

Trolltech<br>| Documentation<br>| Qt Quarterly<br>| " Inside the Qt 4 Containers<br>| Qtopia Core 4.2 Screen Drivers "

Plural Form(s) in Translation(s)

by Jan-Arve Sæther

Ever found yourself writing tr("%1 object(s) found") .arg(count) in<br>one of your applications? Qt 4.2 will introduce a powerful mechanism to handle<br>plurals in a graceful way that works for all languages and that requires<br>little extra work from the developer.

What's the Problem with Plural Forms?

How Does Qt 4.2 Address This Problem?

How Does It Work Under the Hood?

What's the Problem with Plural Forms?

You have most probably seen programs that use the same string for<br>singular and plural, using parentheses to combine the singular and<br>the plural forms into one string (e.g., "6 occurrence(s)<br>replaced").

Natually, it would be preferable to show "6 occurrences replaced"<br>with an 's', and "1 occurrence replaced" with no 's'. Some<br>developers solve this problem through code that looks like this:

tr("%1 item%2 replaced").arg(count)<br>.arg(count == 1 ? "" : "s");

This approach works for languages like English that form their plural<br>using 's', but as soon as we try to translate the application to<br>languages like Arabic, Chinese, German, Hebrew, or Japanese<br>(to name just a few), this breaks in a horrible way.

Developers who are slightly more sympathetic might write code that<br>looks more like this:

QString message;<br>if (count == 1) {<br>message = tr("%1 item replaced").arg(count);<br>} else {<br>message = tr("%1 items replaced").arg(count);

This code is definitely more internationalization-friendly, but it<br>still makes two assumptions about the target language:

It assumes that the target language has two grammatical numbers<br>(singular and plural).

It assumes that the plural form should be used in the "n = 0" case<br>(e.g., "0 items ").

These assumptions hold for many of the world's languages, including<br>Dutch, English, Finnish, Greek, Hebrew, Hindi, Mongolian, Swahili,<br>Turkish, and Zulu, but there are many languages out there for which<br>they don't.

Case in point: In French and Brazilian Portuguese (but not<br>international Portuguese, interestingly enough), the singular form is<br>used in conjunction with 0 (e.g., "0 maison", not "0 maisons"),<br>breaking assumption 2. In Polish, there are three grammatical<br>numbers:

Singular: n = 1

Paucal: n = 2--4, 22--24, 32--34, 42--44, ...

Plural: n = 0, 5--21, 25--31, 35--41, ...

For example, the Polish word dom ("house") has the paucal form<br>domy and the plural form domów. The table below shows the<br>rendition of "n house(s)" in English, French, and Polish for<br>different values of n.

EnglishFrenchPolish<br>0 houses 0 maison0 domów<br>1 house1 maison1 dom<br>2 houses2 maisons2 domy<br>3 houses3 maisons3 domy<br>4 houses4 maisons4 domy<br>5 houses5 maisons5 domów<br>21 houses21 maisons21 domów<br>22 houses22 maisons22 domy<br>24 houses24 maisons24 domy<br>30 houses30 maisons30 domów

Other languages have other rules:

Latvian has a specific grammatical number, the nullar, for<br>the "n = 0" case.

Dhivehi, Inuktitut, Irish, Maori, and a few other languages<br>have a dual form for the "n = 2" case.

Czech, Slovak, Lithuanian, and Macedonian have a dual, but they use<br>it according to more complex rules.

Slovenian has a trial in addition to the singular, dual, and plural<br>forms.

Romanian handles the "n >= 20" case differently from<br>the "n Arabic has six different forms, depending on the value of n.

Chinese, Japanese, Korean, and many other languages don't distinguish<br>between the singular and the plural.

This is just a partial list, but it clearly shows the complexity of<br>the problem.

How Does Qt 4.2 Address This Problem?

Qt 4.2 includes a QObject::tr() overload<br>that will make it very easy to write "plural-aware" internationalized<br>applications. This new overload has the following signature:

QString tr(const char *text, const char *comment, int n);

Depending on the value of n, the tr() function will return a<br>different translation, with the correct grammatical number for the<br>target language. Also, any occurrence of "%n" is replaced with n's<br>value. For example:

tr("%n item(s) replaced", "", count);

If a French translation is loaded, this will expand to "0 item remplacé",<br>"1 item remplacé", "2 items remplacés ", etc., depending on<br>n's value. And if no translation is loaded, the orignal string is used,<br>with "%n" replaced with count's value (e.g., "6 item(s) replaced").

To obtain a more natural English text, you need to load an English<br>translation.[1]<br>An English translation offers other advantages, such as<br>the possibility of editing the application's English user interface<br>without touching the source code.

When the application is ready to be translated, the developers must<br>run lupdate as usual to generate one or several .ts files<br>that can be edited using Qt Linguist. In Qt Linguist, the<br>translator can specify the target language by clicking<br>Edit|Translation File Settings . Specifying a target<br>language is necessary so that Qt Linguist knows...

plural replaced form translation count languages

Related Articles