loading images from the correct bundle when localizing storyboards

前端 未结 3 729

I\'m trying to add an option for the user to switch between Arabic & English language from inside the app (without having to re-set the language of the whole iPhone), I

相关标签:
3条回答
  • 2020-12-31 19:53
    NSBundle *bundle = [LocalizationSystem sharedBundle];  //this get your current bundle
    
    NSString *imgPath = [bundle pathForResource:@"btn-Image" ofType:@"png" inDirectory:nil];
    [self.btnTest setImage:[UIImage imageWithContentsOfFile:imgPath] forState:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-12-31 19:54

    I had same issue :(.. and what worked for me (for such scenarios to switch language) ..

    You have 2 possibilities..

    1) If you want to change the storyboard (hot change), you must modify all your Outlets in the each Storyboard. Choose in your storyboard the desired image (with different names), labels, etc.. For me, with localized images (same name) - only worked after restart of the application.. For the same img_name, I' tried ... to "call" the specific image.. from the specific lproj folder (languageBundle).. but it did not work at changed the storyboard from AppDelegate..

    2) You can have only one storyboard... and change only labels, image, etc... (according to Localizable.strings), something linke this... https://stackoverflow.com/a/12149553/1702413 Also, you must have different file name for images (for "hot changing")

    I don't know if a bug... or what is...

    UPD

    You have a little project https://dl.dropbox.com/u/19438780/test5%20copy2.zip with both..

    If you are changing the storyboard (like you) you will have the same path for languageBundle until you restart the application. So.. you must do the changing right in your storyboard..

    For the second.. you can change/recall outlets .. from specific languageBundle (lproj folders)..

    For 1) .. I found a "trick" to overwrite the languageBundle until restart:

    -(id) initWithCoder:(NSCoder *)aDecoder{
        self = [super initWithCoder:aDecoder];
        if (self) {
            NSArray* languages = [[NSUserDefaults standardUserDefaults]      objectForKey:@"AppleLanguages"];
            Lang2 = [languages objectAtIndex:0]; //NSString
        }
        return self;
    }
    

    and init the Outlets:

    - (void) viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:YES];
    
          NSString *path;
       if([Lang2 isEqualToString: @"ro"])
        {
            path = [[NSBundle mainBundle] pathForResource:@"ro" ofType:@"lproj"];
            NSLog(@"enc ro");
       }
        else{
            path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
            NSLog(@"enc en");
        }
    
        NSBundle* languageBundle = [NSBundle bundleWithPath:path];
        //find and return the desired string
        self.label.text=[languageBundle localizedStringForKey:@"kLabelText" value:@"" table:nil];
        self.img.image = [UIImage imageNamed:[languageBundle localizedStringForKey:@"myImage" value:@"" table:nil]];
    }
    
    0 讨论(0)
  • 2020-12-31 19:56

    You have to reload all the views that contains multilingual text which can't be done as if the view has referenced objects in the controller then the result is unknown and most probably it will crash.

    A correct and efficient approach will be to have a dictionary for each language that you can switch based on language selected. Draw all the static text this way you can easily draw it back when the language changes. On language change post a notification to controllers to redraw the view. For images you can do similar draw them or have reference to them that you can change.

    One there thing you can do for images is to have a custom image view class that also observe the language change notification and automatically loads the appropriate image. Same you can do for labels.

    I can code something for you if you need an example.

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