Localization no longer works in iOS 9 / iOS 9.0.1

Since I am creating iOS applications, I use the following code to translate / localize my applications:

NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0]; if ([language isEqualToString:@"de"]) { // localized language } else { //base language } 

But since upgrading to iOS 9, this code no longer works. All my applications are now available in English.

Neither the applications that I already have in the application store, nor the applications that I run in the simulator are no longer localized.

It would be great if you could tell me how I need to programmatically convert my code to iOS 9.

+5
source share
2 answers

I could solve the problem.

If I use the following code, then localization works under iOS 9.

 [[NSBundle mainBundle] preferredLocalizations]; NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0]; if ([language isEqualToString:@"de"]){ // localization } else { //base language } 

I hope this helps some of you too.

+1
source

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) 
0
source

Source: https://habr.com/ru/post/1232289/


All Articles