I seem to be going in circles around this problem and I am heading no where now.
I am trying to force the language of my app (to swedish) as shown below.
@autoreleasepool {
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"sv", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults]synchronize];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
I have done it in the main method, so that preference is set as soon as the application is launched.
Then when i want to sort an array alphabetically i use NSSortDescriptor with selector localizedCaseInsensitiveCompare:.
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"iName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor];
self.ingredientsList = [NSMutableArray arrayWithArray:[self.ingredientsList sortedArrayUsingDescriptors:descriptors]];
But my problem is sorting with my local language does not happen the first time. But if I restart the app, the sorting is according to the language (Swedish). Why doesnt it happen the first time?
What am i doing wrong?
Help/suggestions will be appreciated.
Thanks
@DarkDust is correct. You should not try to modify the locale itself. That’s not under your control, and as you’re seeing, it doesn’t work very well. Most importantly, if you just want to modify one sort operation, this is a very big hammer to do that…. You could potentially play with the various localization
Info.plistentries (particularlyCFBundleLocalizations) to get this to work right, but not just to sort a single list.What you want to use is
compare:options:range:locale:, which allows you to pass a specific locale. You can use this method inside of asortedArrayUsingSelector:orsortedArrayUsingComparator:.Alternately, you could use a category to add a custom
swedishCaseInsensitiveCompare:toNSString. You could then useNSSortDescriptoras you are currently.Example.
äis sorted afterain English, but afterzin Swedish: