Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization fallback language

I want to localize a couple of strings in my iPhone app and it works so far for the 5 languages I've selected. However, I have one issue, if I haven't defined a key for a specific language I want it to choose english as a fallback because I know that I have defined every string for sure in english. The thing is that it's not very important that all keys are translated why it's okay to show english even if the iPhone is set to spanish, for instance.

Example:

en:

"HELLO" = "hello" ;
"PERSON" = "my friend" ;

es:

"HELLO" = "hola" ;

Expected Outcome:

If the iPhone is set to english I want it to say: hello, my friend and if the languange is set to spanish I want it to say hola, my friend. Currently the app will output hola, PERSON. (I use the NSLocalizedString method).

There is probably a really easy solution but I couldn't find any good ones. Thanks for the help,

Mattias

like image 448
Mattias Farnemyhr Avatar asked Dec 15 '22 12:12

Mattias Farnemyhr


1 Answers

The other answers were definitely correct but it wasn't exactly what I wanted. Instead I found another way to do it and wrote a static method that works perfectly for me in my environment and I wanted to add it here if anybody else wants to acheive the same thing:

+ (NSString *)localizedStringForKey:(NSString *)key table:(NSString *)table
{
    NSString *str = [[NSBundle mainBundle] localizedStringForKey:key value:@"NA" table:table];
    if ([str isEqualToString:@"NA"])
    {
        NSString *enPath = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        return [[NSBundle bundleWithPath:enPath] localizedStringForKey:key value:@"NA" table:table];
    }

    return str;
}
like image 82
Mattias Farnemyhr Avatar answered Dec 18 '22 03:12

Mattias Farnemyhr