Nilay Shah Nilay Shah -4 years ago 147
iOS Question

How to change Language runtime in iphone app?

I am using below code.

This code saves Locale name, but how refresh whole application with new language.

Here langcode variable is dynamic as per user selection.

NSString *langCode = @"fr";
NSArray *languages = nil;
languages = [NSArray arrayWithObject:langCode];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

Which is remains for change the language in runtime? (From my app setting screen)

Answer Source

After making category on Language it will solve the issue.

.h file

@interface NSBundle (Language)

+ (void)setLanguage:(NSString *)language;


.m file

#import <objc/runtime.h>

static const char _bundle=0;

@interface BundleEx : NSBundle

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
    NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);
    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];

@implementation NSBundle (Language)

+ (void)setLanguage:(NSString *)language
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
                      object_setClass([NSBundle mainBundle],[BundleEx class]);
    objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:self];

From my setting view controller called above method of the category class and also fire post notification for whole application.

[NSBundle setLanguage:langCode];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download