有什么方法可以在macOS上获取系统定义的目录的本地化显示名称?



我想获取系统定义的目录'(例如桌面,文档,下载)本地化显示名称与Finder的最爱中所示。

我尝试使用NSFileManagerdisplayNameAtPath:但是它仅适用于自定义目录。包含*.字符串文件的局部目录,而对系统定义的目录不起作用。

下面是我的代码,我对此进行了检查。

#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if (argc < 2) {
        // Use ~/Application directory if no command line arguments are provided
        NSString *localizedApplicationDirectoryName = [[NSFileManager defaultManager] displayNameAtPath:NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSLocalDomainMask, YES).lastObject];
        NSLog(@"%@", localizedApplicationDirectoryName);
        [pool drain];
        return 1;
    }
    // Use command line arguments
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSString *directory = [NSString stringWithUTF8String:argv[1]];
    NSString *displayName = [fileManager displayNameAtPath:directory];    
    NSLog(@"%@", displayName);
    [pool drain];
    return 0;
}

我在做错什么?还有其他方法可以获取这种翻译吗?

从URL获取本地名称,例如桌面文件夹

NSURL *url = [[NSURL fileURLWithPath:NSHomeDirectory()] URLByAppendingPathComponent: @"Desktop"];
NSString *localizedName;
NSError *error;
[url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:&error];
if (error) {
    NSLog(@"%@", error);
} else {
    NSLog(@"%@", localizedName);
}

最新更新