How to localize numbers for iPhone app?

前端 未结 5 1341
南方客
南方客 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:57

    Smartling has released an open source framework to support plurals following the CLDR standard.

    https://github.com/Smartling/ios-i18n

    To use it you add a special marker in your keys that follow CLDR style labeling and call SLPluralizedString instead of NSLocalizedString. At runtime the library will pick the right string to use based on the language the app is currently running in.

    So your English .strings file would have:

    "%@ apples##{one}" = "One apple";
    "%@ apples##{other}" = "%@ apples";
    

    The Russian .strings file:

    "%@ apples##{one}" =   "%@ яблоко";
    "%@ apples##{few}" =   "%@ яблока";
    "%@ apples##{many}" =  "%@ яблок";
    "%@ apples##{other}" = "%@ яблока";
    

    and the code to call it could look like:

        NSString *s2 = [NSString stringWithFormat:SLPluralizedString(@"%@ apples", number, nil), number];
    

    Note this example demonstrates one of the great things about this library - it lets your write your original strings or translations with expressive language that might not even use the format specifier.

    At this writing I am a Product Manager with Smartling.

提交回复
热议问题