axuno / SmartFormat

A lightweight text templating library written in C# which can be a drop-in replacement for string.Format
Other
1.1k stars 105 forks source link

Pluralisation for Polish #41

Closed odie2 closed 9 years ago

odie2 commented 9 years ago

Hello, I am translating GitExtensions, which use SmartFormat.NET There are some texts like "{0} {1:month|months} ago". But for Polish I need 3 forms like:

"nplurals=3; plural=n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;" (for example .po of FileZilla - 1 / 2, 3, 22 ... / 0, 5, 6, 7...)

or something like that: http://www.mediawiki.org/wiki/Localisation#.E2.80.A6on_numbers_via_PLURAL

So I could write "{0} {1:miesiąc|miesiące|miesięcy} temu". For now I have to use unclean "{0} {1:miesiąc|miesięc(e/y)} temu" :/

Could you try add this?

Thanks in advance, Greetings, Maciej

scottrippey commented 9 years ago

Language-specific logic is already a feature. Specifically, Polish uses the following code:

        public static int Polish(decimal value, int pluralCount)
        {
            // Three forms, special case for one and some numbers ending in 2, 3, or 4
            if (pluralCount == 3)
            {
                return (value == 1) ? 0 : (value % 10 >= 2 && value % 10 <= 4 && (value % 100 < 10 || value % 100 >= 20)) ? 1 : 2;
            }
            return -1;
        }

Is this what you're asking for? To utilize this code, you will need to pass a Polish CultureInfo as the first parameter to Smart.Format.

odie2 commented 9 years ago

I checked current rule but it is wrong. Correct rule is that from FileZilla - perfectly chosing form to number. I think is it also the best for most of languages.

Could you update Polish rule or give it me so I could ask GitExtensions developers to update? Thanks!

scottrippey commented 9 years ago

Oh, are you're saying that the current Polish rule is wrong? I'd be happy to correct it. What is the correct rule?

The current rule has "few" defined as value % 10 >= 2 && value % 10 <= 4 && (value % 100 < 10 || value % 100 >= 20), which is numbers like 2, 3, 4, 22, 23, 24, 32, 33, 34, etc.

According to the rule you posted, "few" is defined as n % 10 >= 2 && n % 10 <= 4 && (n % 100 = 20), but that doesn't look right (this expression contains an assignment). Maybe a copy-and-paste error?

Anyway, what is the correct rule?

scottrippey commented 9 years ago

According to my sources (http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html), the current Polish rule is correct. nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);

odie2 commented 9 years ago

Take a look into FileZilla .po file with Polish and search "%s file" string, you will see what I am talking about.

http://maciej.kuzniczysko.pl/files/filezilla.po

And better examples what I need - based on "x months ago":

1 miesiąc temu 2 miesiące temu 3 miesiące temu 4 miesiące temu 5 miesięcy temu 6 miesięcy temu 7 miesięcy temu [... x miesięcy temu ...] 22 miesiące temu 23 miesiące temu

etc.

I can't get it by SmartFormat.NET:

"{0} {1:miesiąc|miesiące|miesięcy} temu" give me -> 4 miesięcy temu 5 miesięcy temu

"{0} {1:miesiąc|miesięcy|miesiące} temu" give me -> 4 miesiące temu 5 miesiące temu

Or maybe you have another rule to get script know what rule should be used, that I didn't know?

scottrippey commented 9 years ago

On a good note:

And hopefully this will help you out:

KindDragon commented 9 years ago

Hi. Should it work without culture info, if correct country set in windows regional settings?

scottrippey commented 9 years ago

@KindDragon Right now, the way it works is:

  1. If you explicitly supply the CultureInfo, it works as I described.
  2. If not, the default language rule is English.
  3. Unfortunately, changing the default language is not easy -- you must replace the PluralLocalizationFormatter with a new PluralLocalizationFormatter("pl")
  4. I don't have extensive experience with localization, so I'm not sure if it makes sense to use the Current Culture Info ... what if the computer's language is not the language of the application? Can this happen? We wouldn't want the wrong language rules to be used.
scottrippey commented 9 years ago

I've added the Polish phrases to the unit tests. Take a look, does it look good?

odie2 commented 9 years ago

As regards my side - fine, that is exactly I need.

KindDragon commented 9 years ago
  1. Unfortunately, changing the default language is not easy -- you must replace the PluralLocalizationFormatter with a new PluralLocalizationFormatter("pl")
  2. I don't have extensive experience with localization, so I'm not sure if it makes sense to use the Current Culture Info ... what if the computer's language is not the language of the application? Can this happen? We wouldn't want the wrong language rules to be used.

My only problem it's hard to pass CultureInfo to each Smart.Format. Can I change default PluralLocalizationFormatter to formatter for my current localization?

BTW, would be nice if you create a wiki page with a description of what plural rules are supported, to what countries and an example of how to use it, so that I could give link to localizers.

KindDragon commented 9 years ago

BTW, would be nice if you create a wiki page with a description of what plural rules are supported, to what countries and an example of how to use it, so that I could give link to localizers.

I found that page https://github.com/scottrippey/SmartFormat.NET/wiki#pluralisation Would be nice if that page have example for language with more complex rule.

scottrippey commented 9 years ago

I've just added a way to change the default:

Smart.Default.GetFormatterExtension<PluralLocalizationExtension>().DefaultTwoLetterISOLanguageName = "pl";

This change is merged to `master", and will be included in v1.2. Unfortunately, I'm still trying to figure out how to manage "release management", so I can't say when 1.2 will be released ... but it shouldn't be long.

KindDragon commented 9 years ago

Thank you

scottrippey commented 9 years ago

@odie2 As a Polish speaker, I have a question for you. Although Polish usually has 3 plural forms, does it ALWAYS have 3 different plural forms? Or are there scenarios where there's only 2 plural forms? (Or where the 2nd and 3rd forms are the same)

SmartFormat's plural rules are currently dependent on the number of forms supplied. So, the Polish rule says "if there are 3 forms, choose [one|few|other]" but there's also a rule that says "if there are 2 forms, choose [one|other]".
And that's how I treat all languages. If there are 4 plural forms, such as "[one|two|few|other]", then it also supports "[one|two|other]" and "[one|other]".

I'd like to determine if this is a useful feature, or if it doesn't really make sense. All other Plural Rules that I can find seem to require all 3 forms, so you'd end up repeating yourself if there were only 2 forms.

odie2 commented 9 years ago

Hmm, we have 2 plurals and one singualar:

Dog(s): 1 pies (singualar) 2 psy (1st plural) 5 psów (2nd plural)

Branch(es): 1 gałąź 2 gałęzie 5 gałęzi

Comment(s): 1 komentarz 2 komentarze 5 komentarzy

As I remember, there are no the same word for two plurals, if exists very rarely.

I suggest you for others languages fill unfilled plurals with previous, so {1:one|two} will be {1:one|two|two}. For most languages that should be fine. The question is for {1:one|two|three} to {1:one|two|three| two or three? } for "nicer" languages.

I think you could check PO examples (maybe predefined for certains languages - option in translation properties).