PDA

View Full Version : Localize NSMenu Title




Darkroom
Nov 2, 2008, 08:15 PM
is this not possible? i have all my NSMenuItems hooked up and localized fine, but the NSMenu title is not being localized


#import <Cocoa/Cocoa.h>

@interface AppController : NSObject
{
//Main Menu Outlets
IBOutlet id MenuItem1;
IBOutlet id MenuItem2;
}

@end





#import "AppController.h"

static NSString * CONSTMenuItem1;
static NSString * CONSTMenuItem2;

@implementation AppController

- (void)awakeFromNib
{
CONSTMenuItem1 = NSLocalizedString (@"ENGLISH MENU", nil);
[MenuItem1 setTitle:CONSTMenuItem1];

CONSTMenuItem2 = NSLocalizedString (@"English Item...", nil);
[MenuItem2 setTitle:CONSTMenuItem2];
}

@end


i can't seem to hook up the NSMenu in IB, only it's NSMenuItem, so maybe this is why it's not working? i've attached the proj.

any help would be great.



kanenas
Nov 10, 2008, 03:14 PM
If you don't need to localize programmatically, here's the procedure:

in XCode, add a localisation to the NIB/XIB:
select the NIB/XIB in Resources
get info (cmd-I or File->Get Info)
"General" tab
click "Add Localization" button at bottom of window
Name the locale. Apple recommends using short names (Alpha-2 (http://www.iso.org/iso/english_country_names_and_code_elements) of ISO 3166).

Expand the NIB/XIB in XCode
Open the localized interface in IB by double-clicking on it
Edit away

This is better than the programmatic approach as you can change not only text but layout, which is important when the label text has different widths in different locales. It's also easier and more efficient (you don't need to do all that stuff in awakeFromNib).

As for why your approach didn't work, I think you're spot on that it's because you don't have a pointer to the NSMenu. Any reason you didn't add a menu outlet to your controller and target the NSMenu in IB? Of course it's now unnecessary, given the above localization procedure.

Darkroom
Nov 10, 2008, 06:27 PM
hi... your suggested procedure is exactly what i've been trying to avoid :p

i'm aware that is how to localize normally, but for me i just find it much easier to have a localization class with all the NSString constants so i can keep track easier...

i received the answer from another board a while back:


[[[[NSApp mainMenu] itemAtIndex:1] submenu] setTitle:NSLocalizedString(@"File", nil)];


the above works perfectly...