How to localize numbers for iPhone app?

前端 未结 5 1276
南方客
南方客 2021-02-08 14:41

In my iPhone app, I need to display object counts which I then localize, since English makes the distinction of singular and plural, I do the following

// pseudocode

5条回答
  •  [愿得一人]
    2021-02-08 14:46

    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.

    Something like the example you gave could be handled with:

    [[JJPluralForm sharedManager] pluralStringForNumber:numberOfObjects
                                        withPluralForms:NSLocalizedString(@"N_OBJECTS_PLURAL_STRING", @"")
                                        localizeNumeral:YES];
    

    Each Localizable.strings file then localize N_OBJECTS_PLURAL_STRING as a semicolon separated list of plural forms. For English, that would be "%@ object;%@ objects".

    Check out the project for more details.

提交回复
热议问题