I would suggest using a category class to localize strings for a specified language, which automatically determines the current language and localizes it.
Create a category class called RunTimeLanguage
.h file
#import <Foundation/Foundation.h> @interface NSBundle (RunTimeLanguage) #define NSLocalizeString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil] - (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName; @end
.m file
#import "NSBundle+RunTimeLanguage.h" @implementation NSBundle (RunTimeLanguage) - (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName { NSString *StrCurrentLang = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0]; NSString *path= [[NSBundle mainBundle] pathForResource:StrCurrentLang ofType:@"lproj"]; NSBundle *languageBundle = [NSBundle bundleWithPath:path]; NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil]; return localizedString; } @end
You can directly access the localized value using the instructions below.
NSLocalizeString(@"yourText", nil)
source share