Frage

I have an OSX Automator workflow that contains a custom Obj-C action. It works, but I am not able to localize it. My test action is:

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo
{
    NSArray *contactStrings = (NSArray *)input;
    NSString *name    = contactStrings[0];
    NSString *address = contactStrings[1];
    NSString *comment = NSLocalizedString(@"IMPORTED", nil);
    NSString *output  = [NSString stringWithFormat:@"Name: %@, Address: %@, Comment: %@", name, address, comment];
    return output;
}

I have a localized Localizable.strings file with a base and a German localization.
Base (without comments):

"IMPORTED" = "Imported from OSX service";  

German (without comments):

"IMPORTED" = "Über OSX-Dienst eingelesen";  

The problem is that when the Automator script is executed, the comment string is output as IMPORTED, e.g. the localization does not work at all.
What might be the reason?

War es hilfreich?

Lösung

I solved the problem:
NSLocalizedString(...) is a macro defined in NSBundle.h as

#define NSLocalizedString(key, comment) \
        [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]  

This means that by using this macro, the Localizable.strings files are searched in the main bundle of the Automator application, not the bundle of the Obj-c action!!! This is also documented here.
The bundle of the Obj-c action is obtained by [self bundle].

I thus replaced the wrong line

NSString *comment = NSLocalizedString(@"IMPORTED", nil);

by

NSString *comment = [[self bundle] localizedStringForKey:@"IMPORTED" value:@"" table:nil];  

and everything works.
Hope this helps somebody else also!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top