UILocalNotification NSLocalizedString使用设备的语言 [英] UILocalNotification NSLocalizedString uses language of device

查看:49
本文介绍了UILocalNotification NSLocalizedString使用设备的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

除了UILocalNotifications和设备语言为英语(第二种为荷兰语)外,此方法都很好用.我们的应用程序语言是荷兰语(但与系统语言不同的任何其他语言都应相同).

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

这是我们发送UILocalNotification的方式:

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

NSString var * message是LocalizedString,在调试时此字符串是荷兰语:

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

现在,iOS收到此localNotification并尝试将其翻译为英语.该翻译有效,因为该字符串在本地化文件中.

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.

iOS尝试将LocalNotification转换为系统语言(英语)而不是应用程序语言(荷兰语)似乎很奇怪.

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

Apple的文档说:

Apple's documentation says this:

alertBody属性通知警报中显示的消息.

alertBody Property The message displayed in the notification alert.

分配一个字符串,或者最好分配一个本地化的字符串键(使用 NSLocalizedString)作为消息的值.如果这个值 属性为非零,则显示警报.预设值为nil (无警报). Printf样式的转义字符从 显示之前的字符串;在其中包含百分比符号(%) 消息,请使用两个百分号(%%).

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决定是本地化字符串还是只是字符串,没有区别.

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

推荐答案

谢谢,它解决了我的问题.使用[NSString stringWithFormat:@"%@ "确实有效!

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


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

这篇关于UILocalNotification NSLocalizedString使用设备的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆