1

How can I get the iPhone device language by below method to server?

$_SERVER['HTTP_ACCEPT_LANGUAGE']
Charles
  • 50,943
  • 13
  • 104
  • 142
Monk
  • 65
  • 1
  • 6
  • 1
    Are you asking for a method to retrieve local language at iPhone client, or remote language at HTTP server? – He Shiming Apr 15 '12 at 07:01

6 Answers6

8

If you're asking how to receive the device language, you can do this:

NSString *userLocale = [[NSLocale currentLocale] localeIdentifier];
NSString *userLanguage = [userLocale substringToIndex:2];

if([userLanguage isEqualToString:@"en"]){
    //English
}
else if([userLanguage isEqualToString:@"es"]){
    //Spanish
}
else if([userLanguage isEqualToString:@"fr"]){
    //French
}

For the rest of the language codes, you can head to this website. The codes for iOS are the ISO 639-1 codes in the last column.

pasawaya
  • 11,515
  • 7
  • 53
  • 92
3

The above answers didn't work for me because [[NSLocale currentLocale] localeIdentifier] would always return en_US. This is because changing the language preference in the simulator is recognized by [NSLocale preferredLanguages]. Here's an alternative way of checking the device's language settings:

NSString *preferredLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLog(@"%@", preferredLanguage);

if ([preferredLanguage isEqualToString:@"en_US"])
{
    // English
}
else if([preferredLanguage isEqualToString:@"es"])
{
    // Spanish
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Nyk
  • 53
  • 1
  • 6
2

Swift

To get current language of device

NSLocale.preferredLanguages()[0] as String

Output:

en-IN

To get the language of app

NSBundle.mainBundle().preferredLocalizations[0] as NSString

Note:

It fetches the language that you have given in CFBundleDevelopmentRegion of info.plist

if CFBundleAllowMixedLocalizations is true in info.plist then first item of CFBundleLocalizations in info.plist is returned

Source

PHP

The browser generally sends a HTTP header, name Accept-Language, that indicates which languages the user is willing to get

$_SERVER['HTTP_ACCEPT_LANGUAGE']
Community
  • 1
  • 1
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
0
  'HTTP_ACCEPT_LANGUAGE'
   Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.

http://php.net/manual/en/reserved.variables.server.php

Basically all you need to is find out what the codes are. You should probably check for something like this:

   if ($_SERVER['HTTP_ACCEPT_LANGUAGE'] != "")
   {
       $currentLanguage = "en";
   }

I havent touched PHP in a while, but there is a chance the browser will not report back a language depending on which browser there is. You will need a default.

Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
  • The question didn't mention anything about PHP, why is this assumed? – He Shiming Apr 15 '12 at 07:02
  • I might be off. I used to do PHP and "$_" indicates global variables in PHP. $_Server is a PHP variable. Like I said, might be wrong since I haven't seen it accessed like this anywhere else. – Serguei Fedorov Apr 15 '12 at 07:04
  • Your right, I should of looked. A little Google didn't show anything that put $_session as part of xcode. Let me know if you find anything I'm actually kind of curious if they are using the same syntax. – Serguei Fedorov Apr 15 '12 at 07:13
  • I'm pretty sure the OP wants to know how to retrieve the iPhone's language on the server side, based on the HTTP headers the iPhone sends. – curtisdf Jun 14 '12 at 04:07
0

I know this is an old post, but today I needed to do just what Monk asked and the solution posted by qegal did not work for me so I'm posting my answer. But it did get me on the right track so I voted +1 for qegal.

In my app I need to do something when the phone is set to Swedish, so on launch the app checks if the language is set to Swedish and if so call the appropriate method. The code is nested in the viewDidLoad: method. Remember to set the Region format in your phone's settings to the corresponding language.

NSString *userLocale = [[NSLocale currentLocale] localeIdentifier];
NSLog(@"%@", userLocale);
if ([userLocale isEqualToString:@"sv_SE"]) {
    // Swedish, do something when phone is set to Swedish or whatever language you prefer

}
0

Swift: To get the device language via native API

let language = NSLocale.preferredLanguages()[0] as String
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168