2015-10-14 35 views
2

我们有一个本地化的应用程序。荷兰的许多用户将他们的设备设置为英语,并将其设为荷兰语的第二语言。我们的应用中有一个语言选择菜单,因为99.9%的用户需要荷兰交通信息而不是英语。因此,如果首选语言之一是荷兰语,我们将该语言设置为荷兰语。UILocalNotification NSLocalizedString使用设备的语言

这个工程很好,除了UILocalNotifications和设备语言是英语(第二个是荷兰语)。我们的应用程序语言是荷兰语(但对于与系统语言不同的任何其他语言应该是相同的)。

这是我们如何将语言设置为特定choosen语言,在这个例子中的荷兰(通过使用回答这个线程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 *消息是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翻译成系统语言(英语)而不是应用程序语言(荷兰语),这似乎很奇怪。

苹果的文件说的:

alertBody物业通知警报显示该消息。使用 NSLocalizedString作为消息的值。如果此 属性的值非零,则会显示警报。默认值为零 (无警报)。显示之前,将从 字符串中去除Printf样式转义字符;要在 消息中包含百分号(%),请使用两个百分号(%%)。

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")]; 

回答

0

谢谢你,它固定我的问题。使用[NSString stringWithFormat:@"%@ "真的有用!

 
notifyAlarm.alertBody = [NSString stringWithFormat:@"%@ ", NSLocalizedString(@"some text here", nil)];
相关问题