How can I get the iPhone device language by below method to server?
$_SERVER['HTTP_ACCEPT_LANGUAGE']
How can I get the iPhone device language by below method to server?
$_SERVER['HTTP_ACCEPT_LANGUAGE']
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.
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
}
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
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']
'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.
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
}
Swift: To get the device language via native API
let language = NSLocale.preferredLanguages()[0] as String