• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [Proposal] On-the-fly NIB localization
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Proposal] On-the-fly NIB localization


  • Subject: Re: [Proposal] On-the-fly NIB localization
  • From: Martin Wennerberg <email@hidden>
  • Date: Tue, 16 Nov 2004 09:03:56 +0100

I did the localization this way in my small freeware MorphX. Basically, I recursively translate all the strings in the menu and the windows after loading them. When designing the nib-files I make the text fields a little extra long so that all translations fit.

I still keep a separate .lproj for each language, but these only contain the Localizable.strings file and other text files (eg. online help). They might contain localized nibs as well if a language demands it, but so far I have not had to do this.

Advantages:

1) Supporting a lot of languages is a lot of work, over time but this method makes it simpler.
2) The people that do the translation can simply use TextEdit and don't need the developer tools. Anybody can easily add a new translation.
3) I can reuse the localizations between applications.
4) There's always the fallback that you can have regular localized nib files when some language demands a different design or when simple dictionary translation does not work.


Editing the .strings files for all the languages and making sure they have the same entries is still a lot of work when doing new versions of the application. It would be nice if Xcode had some nice GUI for this...



Here's the simple code for recursive translation:

.h:
extern void localizeWindow(NSWindow *window);
extern void localizeView(NSView *view);
extern void localizeMenu(NSMenu *menu);

.m:
static NSString *localizeString (NSString *string)
{
if (string == nil || [string length] == 0)
return string;
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[string characterAtIndex:0]])
return string;
return NSLocalizedString (string, @"");
}


static void localizeCell(NSCell *view)
{
if ([view respondsToSelector:@selector(title)] && [view respondsToSelector:@selector(setTitle:)])
[(NSButtonCell *)view setTitle:NSLocalizedString([view title], @"Cell title")];
if ([view respondsToSelector:@selector(stringValue)] && [view respondsToSelector:@selector(setStringValue:)])
[view setStringValue:NSLocalizedString([view stringValue], @"View stringValue")];
}


static void localizeMatrix(NSMatrix *view)
{
    NSEnumerator	*cellEnum = [[view cells] objectEnumerator];
    NSCell			*cell;

    while ((cell = [cellEnum nextObject]))
        localizeCell(cell);
}

void localizeWindow(NSWindow *window)
{
[window setTitle:NSLocalizedString([window title], @"Window title")];
[window setMiniwindowTitle:NSLocalizedString([window miniwindowTitle], @"Window title")];
localizeView([window contentView]);
}


void localizeView(NSView *view)
{
    NSEnumerator *subviewEnum = [[view subviews] objectEnumerator];
    NSView	*subview;

    while ((subview = [subviewEnum nextObject]))
        localizeView(subview);

    if ([view toolTip])
        [view setToolTip:localizeString([view toolTip])];

if ([view isKindOfClass:[NSMatrix class]])
localizeMatrix((NSMatrix *) view);
else if ([view isKindOfClass:[NSPopUpButton class]])
localizeMenu((NSMenu *) view);
else if (![view isKindOfClass:[NSImageCell class]] && ![view isKindOfClass:[NSImageView class]])
{
if ([view respondsToSelector:@selector(title)] && [view respondsToSelector:@selector(setTitle:)])
[(NSBox *)view setTitle:localizeString([(NSBox *)view title])];
if ([view respondsToSelector:@selector(stringValue)] && [view respondsToSelector:@selector(setStringValue:)])
[(NSTextField *)view setStringValue:localizeString([(NSTextField *)view stringValue])];
}
}


void localizeMenu(NSMenu *menu)
{
    NSEnumerator	*itemEnum = [[menu itemArray] objectEnumerator];
    NSMenuItem		*item;

    [menu setTitle:localizeString([menu title])];
    while ((item = [itemEnum nextObject]))
    {
        [item setTitle:localizeString([item title])];
        localizeMenu([item submenu]);
    }
}


Cheers, Martin Wennerberg

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: [Proposal] On-the-fly NIB localization
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: Re: Connecting a progress bar to standard C code?
  • Next by Date: Re: Mixing image filters on an image
  • Previous by thread: Re: [Proposal] On-the-fly NIB localization
  • Next by thread: Re: [Proposal] On-the-fly NIB localization
  • Index(es):
    • Date
    • Thread