Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

ios - Change language of alert in banner of Push Notification

I am facing problem to change the language of alert in banner when push comes. Actually i am working on an app which works in two language. One is English and second is Norwegian. The push I am receiving from my web server end and what the string it has in alert key is displayed in banner when push comes and you are outside of the app. But as a requirement we want that if I change the language from setting from English to Norwegian then when push comes its banner's alert string would also change to Norwegian. Will it be possible at my end or i have to change it from server whenever i change language?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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$@.";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...