iOS - APNS how to use loc_key in payload

2019-06-06 07:29发布

问题:

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 ?

回答1:

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";


回答2:

if loc key is there, loc_key refers to a key in your localizable file...

e.g. if loc_key=XYZ, then there must be an entry "XYZ"="translated XYZ"; in the apps' localizable file.

loc_args is an array of strings that is to be inserted into the translated string as if you had used stringWithFormat:

e.g loc_args=["DOMI", "ME"] inserted into a translated XYZ like %@ is %@ results in "DOMI is ME"