Last active
July 5, 2016 12:01
-
-
Save proactive-solutions/133172d72d8ffc2e8db9 to your computer and use it in GitHub Desktop.
Use localisation in application without need to change the device language setting by user. Particularly useful in app which supports more than one languages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSBundle (Language) | |
+(void)setLanguage:(NSString*)language; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use this code as .h file | |
// | |
// LocalizeHelper.h | |
// KocovicGuide | |
// | |
// Created by ravinder.reddy on 14/09/15. | |
// Copyright (c) 2015 Mikropis. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSBundle (Language) | |
+(void)setLanguage:(NSString*)language; | |
@end | |
// use below code in .mfile | |
// | |
// LocalizeHelper.m | |
// KocovicGuide | |
// | |
// Created by ravinder.reddy on 14/09/15. | |
// Copyright (c) 2015 Mikropis. All rights reserved. | |
// | |
#import "NSBundle+Language.h" | |
#import <objc/runtime.h> | |
static const char _bundle=0; | |
@interface BundleEx : NSBundle | |
@end | |
@implementation BundleEx | |
-(NSString*)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName | |
{ | |
NSBundle* bundle=objc_getAssociatedObject(self, &_bundle); | |
return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [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]); | |
}); | |
objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment