Shah Nilay
Shah Nilay

Reputation: 798

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)

Upvotes: 0

Views: 1228

Answers (3)

Shah Nilay
Shah Nilay

Reputation: 798

After making category on Language it will solve the issue.

.h file

@interface NSBundle (Language)

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

@end

.m file

#import <objc/runtime.h>

static const char _bundle=0;

@interface BundleEx : NSBundle
@end

@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];
}

@end
@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];

Upvotes: 1

NewStackUser
NewStackUser

Reputation: 657

Store this variable in ApplicationDelegate file. Define its property and use anytime via appDelegate instance when you login. Once selection is changed, change the application bundle as you desire also modify the text and all. put the language selection code whether in loop or button selection as per your requirement.

       NSPath* path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
    APP_DELEGATE.strLanguageSelectedFromLoginView = @"SPANISH";

or

       NSPath* path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    APP_DELEGATE.strLanguageSelectedFromLoginView = @"ENGLISH";

and

        languageBundle = [NSBundle bundleWithPath:path];

and finally load the laguageBundle. NSBundle* languageBundle to be defined in app delegate.

Upvotes: 0

Dharma
Dharma

Reputation: 3013

With adding the LocalizationSystem file in your code, you'll be able to change the language in run-time.

download link http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.h http://dl.dropbox.com/u/2917666/LocalizationSystem/LocalizationSystem.m

for more information http://aggressive-mediocrity.blogspot.com/2010/03/custom-localization-system-for-your.html

Upvotes: 0

Related Questions