Habré is full of articles, with examples in different languages, on how to properly handle numerals and to incline the nouns related to them. Let's see how this task
is solved in qt.
In the previous article
about localization, Qt mentioned the QObject :: tr () function and even told that you can insert placeholders% 1,% 2, etc. into the string.
However, unfortunately, no special placeholder is mentioned -% n. If you look closely at the signature of the function QObject :: tr
QString QObject::tr ( const char * sourceText, const char * comment = 0, int n = -1 )
you can see that it has a third parameter - some integer n, what is it?
')
Take the example from the previous article and simplify it a bit, we will only show the number of copied files:
void FileCopier::showProgress(int done)
{
label.setText(tr("%1 files copied.").arg(done));
}
As you can see, depending on the condition done == 1, we have to write either file or files. When translated into Russian, everything becomes even more complicated - the form becomes 3 (1 file, 2 files, 5 files copied) and the condition becomes more complicated.
And here comes the placeholder% n and the third argument to the tr () function.
To make it all right, let's write the showProgress function as follows:
void FileCopier::showProgress(int done)
{
label.setText(tr("%n files copied.", "", done));
}
And when translating, in a linguist, we will have the opportunity to set several options for translating this string for different values of% n.
For the English language there will be two options - Singular and Plural. Singular will look like this: "% n file copied.", And Plural like this: "% n files copied."
For Russian there are three - Singular, Dual and Plural.
Singular: “Copied% n file.”
Dual: "Copied% n file."
Plural: "Copied% n files."
This is where the work of the translator and programmer ends. Qt itself knows for which numbers what form to use in this particular language. When executing the program, the tr () function, depending on the current locale and the third parameter, will return the desired string.
If you are interested, here are the
formulas for some languages used by Qt.