Tuesday, 29 April 2008

Localizing "file(s)" using ngettext

We all know the problem: We have a list of files, and we want to have a label that indicates the number of files.

In English, it's quite straightforward:

Label1.Caption:=IntToStr(FileCount)+' file(s)';

The usually internationalized version looks like this:

resourcestring FileCountStr='%d file(s)';


Looks easy, right? It's not. In Polish, there are three plural forms:

1 plik
2 pliki
5 plików

Even french is different with regard to plural forms. In English, we use the plural form for zero (0 files, 1 file, 2 files), whereas the french use singular (0 fichier, 1 fichier, 2 fichiers).

With GNU gettext for Delphi, the solution is quite straightforward:

Label1.Caption:=Format(ngettext('%d file','%d files',FileCount),[FileCount]);

Ngettext() will automatically select the right plural form in the local language, even if there are 1, 2, 3 or 4 plural forms, and the translations tools (poedit and kbabel) provide good features for the translator to specify each plural form in good ways.

No comments: