• Stars
    star
    233
  • Rank 172,230 (Top 4 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Language Manager iOS

How to change localization internally in your iOS application

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

Method #1

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

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

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

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "LanguageManager.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 the user changes their language preferences inside the app.

See the category for NSBundle.

Header:

#import <Foundation/Foundation.h>

@interface NSBundle (Language)

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

@end

Implementation:

#import "NSBundle+Language.h"
#import <objc/runtime.h>

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, &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(&onceToken, ^{
        object_setClass([NSBundle mainBundle],[BundleEx class]);
    });
    id value = language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil;
    objc_setAssociatedObject([NSBundle mainBundle], &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.

- (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 in this repository. With a simple example.

alt tag

Please, use for free and like it .

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

More details on the blog here.

More Repositories

1

Mac-OS-X-App-Menu-Bar-Popup

Mac OS X Application like a menu bar popup message
Swift
183
star
2

Calendar-iOS

Calendar View
Objective-C
161
star
3

iOS-CoreMotion-Example

Core Motion in iOS using Swift
Swift
101
star
4

SwiftGoogleTranslate

Lightweight framework for using Cloud Translation API by Google
Swift
86
star
5

SwiftAssetsPickerController

A simple assets picker controller based on iOS 8 Photos framework. Supports iCloud photos and videos. It's written in Swift.
Swift
85
star
6

Swift-Amazon-S3-Uploading-Tutorial

How to upload a file to Amazon S3 using Swift
Swift
76
star
7

UICollectionViewHorizontalPaging

iOS UICollectionView Page Scrolling
Swift
71
star
8

iOS-Crosswords-Generator

A simple algorithm for generating crosswords written on Swift. Based on Python Crossword Puzzle Generator.
Swift
52
star
9

iOS-Shared-CoreData-Storage-for-App-Groups

iOS Shared CoreData Storage for App Groups
Swift
49
star
10

SwiftHUEColorPicker

iOS HUE color picker
Swift
46
star
11

iOS-Swift-Drawing-App

Swift Drawing Application Sample
Swift
40
star
12

iOS-AirDrop-Custom-Data

iOS Airdrop Custom Data Transferring Example
Objective-C
38
star
13

CheckMarkView

UI view which draws programmatically a checkmark with different styles
Swift
35
star
14

iOS-Swift-In-App-Purchases-Sample

iOS Swift In-App-Purchases Sample
Swift
33
star
15

SKTextureGradient

A SpriteKit SKTexture Gradient
Swift
29
star
16

iOS-MapKit-Tutorial

iOS MapKit Getting Started
Swift
26
star
17

iOS-MySQL-Client

iOS MySQL client
Objective-C
26
star
18

iOS-Today-Extension-Simple-Tutorial

iOS Today Extension Simple Tutorial
Swift
25
star
19

Quizlet-iOS

iOS framework for using Quizlet API 2.0
Objective-C
18
star
20

UIAlertController-Customization

Customization of UIAlertController
Swift
18
star
21

JobsFinder

A script which parses Upwork RSS feed and sends notifications to Telegram
JavaScript
17
star
22

SwiftlySlider

A simple iOS slider control
Swift
14
star
23

iOS-Heart-Rate-Monitor

iOS Heart Rate Monitor
Objective-C
12
star
24

SignalStrengthIndicator

Signal Strength Indicator
Swift
11
star
25

iOS-Dragon-Mobile-SDK-Example

Nuance Dragon Mobile SDK and ObjectAL
HTML
10
star
26

MBFileDownloader

A file downloader.
Objective-C
9
star
27

The-Pursuit-Demo

3D racing with physics powered by Marmalade SDK and Bullet Physics
GLSL
9
star
28

iOS-UIImage-render-to-PDF

iOS Render UIImage to PDF and merging PDF files
Objective-C
9
star
29

iOS-Pie-Chart

iOS Pie Chart
Objective-C
9
star
30

SwiftlyScrollSlider

A custom scroll slider for UIScrollView
Swift
8
star
31

3D-Touch-Quick-Actions-Demo

iOS 3D Touch: How to add quick actions
Swift
8
star
32

iOS-YouTube-Browser

iOS YouTube Browser Sample
Objective-C
8
star
33

iOS-Swift-ObjC-Mix

An example of the mixing Swift and Obj C code in the same project
Objective-C
7
star
34

SwiftThicknessPicker

iOS thickness picker
Swift
6
star
35

iOS-Document-Iteraction

iOS: How to copy epub files from your app to iBooks app
Swift
6
star
36

iOS-Tag-List

iOS Tag List
Objective-C
6
star
37

SwiftTintedButtonExtension

A small Swift extension for tinting UIButton, like a UIBarButtonItem or a UINavigationItem
Swift
5
star
38

iOS-QuickBlox-File-Uploader-Downloader

Quickblox: Uploading from a file, downloading to a file
Objective-C
4
star
39

UIStripedView

A striped view
Objective-C
3
star
40

WaitSpinner

Very very simple activity indicator
Objective-C
3
star
41

iOS-Frequency-Table

iOS Frequency Table
Objective-C
3
star
42

UITextViewPlaceholder

UITextView Placeholder Tutorial
Swift
3
star
43

UISegmentedControl_IconAndText

A simple category for adding icon and text together to default UISegmentedControl
Objective-C
3
star
44

UIColorRGBA

Provides a convenience UIColor method for set up a color from a HEX string in Swift.
Swift
2
star
45

SwiftyAccountKit

Facebook AccountKit wrapper
Swift
2
star
46

T-Block

T-Block
C++
1
star
47

SwiftOxfordAPI

A framework to use Oxford Dictionaries API written in Swift
Swift
1
star
48

Patchcord

The project represents an example of Redux architecture using SwiftUI + Combine + CoreData.
Swift
1
star
49

Locale-Alphabet

An example that shows how to take the current alphabet from the text
Objective-C
1
star
50

SwiftUI-WidgetKit-Gradient-Issue

This code shows a bug with gradient colors which is happening using Widgets extension for iOS 14
Swift
1
star
51

CIColorRGBA

Provides a convenience CIColor method for set up a color from a HEX string in Swift
Swift
1
star