localization in iOS notifications?
-
25-09-2019 - |
Question
I have my app running nicely and it uses local notifications.
I have decided to internationalize the app now and have everything working just fine except notifications which were set on a language before changing the language on the device.
I populate the messages in the notification from an array which contains localized strings, so I figured that when the user changes the language of the device the string in the notification would also change but I was wrong.
How best to tackle this issue? Should my NSString text also be NSLocalizationString ?
My notification code:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [alertTimes objectAtIndex:i];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSString *text = [alertText objectAtIndex:i];
// Notification details
localNotif.alertBody = text;
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Solution
Should my NSString text also be NSLocalizationString?
Yes, I would do that.
Replace [alertTimes objectAtIndex:i]
with NSLocalizedString(@"alertTimes",[alertTimes objectAtIndex:i])
. I am assuming that you storing strings in the alertTimes
array that match your localized string.