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! )
You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").
1) String(Locale. preferredLanguages[0]. prefix(2)) It returns phone lang properly. 2) Select Project(MyApp) -> Project (not Target) -> press + button into Localizations , then add language which you want.
To get the user's locale in the browser, access the first element of the languages property on the navigator object, e.g. navigator. languages[0] . The property returns an array of strings that represent the user's preferred languages.
The String localeCompare() method compares the string with the reference string according to the browser's language, which means the current locale. It is an object method, so users need to invoke the method by using the reference string and passing the second string as a parameter.
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With