Search code examples
ioslocalizationapple-push-notifications

iOS - APNS how to use loc_key in payload


I have a message string i want to localize before i let APNS send a message to devices. I wish i could see the json payload itself to make me understand the structure instead of the device parsing it out for me. but anyway, how do i use the loc_key and loc_args argument ? from the docs i found:

loc-key string A key to an alert-message string in a Localizable.strings file for the current localization (which is set by the user’s language preference). The key string can be formatted with %@ and %n$@ specifiers to take the variables specified in the loc-args array. See Localized Formatted Strings for more information.

loc-args array of strings Variable string values to appear in place of the format specifiers in loc-key. See Localized Formatted Strings for more information.

and it was offical doc is here

I need a concrete example of how to localize a string and what is loc_args ?

I am just guessing here, but if i have a string localized like this: mystring="cool localized message" then the loc_key will be "mystring", is that right ?


Solution

  • loc-key is a string that needs to match a key in your Localizable.strings file inside the app bundle. For example if the loc-key is "my-push.hello-world" and you have the following line in your Localizable.strings file:

    "my-push.hello-world" = "Hello, World!";
    

    the push message displayed to the user would read "Hello, World".

    log-args are strings that will be replaced in the message, like your have it in [NSString stringWithFormat:@"blabla %@"];

    Apples example is this:

    Push Notification Payload:

    "alert" : { 
        "loc-key" : "GAME_PLAY_REQUEST_FORMAT",
        "loc-args" : [ "Jenna", "Frank"]
    }
    

    Localizable.strings content:

    "GAME_PLAY_REQUEST_FORMAT" = "%@ and %@ have invited you to play Monopoly";