Last active
August 29, 2015 14:23
-
-
Save casspangell/76d743d974ab8058f106 to your computer and use it in GitHub Desktop.
Setting iOS localization on the fly
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> | |
#define RUNTIME_LANGUAGE_STORAGE @"runtimeLanguageStorage" | |
#define RunTimeLanguageEnglish @"en" | |
#define RunTimeLanguageSpanish @"es-MX" | |
#undef NSLocalizedString | |
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil] | |
#define setLanguage(x) [[NSUserDefaults standardUserDefaults] setObject:x forKey:RUNTIME_LANGUAGE_STORAGE]; [[NSUserDefaults standardUserDefaults] synchronize]; | |
@interface NSBundle (RunTimeLanguage) | |
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName; | |
@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
#import "NSBundle+RunTimeLanguage.h" | |
@implementation NSBundle (RunTimeLanguage) | |
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName { | |
NSString *languageCode = [[NSUserDefaults standardUserDefaults] objectForKey:RUNTIME_LANGUAGE_STORAGE]; | |
if ( languageCode == nil ) { | |
languageCode = @"en"; | |
} | |
NSString *path= [[NSBundle mainBundle] pathForResource:languageCode ofType:@"lproj"]; | |
NSBundle *languageBundle = [NSBundle bundleWithPath:path]; | |
NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil]; | |
return localizedString; | |
} | |
@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
- (IBAction)englishPressed:(id)sender { | |
setLanguage(RunTimeLanguageEnglish); | |
[self reloadViewUIForTranslationMethod]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment