---
title: "How To Change Localization Internally In Your iOS Application"
description: "There’s no official way provided by Apple for this purpose. Let’s look at two methods for solve this problem."
url: https://manytomany.co/blog/how-to-change-localization-internally-in-your-ios-application/
published: 2015-01-28
---

# How To Change Localization Internally In Your iOS Application

28 January 2015 · Maxim Bilan

This is an archived article from the website of Factorial Complexity Ltd., an older incarnation of many-to-many. Information may be outdated or inaccurate.

Unfortunately, there’s no official way provided by Apple for this purpose. Let’s look at two methods for solve this problem.

### Method 1

Apple provides a way to specify application specific language, by updating the “AppleLanguages” key in `NSUserDefaults`. For example:

```objc
[[NSUserDefaults standardUserDefaults] setObject:@"fr" forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
```

For working this method, you’ll have to set it before `UIKit` initialized.

```objc
#import &lt;UIKit/UIKit.h&gt;
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        [[NSUserDefaults standardUserDefaults] setObject:@"fr" forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
```

The problem of this method is that the app has to be relaunched to take effect.

### Method 2

The solution is to swap the `mainBundle` of our application as soon as user changes their language preferences inside the app.

See the category for `NSBundle`.

Header:

```objc
#import &lt;Foundation/Foundation.h&gt;

@interface NSBundle (Language)

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

@end
```

Implementation:

```objc
#import "NSBundle+Language.h"
#import &lt;objc/runtime.h&gt;

static const char kBundleKey = 0;

@interface BundleEx : NSBundle

@end

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
    NSBundle *bundle = objc_getAssociatedObject(self, &amp;kBundleKey);
    if (bundle) {
        return [bundle localizedStringForKey:key value:value table:tableName];
    }
    else {
        return [super localizedStringForKey:key value:value table:tableName];
    }
}

@end

@implementation NSBundle (Language)

+ (void)setLanguage:(NSString *)language
{
    static dispatch_once_t onceToken;
    dispatch_once(&amp;onceToken, ^{
        object_setClass([NSBundle mainBundle],[BundleEx class]);
    });
    id value = language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil;
    objc_setAssociatedObject([NSBundle mainBundle], &amp;kBundleKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
```

In this method a problem that may arise is updating elements on active screens. You can reload your `rootViewController` from our application delegate, will always work reliably.

```objc
- (void)reloadRootViewController
{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    NSString *storyboardName = @"Main";
    UIStoryboard *storybaord = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    delegate.window.rootViewController = [storybaord instantiateInitialViewController];
}
```

All code you can see on [github](https://github.com/maximbilan/ios_language_manager). With simple example.

Please, use for free and like it.

Note: in example project by default the app uses method #2. You can disable this. Just comment define `USE_ON_FLY_LOCALIZATION`.

![Vitalii](https://manytomany.co/vitalii-yellow.png)

## Hi. I'm Vitalii.

I’m a software developer, technical manager and solo entrepreneur. I have been building software products all my life. I am offering **CTO as a Service** at many-to-many.

## Contact me

Let me know what are you looking for. I will reach out to you via the provided email with quote and follow up questions. Alternatively, [book a call](https://calendly.com/many-to-many/intro) or reach me directy by email [hello@manytomany.co](mailto:hello@manytomany.co).

Or using your preferred messaging app:

 [![WhatsApp QR Code](https://manytomany.co/qr/qr-whatsapp.png)](https://wa.me/qr/QH6GQNCO5CZEO1)[WhatsApp](https://wa.me/qr/QH6GQNCO5CZEO1)

 [![Telegram QR Code](https://manytomany.co/qr/qr-telegram.png)](https://t.me/wicharek)[Telegram](https://t.me/wicharek)
