Reputation: 2675
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.
Upvotes: 3
Views: 1852
Reputation: 2349
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;
Upvotes: 9
Reputation: 901
I currently do it like this on my apps.
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
On the View add all string on a method.
- (void)loadStrings{
//Load all strings here
self.titleLabel = [RDLocalizedString localizedStringForKey:@"mainTitle"];
}
Create and observer that will run when the language change.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStrings) name:@"LANGUAGE_HAS_CHANGE" object:nil];
Upvotes: 1