0

I am creating one sample demo for changing label Language when button pressed using localization.

I have tried so many times but could not able to display different language.

My Question is I want change my Language English to other language.

I have already create Localizable.string file but not able to change language.

NOTE:Do not change system(simulator)language.

user7018875
  • 160
  • 9
  • You mean you want to change language on action within your app, not on change of language configuration of the device? – NSNoob Dec 06 '16 at 10:30
  • Ya exactly,I want on button action – user7018875 Dec 06 '16 at 10:31
  • Possible duplicate of [iOS: How to change app language programmatically WITHOUT restarting the app?](http://stackoverflow.com/questions/9416923/ios-how-to-change-app-language-programmatically-without-restarting-the-app) – NSNoob Dec 06 '16 at 10:31
  • Also see: http://stackoverflow.com/questions/16032207/how-to-change-application-language-on-run-time-using-two-button-in-iphone-sdk – NSNoob Dec 06 '16 at 10:32
  • Also: http://stackoverflow.com/questions/9939885/manual-language-selection-in-an-ios-app-iphone-and-ipad – NSNoob Dec 06 '16 at 10:32
  • Not working can any one give me sample demo please.Do not suggest me github link.. – user7018875 Dec 06 '16 at 11:04

1 Answers1

0

You can create a Helper class. See the class I'm using below.

LocalizeHelper.h

#import <Foundation/Foundation.h>

// some macros (optional, but makes life easy)

// Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)"
#define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)]

// "language" can be (for american english): "en", "en-US", "english". Analogous for other languages.
#define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)]

#define LocalizationGetLanguage() [[LocalizeHelper sharedLocalSystem] getLanguage]

@interface LocalizeHelper : NSObject

// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;

// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;

//set a new language:
- (void) setLanguage:(NSString*) lang;

//get current language
- (NSString *)getLanguage;

@end

LocalizeHelper.m

#import "LocalizeHelper.h"
#import "Constants.h"

// Singleton
static LocalizeHelper* SingleLocalSystem = nil;

// my Bundle (not the main bundle!)
static NSBundle* myBundle = nil;


@implementation LocalizeHelper


//-------------------------------------------------------------
// allways return the same singleton
//-------------------------------------------------------------
+ (LocalizeHelper*) sharedLocalSystem {
    // lazy instantiation
    if (SingleLocalSystem == nil) {
        SingleLocalSystem = [[LocalizeHelper alloc] init];
    }
    return SingleLocalSystem;
}


//-------------------------------------------------------------
// initiating
//-------------------------------------------------------------
- (id) init {
    self = [super init];
    if (self) {
        // use systems main bundle as default bundle
        myBundle = [NSBundle mainBundle];
    }
    return self;
}


//-------------------------------------------------------------
// translate a string
//-------------------------------------------------------------
// you can use this macro:
// LocalizedString(@"Text");
- (NSString*) localizedStringForKey:(NSString*) key {
    // this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment")
    // the difference is: here we do not use the systems main bundle, but a bundle
    // we selected manually before (see "setLanguage")
    return [myBundle localizedStringForKey:key value:@"" table:nil];
}


//-------------------------------------------------------------
// set a new language
//-------------------------------------------------------------
// you can use this macro:
// LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de");
- (void) setLanguage:(NSString*) lang {

    // path to this languages bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ];
    if (path == nil) {
        // there is no bundle for that language
        // use main bundle instead
        myBundle = [NSBundle mainBundle];
    } else {

        // use this bundle as my bundle from now on:
        myBundle = [NSBundle bundleWithPath:path];

        // to be absolutely shure (this is probably unnecessary):
        if (myBundle == nil) {
            myBundle = [NSBundle mainBundle];
        }
    }

    [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"lang"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//-------------------------------------------------------------
// get current language
//-------------------------------------------------------------
- (NSString *)getLanguage {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"lang"];
}


@end

When you want to change the language from your app call this function LocalizationSetLanguage(@"fr");.

In order to get a localized string, call LocalizedString(@"string");.

Joseph El Khoury
  • 406
  • 1
  • 5
  • 13