UILocalNotification NSLocalizedString uses language of device

こ雲淡風輕ζ 提交于 2019-12-10 17:57:26

问题


We have a localized app. Lot of users in the Netherlands have their device set to English and as second language to Dutch. We have a language selection menu in our app because 99.9% of the users wants dutch traffic information and not English. Therefor we set the language to Dutch if one of the preferred language is Dutch.

This works great except for UILocalNotifications and the device language is English (second one is Dutch). Our app language is Dutch (but should be the same for any other language which is different then the system language).

This how we set the language to a specific choosen language, in this example Dutch (by using the answer from this thread 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

This is how we send a 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];

The NSString var *message is a LocalizedString and when debugging this string is in Dutch:

 (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

Now the iOS receives this localNotification and tries to translates this to English. This translation works because the string is in the localization file.

If the message is not in the translation file (because it has a number in it) or if I add a space to the end of the message it wont find the string in the localization file and shows a dutch notification.

It seems weird that iOS tries to translate the LocalNotification to the system language (English) and not the app language (Dutch).

Apple's documentation says this:

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 decides if its a localized string or just a string, there is no difference.

Question: how can I make sure all my local notifications are in the chosen user language (Dutch in this example) instead of the systemlanguage when the string exist in the localisation file ?

Workaround (just add a space to the localized string):

localNotification.alertTitle = [NSString stringWithFormat:@"%@ ", NSLocalizedString(@"Some notifcation text", @"Notification text")];

回答1:


Thank you, it fixed my issue. Using [NSString stringWithFormat:@"%@ " really work!

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


来源:https://stackoverflow.com/questions/33120935/uilocalnotification-nslocalizedstring-uses-language-of-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!