Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization: How to get the current user language?

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! )

like image 423
Felix Avatar asked Jan 19 '11 15:01

Felix


People also ask

How do I find current device language?

You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").

How do I get the current language code in Swift?

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.

How do I get user browser language?

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.

What is current locale JavaScript?

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.


1 Answers

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]];
like image 59
Alex Terente Avatar answered Oct 22 '22 15:10

Alex Terente