iOS: Change Localized xib without restart

吃可爱长大的小学妹 提交于 2019-12-13 13:54:22

问题


I Know that there's some pods for changing localized strings file without restarting .. just like iOS-CustomLocalisator But my problem is my project depending heavily on xibs and it's trying to depend on server localization not device localization..

Hope there's a way for that.


回答1:


I think you first need to localize all your xibs (by clicking on "Localize..." button in the file inspector). Choose language you need and do the same for a Localizable.strings if you need it too.

Then import BundleLocalization files in your project. If you need to change language, simply use:

[[BundleLocalization sharedInstance] setLanguage:@"fr"];

It will work for xib, storyboard and NSLocalizedString function. The only "issue" is your current view controller need to be reload when you set the language. Here is an approach you can use if you have a UINavigationController (with a xib or a stroyboard, it dpesn't matter):

UINavigationController *navController = self.navigationController;
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"currentViewControllerId"];
NSMutableArray *viewControllersArray = [[NSMutableArray alloc] initWithArray:navController.viewControllers];
[viewControllersArray replaceObjectAtIndex:([navController.viewControllers count] - 1) withObject:vc];
navController.viewControllers = viewControllersArray;



回答2:


I currently do it like this on my apps.

  1. Created a class to access the desired string on the language you want.

    #import <Foundation/Foundation.h>
    
    @interface RDLocalizedString : NSObject
    + (NSString*)localizedStringForKey:(NSString*)key;
    @end
    
    #import "RDLocalizedString.h"
    
    @implementation WMLocalizedString
    + (NSString*)localizedStringForKey:(NSString*)key{
    
        //This method will return the name of the string file. my string files are all (en.strings, fr.strings, es.strings ,etc)
        NSString *tableName = [[LanguageManager sharedManager] getCurrentlanguageKey];
        NSString* str = [[NSBundle mainBundle] localizedStringForKey:key value:@"#NA#" table:tableName];
    
        //if no string is found use the english pne
        if ([str isEqualToString:@"#NA#"]) {
            str = [[NSBundle mainBundle] localizedStringForKey:key value:@"#NA#" table:@"en"];
        }
        return str;
    }
    @end
    
  2. On the View add all string on a method.

    - (void)loadStrings{
        //Load all strings here
        self.titleLabel = [RDLocalizedString localizedStringForKey:@"mainTitle"];
    }
    
  3. Create and observer that will run when the language change.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStrings) name:@"LANGUAGE_HAS_CHANGE" object:nil];
    


来源:https://stackoverflow.com/questions/31960326/ios-change-localized-xib-without-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!