Reputation: 2816
I'm not sure how to properly use NSNumberFormatter for the following case and properly keep the localization/internationalization.
Output in English:
0 bananas
1 banana
5 bananas
What should I do to properly keep internationalization/localization?
Upvotes: 1
Views: 262
Reputation: 7966
I just posted JJPluralForm, an adaptation for Mozilla's PluralForm.
With that, you wouldn't have to worry about doing all the if-else
, switch-case
code to do localizing in your code, which becomes impossible to maintain as your number of localizations grow (Russian has 3 plural rules, Arabic has 6, etc. etc.).
Something like the example you gave could be handled with:
[[JJPluralForm sharedManager] pluralStringForNumber:numberOfBananas
withPluralForms:NSLocalizedString(@"N_BANANAS_PLURAL_STRING", @"")
localizeNumeral:YES];
Each Localizable.strings file then localize N_BANANAS_PLURAL_STRING
as a semicolon separated list of plural forms. For English, that would be "%@ banana;%@ bananas"
.
Check out the project for more details.
Upvotes: 0
Reputation: 90531
Internationalization of plurals is a hard problem. I'm no expert, but various languages have various schemes for plurals beyond English's simple "1 is singular, all others are plural". Some languages have different forms for 0, 1, 2, 3, and >3. Given the complexity of languages, there are probably even more complex schemes.
Nothing in Cocoa, including NSNumberFormatter
, provides any help beyond just the normal string localization of NSLocalizedString
and friends. So, you might use NSLocalizedString
with keys like "0 bananas", "1 banana", "2 bananas", "3 bananas", and "%d bananas". Then, use a switch to load the one for the actual quantity of bananas that you wish to format.
Another approach is to punt and use a string like "bananas: %d".
Or you can combine techniques by providing localized strings for 0, 1, and >1, and suggest to translators that they switch to the punt style if those forms aren't sufficient for their language.
Upvotes: 4
Reputation: 5558
Ken answered well.
I would add this.
In first approximation, it's better to give full phrases to the translators as grammar varies a lot between languages. But this is not enough. You must be prepared to handle some requests from the translation team. Per example in French, the boat is translated to le bateau, the plane into l'avion (and not le avion). Pretty often, translators uses tricks to avoid such a situation, introducing special cases, but this isn't always possible nor desirable.
Work with the translation team to find the best solutions, and generalize the specific cases to all languages if you can so the code is kept language neutral. Several identical strings are better than language specific cases in the code.
With the time and experience, you'll get used to this. I worked more than 10 years on softwares localized in many languages (between 7 and 23 I think, not completely sure about 23 tho ;). You'll learn where things become problematic and some specifics about these languages. There's not that many problems by the way: plurals, genres, contractions are the most common. Formats are usually well handled by the system locales API. This is very interesting!
Upvotes: 1