UILocalNotification NSLocalizedString uses device language

We have a localized app. Many users in the Netherlands have their device set to English and the second set to Dutch. We have a language selection menu in our application because 99.9% of users want information about it, not about it. Therefore, we set the language to Dutch if one of the preferred languages ​​is Dutch.

This works great except for UILocalNotifications and the device language is English (the second is Dutch). Our application language is Dutch (but must be the same for any other language other than the system language).

This is how we set the language to a specific language of choice, in this example Dutch (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 the 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 message NSString var * is localizedString and when debugging this string 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 iOS receives this localNotification and tries to translate it into English. This translation works because the string is in the localization file.

If the message is not in the translation file (because there is a number in it), or if I add a space at the end of the message, it will not find the line in the localization file and show a Dutch notice.

It seems strange that iOS is trying to translate LocalNotification into the system language (English) and not the application language (Dutch).

Apple's documentation says this:

property alertBody The message to display in the alert.

Assign a string, or preferably a key with a localized string (using NSLocalizedString) as the message value. If the value of this property is not zero, a warning is displayed. The default is nil (no warning). The escape-style characters in printf format are stripped from the string before being displayed; to include the percent character (%) in the message, use two percent characters (%%).

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, no difference.

Question: how can I ensure that all my local notifications are in the user's selected language (Dutch in this example) instead of the system language when the string exists in the localization file?

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

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

      

+2


source to share


1 answer


Thanks, he fixed my problem. The use [NSString stringWithFormat:@"%@ "

really works!



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


source







All Articles