how to change Application language on run time using Two Button in iphone SDK?

11,568

Solution 1

Assuming that you have created localizations for English and German, your app will use the language selected in Settings (this is the preferred option).

If you want to set the language directly from your application:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];

will set the language for your application only to English (for German, I assume the key is "de"). You should also:

[[NSUserDefaults standardUserDefaults] synchronize];

This will not take effect until your application restarts.

Solution 2

Have a class "LanguageUtils" with that method :

- (NSString *) localizedString:(NSString *)key
{
    if (self.currentLanguage == nil) {
        self.currentLanguage = @"en";
    }

    NSString* path = [[NSBundle mainBundle] pathForResource:[self.currentLanguage lowercaseString] ofType:@"lproj"];
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    return [languageBundle localizedStringForKey:key value:@"" table:nil];
}

And the property NSString currentLanguage.

Then in your .pch do :

#undef NSLocalizedString
#define NSLocalizedString(key, _comment) [[Language sharedInstance] localizedString:key]

Works like a charm and you can redefine the current language at runtime.

Just keep in mind that your view should be refreshed by yourself. It won't automatically do it for you.

Solution 3

I came up with a solution that allows you to use NSLocalizedString. I create a category of NSBundle call NSBundle+RunTimeLanguage. The interface is like this.

// NSBundle+RunTimeLanguage.h
#import <Foundation/Foundation.h>
@interface NSBundle (RunTimeLanguage)
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil]
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName;
@end

The implementation is like this.

// NSBundle+RunTimeLanguage.m
#import "NSBundle+RunTimeLanguage.h"
#import "AppDelegate.h"

@implementation NSBundle (RunTimeLanguage)

- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *path= [[NSBundle mainBundle] pathForResource:[appDelegate languageCode] ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:path];
    NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil];
    return localizedString;
}
@end

Than just add import NSBundle+RunTimeLanguage.h into the files that use NSLocalizedString.

As you can see I store my languageCode in a property of AppDelegate. This could be stored anywhere you'd like.

This only thing I don't like about it is a Warning that NSLocalizedString marco redefined. Perhaps someone could help me fix this part.

Solution 4

This is very good and the language is changed within the app and also when device language changes. I used this in many apps.

For localization we generally use

 NSLocalizedString(@"hello",@"Hello World"); 

in the custom implementation they have something similar like this

 AMLocalizedString(@"hello",@"Hello World");

Solution 5

Here is demo tutorial

https://github.com/object2dot0/Advance-Localization-in-ios-apps

Just remove

 [self dealloc];

from

 -(IBAction) languageSelection:(id)sender

so it wont be crash!!! enjoy!

Share:
11,568
nivrit gupta iphone
Author by

nivrit gupta iphone

iOS Developer

Updated on June 13, 2022

Comments

  • nivrit gupta iphone
    nivrit gupta iphone almost 2 years

    i am building up a project in ios platform and want to use two language in this application and also want . the user can change the language on Runtime suppose we can take two language button button 1 for english and button 2 for german and the user can change the application language at any time by using these button . any help or tutorials

    Thanks in advance

  • V-Xtreme
    V-Xtreme about 11 years
    :This is efficient way . But you have to restart application which is not user friendly.
  • Paul Lynch
    Paul Lynch about 11 years
    Correct. But there is no way that Apple allows to change the language without restarting the app. I imagine you could do it programmatically be reimplementing localization yourself, but that's not very appealing.
  • V-Xtreme
    V-Xtreme about 11 years
    Yes Actually I was also came across the same problem but requirement is like in application the user should able to change the language at that time I didn't have any ways so I have followed the following tutorial.
  • ram880
    ram880 over 9 years
    does this work with storyboard, means if we want to change the text of uielement on storyboard (or xib) can we able to change the text to new set language
  • voghDev
    voghDev over 9 years
    Seems to work in iOS 7.1 simulator. No need to restart
  • bbjay
    bbjay almost 9 years
    You can add a #undef NSLocalizedString before to get rid of the redefinition warning
  • bbjay
    bbjay almost 9 years
    This method has the disadvantage that the genstrings command doesn't work anymore
  • Can
    Can over 7 years
    iOS 8 didn't work until restart. But this is for testing, so restarting is ok, in fact, I added an exit(1) to force-crash the app after changing the language, hacky, but works.
  • Paul Lynch
    Paul Lynch over 7 years
    I did the same (exit), but this was in an enterprise app. Might not be acceptable for the App Store.
  • Uma Madhavi
    Uma Madhavi over 6 years
    @PaulLynch. Is there any option without restarting the app . Please help me
  • Paul Lynch
    Paul Lynch over 6 years
    Only by managing your own language lookup. There are several examples given in other answers. But to switch languages automatically requires a restart.