views:

481

answers:

5

Is there an easy way to convert the number 1, 2, 3, ... to "1st", "2nd", "3rd", ..., and in such a way that I can give the function a language and have it return me the correct form for the language I'm targeting? Either standard C++ (stl or boost OK), MFC or ATL, win32 api or a small, single-purpose and free library that I can download from somewhere. Thanks.

A: 

Here is the piece of code on CodeProject that does the job. Haven't tried it on my own.

Sergey Kornilov
A: 

I've spend quite some time researching this, because it's too large a project to get right myself. It looks like the ICU library is the only one that provides this functionality in a somewhat comprehensive way (http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html). I'm not too keen on incorporating a huge library like that, though. I'll keep on looking and I'm still open to suggestions.

Roel
+2  A: 

I doubt whether it is possible at all, since in many languages this form will depend on the context, like gender or case of the noun it describes and different languages will require different kind of context information to allow to determine the correct form.

EDIT: E.g. in polish it is "5-ta klasa" (5th class) vs. "5-ty miesiąc" (5th month) vs. "w 5-tym miesiącu" (in the 5th month).

Jacek Szymański
Do you have any specific languages or examples in mind? AFAIK, but my practical experience is limited to Western European languages, the ordinal (1st, 2nd) is always the same, and only the fully spelled out form is conjugated (eg el secundo mano/ la secunda dia in Spanish).
Roel
Interesting, thanks for that. It looks like I will be using the ICU library to get ordinals but I don't know how they handle this. I'll test that out.
Roel
A: 

Did you look up the CLDR repository on the Unicode site? I don't know if they have this kind of thing but since it's probably the most comprehensive locale data repository out there, it's probably worth a look. http://www.unicode.org/cldr/

Serge - appTranslator
Yes, CLDR has ordinal information and ICU uses it as a data source. I'd have to write my own code around it though, or split the ordinal-related code from ICU into a separate sub-library or so. Both not very attractive.
Roel
A: 

Since you use C++, I assume you could use GNU gettext (there's a Windows port as well) for all the translations, or at least get the idea how they solved it. Here's the relevant manual page on plural forms that explains the problem (which you already found, but in more detail) and their solution:

http://www.gnu.org/software/automake/manual/gettext/Plural-forms.html

Milan Babuškov
The only functionality I could find in gettext is for plurals, not ordinals. I could build my own solution using something similar to their approach but I don't want that; I'd need access to many native speakers. I'm not that motivated :)Besides, gettext is GPL which makes it unsuitable for my use.
Roel