Obtaining the localized application name in Cocoa

Q: How can I get the localized name of my Cocoa application?

A: There are several different versions of the application name, all of which are readily available as an NSString. Depending on what you need, you can use one of the following four approaches:

The application's canonical "short" name is defined by CFBundleName and is used by the menu and about box.

Listing 1: Getting the application short name from the main bundle.

    NSString *appName =
        [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:(NSString *)kCFBundleNameKey];

The application's canonical "display name" is typically longer and is used by the Finder to display the bundle.

Listing 2: Obtaining the application's display name.

     NSString *appName =
        [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"];

Note that it is possible for both of these preceding methods to return nil. If they do, a viable approach is to do what the default about box does and simply use the process name itself.

Listing 3: Acquiring the process name.

     NSString *appName = = [[NSProcessInfo processInfo] processName];

Probably the most general approach, however, is to ask the NSFileManager. The method displayNameAtPath: will return the localized display name if it exists, fall back on the bundle name if it does not, and will even reflect a renaming of the application by the user.

Listing 4: Obtaining a general display name from NSFileManager

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *appName = [[NSFileManager defaultManager] displayNameAtPath: bundlePath];

For more information, see File System Overview: Display Names and the section "Localizing Your Application Name" in Internationalization Programming Topics: Localizing Pathnames.

Document Revision History

DateNotes
2007-09-21First Version

Posted: 2007-09-21


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.