Localization of singular/plural words - what are the different language rules for grammatical numbers?

前端 未结 3 643
粉色の甜心
粉色の甜心 2021-02-05 18:39

I have been developing a .NET string formatting library to assist with localization of an application. It\'s called SmartFormat and is open-source on GitHub.

3条回答
  •  走了就别回头了
    2021-02-05 18:48

    The approach you have taken might work on most cases in English and Spanish but most likely fails on many other languages. The problem is that you only have one pattern that tries to solve all grammatical numbers.

    var message = "There {0:is|are} {0} {0:item|items} remaining";
    

    You need one pattern for each grammatical gender. Here I have combined two patterns together into a single multi pattern string.

    var message = PluralFormat("one;There is {0} item remaining;other;There are {0} items remaining", count);
    

    English uses two grammatical number: singular and plural. one starts singular pattern and other starts plural pattern.

    When translated for example to Finnish that uses the same amount of grammatical numbers you would use

    "one;{0} kappale jäljellä;other;{0} kappaletta jäljellä"
    

    However Japanese use only one grammatical number so Japanese would only use other. Polish uses three grammatical numbers so it would contains one, few and many.

    Secondly you would need the proper rules to choose the right pattern amount multiple patterns. Unicode consortium's CLDR contains the rules in XML file.

    I have implemented an open source library that uses CLDR rules (converted from XML into C# code and included into the library) and multi patterns strings to support both grammatical numbers and grammatical genders.

    https://github.com/jaska45/I18N

    Using this library your samples turns into

    var message = MultiPattern.Format("one;There is {0} item remaining;other;There are {0} items remaining", count);
    

提交回复
热议问题