我的应用运行良好,它使用本地通知。
我已经决定现在对该应用程序进行国际化,并且除了在设备上更改语言之前在语言上设置的通知之外,其他所有功能都可以正常工作。
[我从包含本地化字符串的数组中填充了通知中的消息,所以我发现当用户更改设备的语言时,通知中的字符串也会更改,但是我错了。
如何最好地解决这个问题?我的NSString文本也应该也是NSLocalizationString吗?
我的通知代码:
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];
我的NSString文本也应该是NSLocalizationString吗?
是,我会这样做。
用[alertTimes objectAtIndex:i]
替换NSLocalizedString(@"alertTimes",[alertTimes objectAtIndex:i])
。我假设您在alertTimes
数组中存储与本地化字符串匹配的字符串。
刚刚碰到同一问题,发现Apple文档UNMutableNotificationContent表示使用NSString. localizedUserNotificationString(forKey:arguments:)
函数,该函数推迟加载本地化的字符串,直到显示通知为止。这样,即使用户在安排通知和正在发送的通知之间更改语言,该字符串也将被正确地本地化。
“您在通知警报中显示的字符串应针对当前用户进行本地化。尽管您可以使用NSLocalizedString宏从应用程序的资源文件中加载字符串,但是更好的选择是使用localizedUserNotificationString(forKey:arguments NSString :)方法localizedUserNotificationString(forKey:arguments :)方法延迟了本地化字符串的加载,直到通知被传递为止,因此,如果用户在通知被传递之前更改了语言设置,则警报文本将更新为用户的当前语言,而不是计划通知时设置的语言。“`