Manually loading a different localized nib in iOs

前端 未结 1 1814
遇见更好的自我
遇见更好的自我 2020-12-01 19:44

I\'m working on an app with multi-language support. As you may expect, from time to time I load some nib files using a code like this:

 self.currentControll         


        
相关标签:
1条回答
  • 2020-12-01 20:20

    So, just like I said in the edit, this is what I found as a solution:

    NSString* path= [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"]; 
    
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    
    self.currentController = [[newClass alloc] initWithNibName:@"CustomController" bundle:languageBundle];
    

    And if you need to load a text into a localized label

    NSString* path= [[NSBundle mainBundle] pathForResource:[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0] ofType:@"lproj"];
    
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    
    someLabel.text = [languageBundle localizedStringForKey:@"textKey" value:@"" table:nil];       
    

    More info here: http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html

    To whom it may concern, this method raises quite a number of problems. For starters, something that might affect everyone: you need to have every resource used by a localized xib also localized. If I load a new localized xib using this method, and that xib contains a regular non-localized image, it won't show up until it's localized. The other problems are more particular and are connected to the way you retrieve the localized data.

    In the end, I don't think I will be using this, because for the current app it's way too problematic, but it might turn out handy in the future.

    0 讨论(0)
提交回复
热议问题