I'm about to localize an iPhone application. I want to use a different URL when the user's language (iOS system language) is german.
I want to know if this is the correct way of doing that:
NSURL *url = [NSURL URLWithString:@"http://..."]; // english URL
NSString* languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([languageCode isEqualToString:@"de"]) {
url = [NSURL URLWithString:@"http://..."]; // german URL
}
I understand that [NSLocale currentLocale]
returns the language based on the current region, but not the system language, neither does [NSLocale systemLocale]
work.
(I don't want to use NSLocalizedString
here! )
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
Your code is OK. But I will doit like this:
NSString *urlString = nil;
NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([languageCode isEqualToString:@"de"]) {
urlString = @"http://...";
}else{
urlString = @"http://...";
}
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];