How to localize numbers for iPhone app?

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

    There is an even more powerful solution to this problem. Take a look at the Tr8n library from TranslationExchange.com

    https://github.com/tr8n/tr8n_objc_clientsdk

    The library uses TranslationExchange's TML (Translation Markup Language), which makes internationalization process very easy. First of all, you don't even need to deal with Strings XML files ever again, really... - the Tr8n SDK will create and manage your String files for you on the fly - you will never look at them again.

    Your particular example would simply be:

    Tr8nLocalizedStringWithTokens(@"{count || object}", @{@"count": objectList.count}) 
    

    Tr8n library will automatically pick the right plural form for you in any language. Hah? Yes, it is magical. The full form of the above token example is actually:

    {count:number || one: object, other: objects}
    

    That means that "count" token is of a numeric type that is mapped to English plurals using keywords "one" and "other".... But Tr8n is smart enough to not have you type all that. It is also smart enough to map the sequence of parameters to the appropriate rule values as well. And, of course, it knows that "count" is associated with the numeric rules through naming convention. So it simply becomes:

    {count || object}
    

    Btw, since you've mentioned Russian, the Russian translation for the above would simply be:

    "{count || object}" = "{count || объект, объекта, объектов}"
    

    This example was too easy, let's take a look at a more interesting one:

    Tr8nLocalizedStringWithTokens(
       @"{user} uploaded {count || photo} to {user | his, her} photo album.", 
       @{@"user": user, @"count": 5}
    ) 
    

    First of all, good luck translating this sentence using the standard iOS i18n library (or any other library for that matter)... it's a joke - but really, there is no way to do it using anything, but Tr8n.

    The above TML translated to Russian would simply be:

    @"{user || загрузил, загрузила} {count || фотографию, фотографии, фотографий} в свой фотоальбом." 
    

    Here we deal with gender rules the same way we deal with numeric rules. But instead of "one", "few", "other", we have "male", "female", "unknown" - well, each language may have different gender and numeric rules. Tr8n will deal with it, so you don't have to.

    Alright, let's take it to the next level. You have decided that you must have the number of photos to be bold. Piece of cake.

    Tr8nLocalizedAttributedStringWithTokens(
      @"{user} uploaded [bold: {count || photo}] to {user | his, her} photo album.", 
      @{
        @"user": user, 
        @"count": 5, 
        @"bold": @{@"font":@{@"name": @"system", @"size": @12, @"type": @"bold"}}
      }
    ) 
    

    [bold: ... ] is a decoration token. Did you notice that we switched the macro to the AttributedString version? This macro would actually produce an NSAttributedString using the native decoration mechanism of iOS. Can you guess what the translation in Russian would be?

    @"{user || загрузил, загрузила} [bold: {count || фотографию, фотографии, фотографий}] в свой фотоальбом."
    

    Btw, you can predefine all your decoration tokens elsewhere, so you don't have to keep defining them every time.

    Let's do just one more final example... Say, you have a newsfeed story in the following form:

    Tr8nLocalizedAttributedStringWithTokens(
      @"{actor} sent {target} [bold: {count || gift}].", 
      @{
        @"actor": user1, 
        @"target": user2, 
        @"count": 5
      }
    ) 
    

    It doesn't look interesting in English. But it does in Russian or any other languages that supports language cases. The name of {target}, if it happens to be in Russian, will actually need to use Russian Dative Language Case.

    http://en.wikipedia.org/wiki/Dative_case

    If you don't speak Russian, you probably don't need to know about it. But your Russian translators should. Let's see the Russian translation then:

    @"{actor || подарил, подарила} {target::dat} [bold: {count || подарок, подарка, подарков}].", 
    

    Tr8n is smart enough to use its powerful language rules engine and apply the Dative language case to the Russian names passed through the {target} token...

    That was a bit of a long answer for a simple numeric question. Thank you for reading it that far. Hope it helps.

    Disclaimer: I am the creator of Tr8n framework and TML language. If you have any questions, please ping me and I would love to help you out with all your translation questions.

提交回复
热议问题