dustyburwell
dustyburwell

Reputation: 5813

Pluralising and Localizing strings in C#

I've got a C# WPF application I'm attempting to globalize with resx files. It works splendidly. I've run into a hitch, however. I've got a relatively simple solution for pluralisation where I have a singular and plural form of the string I'm displaying and I pick one based on the number of whatever things I'm talking about.

However, I've recently come to terms with the fact that some cultures have multiple plural forms. Has anyone come across a good solution to this problem in C#, let alone WPF?

The canonical example that I've seen for this problem comes from the Polish language. Polish has a singular, paucal, and plural form. This example is for the translation of the word "file":

Upvotes: 4

Views: 3210

Answers (4)

peterchen
peterchen

Reputation: 41106

I guess yo uare aware of gettext's plural form handling. But generally, I'd try to avoid it (as Yuliy said).

It's not only the nouns - phrases can change (e.g. in German "1 Datei konnte nicht gelöscht werden" / "2 Dateien konnten nicht gelöscht werden").

It is much more friendly and elegant than the problem-evasive "Dateien, die nicht gelöscht werden konnten: 2", but there's a tradeoff in how many ressources you have for localization.

Upvotes: 1

user146122
user146122

Reputation:

Localization like this takes consideration of the languages you want to translate to. Multiple plurals are fairly rare and I would imagine in most cases are interchangeable or context sensitive. Which unless it is being used in multiple places in your application you will not need to worry about. If you are doing this and a particular usage does create a grammatical error in plural usage then you need to add a new key across the board (all languages) for that one instance. Or, since you are detecting culture anyway, add an additional conditional for the affected language and use the single alternate plural form.

I would not suggest avoiding it as you can quickly lose the flow of natural language "Mins answered ago: 6".

Maybe you meant this in the first place but the far more common scenario is variations in syntactical placement across different cultures. For example, the string wanting to be localized "This page is viewed X times". You may want to make 3 localizable strings for this:

PageViewStart = "This page is viewed" PageViewEnd = "time" PageViewEndPlural = "times"

Then a simple pseudo-implementation would be

PageViewStart + pageCount.ToString() + pageCount == 1 ? PageViewEnd : PageViewEndPlural;

However in Dutch "Deze pagina is {0} keer bekeken" and in Korean "조회수 {0}". So you see you will immediatley run into problems with implementations on the multiple ways to format plural sentence structure across languages.

I purposely left a {0} in the examples as it alludes to my solution. Use a localization for the whole sentence in plural and non-plural.

PageView = "This page viewed 1 time." PageViewPlural = "This page viewed {0} times."

This way you can write the conditional (pseudo again depending on your implementation):

pageCount > 1 ? PageView : String.Format(PageViewPlural, pageCount.ToString());

The only thing is that your translators will need to be instructed as to the meaning and placement of the {0} token in the resx file.

Upvotes: 2

Yuliy
Yuliy

Reputation: 17718

Consider trying to just avoid the problem altogether. Instead of building sentences, try and build your UI to avoid the problem. Instead of saying "5 pages" try saying: "Pages: 5".

Upvotes: 4

AASoft
AASoft

Reputation: 1344

Mozilla has implemented this in Firefox 3, and they have a guide describing how to use their implementation here. Most notably, in the Developing with PluralForm section, they have a link

resource://gre/modules/PluralForm.jsm

to the source of their implementation. Must be opened from within Firefox 3 and higher.

I have not read through the whole thing, but this seems to be like a good place to at least get some ideas.

HTH.

Upvotes: 9

Related Questions