I am internationalizing my app and am looking for a solution to how to deal with plural forms. I ran across Mozilla\'s PluralForm project, which essentially abstracts away the i
From looking at Mozilla's implementation of PluralForm.jsm it should be quite easy to implement your own version of the PluralForm.get
in Objective-C.
First you retrieve the plural rule number from your localization and use it to select appropriate getter, that implements the rule. There are 16(1) expressions that return the index of proper plural form to use for given numeral. Luckily they appear to be written in JavaScript subset that is also valid C code... Boolean and ternary expressions with some modulo operations mixed in.
Plural forms are supplied by your translators as semicolon separated list in the localized string for the given word. The order of the forms is described in the list of plural rules. You split them by the semicolon and using the index from the getter select the matching form.
Technically, no problem. The only issue is that I don't know how to properly derive your code and respect the MPL license.
(1) Implementation contains 17th rule for Breton, but its forms are not documented yet, so...
Smartling (a Translation Management Platform) has released an open source library for managing plurals in iOS. After having dealt with numerous clients having iOS plurals problems and not finding a solution that would work how we and our clients wanted, we decided to build our own.
The library takes the keys for the plural strings and extends them to contain the plural form based on the CLDR plural rules. The library provides an alternative function to NSLocalizedString called SLPluralizedString to do the look up.
An English source file would look like:
"%d Items Processed##{one}" = "1 Item Processed";
"%d Items Processed##{other}" = "%d Items Processed";
And you would use the SLPluralizedString function to look up the string:
SLPluralizedString(@”%d Items Processed”, numItems, @”Number of items processed”);
A translated Russian file would have the appropriate number of keys/values for the language:
"%d Items Processed##{one}" = "%d элемент обработан";
"%d Items Processed##{few}" = "%d элемента обработано";
"%d Items Processed##{many}" = "%d элементов обработано";
"%d Items Processed##{other}" = "%d элемента обработано";
The actual code wouldn't need to change depending on the language. One function would work across all languages and return the appropriate translated string.
Feel free to share comments, improvements, etc.