I did localisation for my iOS app.By the client order i need to make a modification i.e the app languages should change by button action. My previous localisation is works based on device language.Now i have to modify the application to change the language with out rebooting the device.
            Asked
            
        
        
            Active
            
        
            Viewed 4,552 times
        
    4
            
            
        - 
                    r u need tom change the language in button action – Anbu.Karthik Oct 29 '14 at 08:48
- 
                    you can create a custom localisation procedure anytime which can be operated by a button inside the app. where were you stuck implementing such solution? – holex Oct 29 '14 at 11:27
2 Answers
5
            
            
        Create 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)]
@interface LocalizeHelper : NSObject
// a singleton:
+ (LocalizeHelper*) sharedLocalSystem;
// this gets the string localized:
- (NSString*) localizedStringForKey:(NSString*) key;
//set a new language:
- (void) setLanguage:(NSString*) lang;
@end
and in .m file
// LocalizeHelper.m
#import "LocalizeHelper.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];
        }
    }
}
@end
For Setting language use
 LocalizationSetLanguage(@"ar");
For Getting Values use
self.Mylabel.text = LocalizedString(@"rent");
For each language you want to support you need a file named Localizable.strings. This works exactly as described in Apples documentation for localization.
// TABS
"buy" = "شراء";
"rent" = "إيجار";
"addListing" = "إضافة إعلان" ;
"calculator" = "دلالي" ;
"news" = "أخبار" ;
 
    
    
        neo D1
        
- 1,710
- 13
- 15
- 
                    Thank you its working fine.But I am facing an issue if I kill the app again open means need a changed language..How can I save/get a current chosen language. – Shangari C Nov 15 '18 at 04:36
4
            You can't change the language of the device in your application but you can change it just for your application by changing the AppleLanguages property in NSUserDefaults. Please note however I believe this still requires restarting the app itself but doesn't require a device restart.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];
Here's another similar question that also maybe able to help you
- 
                    1the point is here not changing the language on the device programmatically, but changing the language inside the app by tapping a button. the _first_ is not possible via public API, the _second_ one is totally reasonable and can be done without any restriction. – holex Oct 29 '14 at 11:29
 
     
    