How to localize numbers for iPhone app?

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

    NSLocalizedString is going to be reading from a string table in your app bundle. Therefore, the list of languages you need to support is known at compile time. Rather than worry about how to code for every possible language, just support the ones you are supporting.

    If your translator comes to you and says that, to support Martian, you need a separate spelling for even and odd numbers, you can adjust your code then, to something like:

    if (objectList.count == 1) {
        NSLocalizedString(@"ObjectCount1", @"display one");
    } else if (objectList.count % 2 == 0) {
        NSLocalizedString(@"ObjectCountEven", @"display even");
    } else if (objectList.count % 2 == 0) {
        NSLocalizedString(@"ObjectCountOdd", @"display odd");
    }
    

    en.lproj/Localizable.strings:

    ObjectCount1 = "1 object";
    ObjectCountEven = "%d objects";
    ObjectCountOdd = "%d objects"; // same as ObjectCountEven
    

    mars.lproj/Localizable.strings:

    ObjectCount1 = "1 object-o";
    ObjectCountEven = "%d object-e";
    ObjectCountOdd = "%d object-o"; // same as ObjectCount1
    

    Sorry if this sounds less than ideal, but human languages are messy and irregular, so it's a waste of time to try to find an elegant, unified, common solution for them all. There isn't one.

提交回复
热议问题