NSLocale loaded language in the application

In my interface, I display the locale display name with this:

[[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier] 

But this gives the display name using the system locale, which does not always match the application locale. For example, if I installed my system in French and my application does not have French localization, it will display the French display name in the English interface. Instead, I would like to have an English display name in the English interface, so I don't mix languages.

+6
source share
2 answers

I'm not sure if this is what you want ... But this is a great way to find out what language / localization your application is running in:

 [[NSBundle mainBundle] preferredLocalizations] 

If the application supports both English and French, it will return an array of both in the preferred order. When testing, one of 0 appears to be a downloadable and running xib.

+3
source

You must create a plist file that you localize, in your plist store the correct locale identifier for the corresponding localization, when you use your piece of code, you must load the locale from this localized plist file and use it to get the display name.

Thus, your language will always correspond to the interface language on the screen. I needed to do this for some dates that I wanted to format, and not rely on the system, since the calendar format can be installed in another language on the system ...

hope this helps ...

enter image description here

 - (NSDictionary*)getLocalizedCalendarStrings{ NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"calendar" ofType:@"plist"]; // will return the path of the plist in the right language-specific .lproj directory) NSDictionary* calendar = [NSDictionary dictionaryWithContentsOfFile:plistPath]; return calendar; } 

And use it like this:

 NSDictionary * calendar = [self getLocalizedCalendarStrings]; NSString * localeIdentifier = [calendar objectForKey:@"locale_identifier"]; [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: localeIdentifier] 

To avoid the risk of introducing these locale lines in misspelt, etc., or if you want to improve a code-oriented solution, you can of course use a property named "default_language" as BOOL in plist and set it to YES in English only language, in your code just check this value, and if it is NO, then get the device localization, should it be YES, then you know that the application is either in English, because this is the device’s setting, or in English, therefore that it does not support the current locale of the device and therefore, by default it fell off into English ... so that "default_language" should be "YES", hard-code the locale to en_US or en_UK if you are a patriotic shave ...

Something like the lines of this example may solve your problem, a category for NSLocale, but of course it will force you to have a localized plist for each language you support ...

 @implementation NSLocale (AppLocale) +(NSString*)applicationCurrentLocale{ NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"currentLocale" ofType:@"plist"]; NSDictionary* currentLocaleData = [NSDictionary dictionaryWithContentsOfFile:plistPath]; if([[currentLocaleData objectForKey:@"english_default"] boolValue] == YES){ return @"en_US"; }else{ NSString * deviceLocaleIdentifier = [[NSLocale currentLocale] localeIdentifier]; return [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:deviceLocaleIdentifier]; } } @end 
+3
source

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


All Articles