Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLocalizedString not defaulting to Base language

I have the following problem with a small iOS 7 project on which I'm testing the localisation capabilities.

  • I have a default project, with one VC, in which I have one button in the middle of the scene
  • in my VC I have an IBOutlet to my button called myButton
  • in the viewDidLoad method of the VC I am setting the buttons's title:


    NSString *title = NSLocalizedString(@"MY_BUTTON", @"My comment for my button");
    [self.myButton setTitle:title forState:UIControlStateNormal];

  • I generated the Localizable.strings file end enabled it for localization for the following languages: Base, Dutch
  • the contents of each file are as follows:

/* My comment for my button */ "MY_BUTTON" = "My [VALUE] Button"; where VALUE = Base, Dutch; so the labels should be My Base Button & My Dutch Button

Problem: If I launch my app using the simulator's language as Dutch, the label is (as expected) "My Dutch Button". If I launch it in English, the label is "My Base Button" (kind of ok…)

However, if I launch it with the phone's language set to French, and I previously had it set to Dutch, the label of the button does not default to Base, and instead displays again "My Dutch Button"

Any thoughts on this?

Thanks

like image 616
tufyx Avatar asked Feb 13 '14 12:02

tufyx


2 Answers

I have created the following class, which supports a fallback to a customizable language. In my case I use Base.lproj as file for my default language contents.

StringUtilities.h

@interface StringUtils : NSObject

#define GetLocalizedString(key) [StringUtils getLocalizedString:key comment:nil]
//#define GetLocalizedString(key,comment) [StringUtils getLocalizedString:key comment:comment]


+ (NSString*) getLocalizedString:(NSString*)key comment:(NSString*)comment;

@end

StringUtilities.m

#import "StringUtilities.h"

@implementation StringUtils


//Returns a localized string, with fallback to version of Base
+ (NSString*) getLocalizedString:(NSString*)key comment:(NSString*)comment {
    NSString* localizedString = NSLocalizedString(key, nil);

    //use base language if current language setting on device does not find a proper value
    if([localizedString isEqualToString:key]) {

        NSString * path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];
        NSBundle * bundle = nil;
        if(path == nil){
            bundle = [NSBundle mainBundle];
        }else{
            bundle = [NSBundle bundleWithPath:path];
        }
        localizedString = [bundle localizedStringForKey:key value:comment table:nil];
    }
    return localizedString;
}

@end

How to use

Import the header file and use the GetLocalizedString macro instead of NSLocalizedString macro.

#import "StringUtilities.h"

NSString* str = GetLocalizedString(@"your.text.key");
like image 154
Dirk Avatar answered Oct 08 '22 01:10

Dirk


the order of default languages is a user setting on OSX and not editable (AFAIK) on iOS
BUT still adhered to!

the app is passed the array AppleLanguages (or so..) that specifies the languages to try. The NSLocalizedString macro will try load each language in the array in the order they appear UNTIL it finds a working one and then it uses that

compare: How to force NSLocalizedString to use a specific language

like image 5
Daij-Djan Avatar answered Oct 08 '22 00:10

Daij-Djan