ios - UILocalNotification NSLocalizedString 使用设备的语言

标签 ios localization uilocalnotification

我们有一个本地化的应用程序。荷兰的许多用户将他们的设备设置为英语,并将其作为荷兰语的第二语言。我们的应用程序中有一个语言选择菜单,因为 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 (%%).

https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertBody

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/

相关文章:

iphone - 如何在 iPhone 应用程序中显示阿拉伯数字而不本地化每个数字

xcode - 使用 Xcode 10 进行基础国际化

ios - 单击单元格时 UIView 位于 UITableView 之上?

ios - ios 7.0.X 的 popover 大小问题,加载 popover 时没有得到

graph - Open Graph 对象国际化 : no scrape/missing parameter fb_locale

ios - UILocalNotification 在后台时未调用

objective-c - 关于 iOS 本地通知的问题

iOS 9.2.1,取消通知不起作用

iphone - 我需要在 dealloc 方法中将 release 发送到我的实例变量吗? (iOS)

ios - UISearchController 搜索栏宽度不变