views:

350

answers:

3

Consider the following situation:

  • We have two Localizable.string files, one in en.lproj and one in it.lproj
  • When the user switches to English or Italian, the proper localized string is loaded using NSLocalizedString(@"keyword", nil)
  • If one of the files is missing the keyword, the string is not retrieved

Is there any way to make this macro load the string from a specific language if it's keyword is not found in the current locale's Localizable.string?

A: 

By supplying the second parameter of the NSLocalizedString function, you can specify a string which will be used, if the keyword wasn't found in the Localizable.string.

Is that what you wanted?

schaechtele
That second parameter is a comment and it's used as a hint for the translators
MihaiD
MihaiD you are thinking of one of the LocalizedString macros. The ACTUAL LocalizedString method's second parameter _is_ the default value to display if the keyword can't be foundhttp://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBundle/localizedStringForKey:value:table:
CraigD
I was thinking about the macro. That's what the answer mentioned.
MihaiD
+1  A: 

NSLocalizedStringWithDefaultValue function is probably what you need - using it you can specify default value for the case if localization is not found for a given key.

Vladimir
Indeed I can. The only arguments are that: 1) The default value cannot be unicode from what the docs say, and 2) I would then have all my "other" locales separated nicely in Localizable files and one "default" locale spread like crazy throughout the code. I was hoping for a more elegant solution
MihaiD
It's not ideal, but still workable. I'll consider it accepted.
MihaiD
A: 

I ran into the same problem. I though it would use the string from the development language strings file if it cannot find a localized string in the user's language, but apparently not.

I ended up creating my own function for getting a localized string.

@interface Localization : NSObject {
    NSBundle* fallbackBundle;
}

- (NSString*) localizedStringForKey:(NSString*)key;

@end


@implementation Localization

- (id)init {
    if (self = [super init]) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        fallbackBundle = [[NSBundle bundleWithPath:path] retain];
    }
    return self;
}

- (void)dealloc {
    [fallbackBundle release];
    [super dealloc];
}

- (NSString*) localizedStringForKey:(NSString*)key {
    NSString* result = [[NSBundle mainBundle] localizedStringForKey:key value:@"!#€NOTFOUND%&/" table:nil];
    if (result == nil || [result isEqualToString:@"!#€NOTFOUND%&/"]) {
        result = [fallbackBundle localizedStringForKey:key value:nil table:nil];
    }
    if (result == nil) {
        result = key;
    }
    return result;
}

@end

You can make a singleton of this and have macros similar to NSLocalizedString that call localizedStringForKey, or something along those lines if you will.

jarnoan