Apple 是否为 NSError 的常见 NSButton 标题提供本地化恢复选项?

发布于 2024-09-07 20:00:41 字数 1352 浏览 9 评论 0原文

Apple 是否为常见 NSButton 标题提供本地化字符串,例如相当于 OKCancelTry AgainQuit嗯>?

我正在尝试从另一个 Cocoa 框架交给我的一个自定义 NSError 对象中创建一个。我想实现 NSErrorRecoveryAttempting 非正式协议,让用户在发生错误时重试退出。举个(简短的)例子:

NSString *cachesDir = ...;
NSError  *error     = nil;
BOOL     success    = [[NSFileManager defaultManager] createDirectoryAtPath:cachesDir withIntermediateDirectories:NO attributes:nil error:&error];

// Failed creating the directory.
if (!success)
{
 NSMutableDictionary *errorUserInfoCopy = [[[error userInfo] mutableCopy] autorelease];

 NSArray *recoveryOptions = [NSArray arrayWithObjects:@"Try Again", @"Quit", nil];
 [errorUserInfoCopy setObject:recoveryOptions forKey:NSLocalizedRecoveryOptionsErrorKey];

 NSError *newError = [NSError errorWithDomain:[error domain] code:[error code] userInfo:errorUserInfoCopy];
 [NSApp presentError:newError];
}

在第 10 行,NSArray *recoveryOptions = ...,Apple 是否提供了一种轻松获取 Try Again/Quit 本地化版本(以及其他常见 NSButton 标题)的方法?或者我必须手动填充 .strings 文件并使用 NSLocalizedString() 代替?

Apple 确实为非自定义 NSError 提供了本地化恢复选项:

NSArray *recoveryOptions = [anError localizedRecoverySuggestion];

但这通常只是默认为本地化的“OK”。它不适合自定义 NSErrorRecoveryAttempting

Does Apple provide localized strings for common NSButton titles, such as the equivalents to OK, Cancel, Try Again, and Quit?

I'm attempting to create a custom NSError object from one handed to me by another Cocoa framework. I want to implement the NSErrorRecoveryAttempting informal protocol to let the user Try Again or Quit if an error occurs. For (brief) example:

NSString *cachesDir = ...;
NSError  *error     = nil;
BOOL     success    = [[NSFileManager defaultManager] createDirectoryAtPath:cachesDir withIntermediateDirectories:NO attributes:nil error:&error];

// Failed creating the directory.
if (!success)
{
 NSMutableDictionary *errorUserInfoCopy = [[[error userInfo] mutableCopy] autorelease];

 NSArray *recoveryOptions = [NSArray arrayWithObjects:@"Try Again", @"Quit", nil];
 [errorUserInfoCopy setObject:recoveryOptions forKey:NSLocalizedRecoveryOptionsErrorKey];

 NSError *newError = [NSError errorWithDomain:[error domain] code:[error code] userInfo:errorUserInfoCopy];
 [NSApp presentError:newError];
}

In line 10, NSArray *recoveryOptions = ..., does Apple provide a way to easily get localized versions of Try Again/Quit (among other common NSButton titles)? Or must I manually populate .strings files and use NSLocalizedString() instead?

Apple does provide localized recovery options for non-customized NSError's:

NSArray *recoveryOptions = [anError localizedRecoverySuggestion];

But this usually just defaults to a localized "OK". It's not suitable for custom NSErrorRecoveryAttempting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春庭雪 2024-09-14 20:00:41

苹果似乎没有。

It appears that Apple does not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文