3

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.

Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64
  • Check first your language is updated or not . If updated and again call API in every controller . Because server based localization without web service call you cant update it . – Alok Aug 17 '15 at 19:51
  • Can't change string of loaded nib. A solution is: change bundle language -> unload nibs will take the changed language. For the loaded nibs: re-init them. You can take a look this app https://itunes.apple.com/vn/app/intima/id998047638 – tuledev Aug 19 '15 at 06:44

2 Answers2

9

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;
Y.Bonafons
  • 2,329
  • 13
  • 20
  • But it doesn't arrange left-to-right to right-to-left languages. For example change English to Arabic. No doubt text is changed but views remain there i.e. they don't orient as they do if you change language from iOS Settings. – Chanchal Raj Dec 21 '15 at 16:15
  • just tried. works if I manually match "AppleLanguages" first object with the .proj name. Great work! – Wingzero Mar 14 '16 at 10:30
  • This is the best answer about change language in app WITHOUT restart app. – JordanChina May 10 '16 at 03:43
1

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];
    
rob180
  • 901
  • 1
  • 9
  • 29
  • still that solution is on the controller side .. it's huge project i can't just localize label by label in code while they are already localized in .xib strings file – Mohamed Emad Hegab Aug 18 '15 at 07:06