There are two ways to display localized text in a push notification in iOS:
Localize the message in your server
In this case, you must send the device language to your server. The code you need to add to your iOS app would be similar to the following:
NSString *preferredLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
const char *langStr = [preferredLanguage UTF8String];
[self sendCurrentLanguage:langStr]; // Method that communicates with your server
Then you can send the notification message in the appropriate language by using the alert
key in the notification JSON payload.
Send a localization string with the notification payload
You can send the localized string in the payload. The alert
key accepts a child loc-key
key that you can use to send a localized string:
"alert" : {
"loc-key" : "My Localized String",
...
}
And then, in your Localizable.strings
file inside the correspondent language identifier, add the following:
"My Localized String" = "The localized string in the language you want.";
If you need to pass arguments to build the final localized string, you can pass it as a loc-args
JSON array in the notification payload, too:
"alert" : {
"loc-key" : "My Localized String",
"loc-args" : [ "First argument", "Second argument" ],
...
}
And, in your Localizable.strings
:
"My Localized String" = "The localized string with first argument %@, and second argument %@."
Or, if you need to change the positions:
"My Localized String" = "The localized string with second argument %2$@, and first argument %1$@.";