我们有一个本地化的应用程序。荷兰的许多用户将他们的设备设置为英语,并将其作为荷兰语的第二语言。我们的应用程序中有一个语言选择菜单,因为 99.9% 的用户想要荷兰语交通信息而不是英语。因此,如果首选语言之一是荷兰语,我们将语言设置为荷兰语。
除了 UILocalNotifications 和设备语言是英语(第二个是荷兰语)外,这很好用。我们的应用程序语言是荷兰语(但对于不同于系统语言的任何其他语言应该是相同的)。
这就是我们如何将语言设置为特定选择的语言,在此示例中为荷兰语(通过使用来自此线程的答案 How to force NSLocalizedString to use a specific language ):
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:language, nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
这是我们发送 UILocalNotification 的方式:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = message;
if(notificationCategory != NULL)
localNotification.category = notificationCategory;
if(referenceDic != NULL)
localNotification.userInfo = referenceDic;
if(title != nil && [localNotification respondsToSelector:@selector(setAlertTitle:)])
{
[localNotification setAlertTitle:title];
}
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
NSString var *message 是一个 LocalizedString,调试时这个字符串是荷兰语:
(lldb) po localNotification
<UIConcreteLocalNotification: 0x15ce32580>{fire date = (null), time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Wednesday 14 October 2015 at 09 h 56 min 47 s Central European Summer Time, user info = (null)}
(lldb) po localNotification.alertBody
Flitsmeister heeft geconstateerd dat je niet meer onderweg bent en is automatisch uitgeschakeld.
(lldb) po localNotification.alertTitle
nil
现在 iOS 收到此 localNotification 并尝试将其翻译成英语。此翻译有效,因为该字符串在本地化文件中。
如果消息不在翻译文件中(因为它有一个数字)或者如果我在消息末尾添加一个空格,它不会在本地化文件中找到该字符串并显示荷兰语通知。
iOS 试图将 LocalNotification 翻译成系统语言(英语)而不是应用程序语言(荷兰语),这似乎很奇怪。
Apple 的文档是这样说的:
alertBody Property The message displayed in the notification alert.
Assign a string or, preferably, a localized-string key (using NSLocalizedString) as the value of the message. If the value of this property is non-nil, an alert is displayed. The default value is nil (no alert). Printf style escape characters are stripped from the string prior to display; to include a percent symbol (%) in the message, use two percent symbols (%%).
iOS 决定它是一个本地化字符串还是一个字符串,没有区别。
问题:当字符串存在于本地化文件中时,如何确保我所有的本地通知都使用所选的用户语言(本例中为荷兰语)而不是系统语言?
解决方法(只需在本地化字符串中添加一个空格):
localNotification.alertTitle = [NSString stringWithFormat:@"%@ ", NSLocalizedString(@"Some notifcation text", @"Notification text")];
最佳答案
谢谢,它解决了我的问题。使用 [NSString stringWithFormat:@"%@ "
确实有效!
notifyAlarm.alertBody = [NSString stringWithFormat:@"%@ ", NSLocalizedString(@"some text here", nil)];
关于ios - UILocalNotification NSLocalizedString 使用设备的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120935/