2

我的应用程序运行良好,它使用本地通知。

我决定现在将应用程序国际化,并让一切工作正常,除了在更改设备上的语言之前设置为语言的通知。

我从包含本地化字符串的数组中填充通知中的消息,所以我认为当用户更改设备的语言时,通知中的字符串也会改变,但我错了。

如何最好地解决这个问题?我的 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];
4

3 回答 3

1

我的 NSString 文本也应该是 NSLocalizationString 吗?

是的,我会那样做。

替换[alertTimes objectAtIndex:i]NSLocalizedString(@"alertTimes",[alertTimes objectAtIndex:i])。我假设您将字符串存储在alertTimes与您的本地化字符串匹配的数组中。

于 2011-01-05T21:54:41.610 回答
0

刚刚遇到同样的问题,发现 Apple Docs for UNMutableNotificationContent说要使用该NSString. localizedUserNotificationString(forKey:arguments:)函数,该函数将加载本地化字符串直到通知出现。这样,即使用户在安排通知和发送通知之间更改语言,字符串也会正确本地化。

“您在通知警报中显示的字符串应该针对当前用户进行本地化。虽然您可以使用 NSLocalizedString 宏从应用程序的资源文件中加载字符串,但更好的选择是使用本地化用户通知字符串(forKey:arguments:) NSString 的方法。localizedUserNotificationString(forKey:arguments:) 方法延迟本地化字符串的加载,直到传递通知。因此,如果用户在传递通知之前更改语言设置,则警报文本将更新为用户的当前语言而不是安排通知时设置的语言。"`

于 2020-04-27T18:14:53.780 回答
0

本地化本地通知时始终使用本地化用户通知字符串(forKey:arguments:)。

本地化的“陷阱”是静态字符串 fromNSLocalizedString无法正常工作,因为用户可能会在安排通知后切换语言。这将导致通知警报中显示错误的语言。

例如,在en-US设置了英文副本字符串的情况下安排了通知,在触发通知之前,用户切换了语言,jp因为生成了静态字符串,NSLocalizedString在触发通知之前无法更改它。

localizedUserNotificationString(forKey: arguments:) should be used to localize string text for a notification content, https://developer.apple.com/documentation/foundation/nsstring/1649585-localizedusernotificationstring. Where a localized string value is created dynamically from a localized string resource when the notification is about to be displayed.

于 2020-11-16T02:33:17.353 回答