Pluralising and Localizing strings in C#

ぐ巨炮叔叔 提交于 2019-12-03 21:29:10

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.

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".

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!