2

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

7 Answers7

8

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.

Paul Lynch
  • 19,769
  • 4
  • 37
  • 41
  • 2
    :This is efficient way . But you have to restart application which is not user friendly. – V-Xtreme Apr 16 '13 at 08:54
  • 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. – Paul Lynch Apr 16 '13 at 09:23
  • 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. – V-Xtreme Apr 16 '13 at 09:57
  • Seems to work in iOS 7.1 simulator. No need to restart – voghDev Dec 03 '14 at 11:58
  • 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. – Can Sep 06 '16 at 17:46
  • I did the same (exit), but this was in an enterprise app. Might not be acceptable for the App Store. – Paul Lynch Sep 08 '16 at 17:37
  • @PaulLynch. Is there any option without restarting the app . Please help me – Uma Madhavi Sep 15 '17 at 10:03
  • Only by managing your own language lookup. There are several examples given in other answers. But to switch languages automatically requires a restart. – Paul Lynch Sep 16 '17 at 08:50
3

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.

delannoyk
  • 1,216
  • 1
  • 12
  • 22
2

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.

qman64
  • 151
  • 1
  • 4
1

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");
Satheesh
  • 10,998
  • 6
  • 50
  • 93
1

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!

Irshad Mansuri
  • 397
  • 2
  • 12
  • 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 – ram880 Sep 25 '14 at 11:30
0

You have to use localization for this . Just store the key and value in side localizable.string and load the appropriate .string file as per your requirement . You can refer following tutorial for this : http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial

V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
0

SWIFT 2.0 version We can do this on run time without restarting the app in Swift 2.0 as follows

Make a function within a class, lets say LanguageManager is the name of the class

class LanguageManager
{

    static let sharedInstance =  LanguageManager()

    //Make a function for Localization. Language Code is the one which we will be deciding on button click.
    func LocalizedLanguage(key:String,languageCode:String)->String{

        //Make sure you have Localizable bundles for specific languages.
        var path = NSBundle.mainBundle().pathForResource(languageCode, ofType: "lproj")

        //Check if the path is nil, make it as "" (empty path)
        path = (path != nil ? path:"")

        let languageBundle:NSBundle!

        if(NSFileManager.defaultManager().fileExistsAtPath(path!)){
            languageBundle = NSBundle(path: path!)
            return languageBundle!.localizedStringForKey(key, value: "", table: nil)
        }else{
            // If path not found, make English as default
            path = NSBundle.mainBundle().pathForResource("en", ofType: "lproj")
            languageBundle = NSBundle(path: path!)
           return languageBundle!.localizedStringForKey(key, value: "", table: nil)
        }
    }
}

How to Use this.
Now Assume you have two Languages for the application (English and Spanish)
English - en
Spanish - es
enter image description here

Suppose you have to place something for a label

//LOGIN_LABEL_KEY is the one that you will define in Localizable.strings for Login label text
 logInLabel.text = LanguageManager.sharedInstance.LocalizedLanguage("LOGIN_LABEL_KEY", languageCode: "es")

//This will use the Spanish version of the text. 
//"es" is the language code which I have hardcoded here. We can use a variable or some stored value in UserDefaults which will change on Button Click and will be passed in loginLabel

For more info on language codes see this link below
https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html

or

http://www.ibabbleon.com/iOS-Language-Codes-ISO-639.html

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98