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 issue of plurals for internationalization. Here's how it works:
There are a number of "plural rules" that languages can follow. Each language fits into one and only one of the many plural rules (Mozilla's documentation has 15 potential plural rules). For example, in languages with rule 0 (such as Chinese), there are no plural forms, and so there is only one needed word form. In languages with rule 1 (such as German), each word has two distinct plural forms. And so on, depending on how the rules match up.
When you're programming an internationalized string, you not only pass the string to be internationalized but the number that you want to pluralize the noun with:
print("You have " + num + " " + PluralForm.get(num, downloads) + ".");
In Objective-C, one would do it rather differently:
NSString *str = [NSString stringWithFormat:NSLocalizedStringFromTable(@"%d Items"), myNumber];
Of course, this above Objective-C example does not solve the issue of plural forms.
When the PluralForm.get function gets a call, depending on the language currently in use, it applies the plural form, and pulls the proper internationalized string from the strings file depending on which specific sub-rule that it should follow.
The advantage to PluralForm is clear (at least to me). It abstracts away the actual language rules from the coder, so that they do not need to be duplicated in all locations where you have to deal with a plural form.
I'd like to use a system like when when programming my iPhone apps. Is there a project or code available which ports Mozilla's PluralForm to objective-c?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With