Nischal G Prasad
Nischal G Prasad

Reputation: 11

Dynamically localize MainMenu.xib

I need a way to change the strings in the menu bar (MainMenu.xib) in Cocoa application to render dynamically based on the locale I set. I have created MainMenu.strings file for different locales. Currently, all strings that are displayed are for the system locale. Is there a way where I can override this logic to display the strings for the locale I want. Any help is appreciated.

Upvotes: 0

Views: 270

Answers (1)

Mark Bessey
Mark Bessey

Reputation: 19782

Overriding the system locale is not something that OS X applications normally do. Unless you've got a really good reason to do so, it'd be better to just use the user's preferred locale, as set in System Preferences.

If you really want to do this, you can set the "AppleLanguages" user default to whatever locale you want before calling NSApplicationMain, and the application will use that locale.

Like this:

// Note: Don't do this.
int main(int argc, const char * argv[]) {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@[@"de"] forKey:@"AppleLanguages"];
    return NSApplicationMain(argc, argv);
}

Upvotes: 1

Related Questions