So here's the problem...
My app will have a link to buy a product from a global online retailer, however in the UK it will come from "shop.co.uk", in the US "shop.com", Australia "shop.oz.au", etc
How do I set the locale to location rather than language, as they are all English speaking countries?
Many thanks in advance
Rob
using this code you can get the country from the current locale, and with this you can choose the proper URL
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
Related
I am working on developing the cordova HERE map plugin to get route calculation data in app. I am working on adding the functionality for ios.
I want to get a maneuver instructions for the route, following is my code to get maneuver instructions for the calculated route.
NSString * language = [[NSLocale preferredLanguages] firstObject]; // getting lang code en
NSArray* inst= [route instructionsForLanguage:language unitSystem:NMARouteInstructionsUnitSystemMetric];
But I don't know why it is returning empty array. Please help it anyone knows how to get maneuver instructions.
That happening because NSLocale preferredLanguages can give you language code instead of language and country code, for example
[NSLocale preferredLanguages] // -> tr
According to HERE maps documentation function expect language code, like en-US
language should be a valid code according to the IETF BCP-47 standard (see http://tools.ietf.org/html/bcp47 ).
So to make it easier for you, to need to get language code instead.
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [NSString stringWithFormat: "%#-%#", [locale languageCode], [locale countryCode]]; // -> tr-TR
I am working on an app which asks users for phone number and has a country picker, where the user should input country code.
But while testing null country code values, I found out that somehow my iPhone with iOS 10.2.1 has a region format set to Europe.
The Europe region does not exist in the regions list when I search for it.
When I try to get the current locale country code
NSLocale *currentLocale = [NSLocale currentLocale];
return [currentLocale objectForKey:NSLocaleCountryCode]
I get en_150 which is not useful to determine user's country, at least on my device. Returned country code is 150, which does not exist.
I found a workaround to find to country code by using CTTelephonyNetworkInfo
if(![currentLocale objectForKey:NSLocaleCountryCode]) {
CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
return [countryCode capitalizedString];
}
But how does it work with this Europe locale and why do I have it set like this?
First of all, Europe is a continent not a country. It doesn’t have a country code. It does, however, have a region code. That region code is indeed 150.
Continents have numeric area codes instead including 001 for “World” and 150 for “Europe”.
Be sure to read my article “Are there standard language codes for ‘World English’ and ‘European English’” for a much more detailed explanation. These codes are based in UN M.49, which is the basis for ISO 3166, which is the standard you’re probably thinking of when you say “country codes”.
The problem you’re facing is that you can’t use someone’s language or formatting preferences to determine their physical location. My current device’s locale is en_DK but my physical location is NO. Many devices default to en_US and users all over the world never change the default settings.
Please use CoreLocation to automatically determine the user’s location, or just ask the user to disclose their location.
When I have an UI string with capital letters, i'm used to define them in lowercase as for all the others, and then to use uppercaseStringWithLocale:[NSLocale currentLocale].
But recently I happened to notice that the [NSLocale currentLocale] may not be the one used in your app. For example if your device is in Turkish but your app only support english, the currentLocale would be a Turkish locale while your app is localized in english.
With those settings, a direct effect of using [NSLocale currentLocale] is that my uppercaseString will be "İ LİKE İOS" instead of "I LIKE IOS".
So far, the only workaround I see is to create a category of NSLocale to add a +(NSLocale*) applicationLocale; and use it in all uppercaseStringWithLocale:.
+ (NSLocale*) applicationLocale
{
NSMutableDictionary<NSString*,NSString*>* localeComponents = [[NSLocale componentsFromLocaleIdentifier:[NSLocale currentLocale].localeIdentifier] mutableCopy];
localeComponents[NSLocaleLanguageCode] = NSBundle.mainBundle.preferredLocalizations.firstObject;
return [NSLocale localeWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:localeComponents]];
}
My question is simple: am I doing this the right way or did I miss something? I indeed wonder why Apple links to currentLocale while it won't work as expected in a lot of cases.
The most robust way to get the application locale is to edit your Localizable.strings files. In the English localization file add an entry
"lang"="en";
in the German localization file add an entry
"lang"="de";
in the French localization file add an entry
"lang"="fr";
and so on... You can get the localization code with NSLocalizedString(#"lang").
I'm trying to determine whether to use imperial or metric units automatically before asking the user to enable location services. I know that you can't get precise data but all I really need is United States or not. Can you determine what store it was downloaded from or use IP address or anything like that?
You could use the device's locale to achieve this...
Obj-C
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
Swift
let locale = NSLocale.currentLocale()
let countryCode = locale.objectForKey(NSLocaleCountryCode)
Country codes are in the format US (United States), FR (France), etc...
Note that the locale is based on the user's device settings and not necessarily the current physical location of the device.
You're probably looking for NSLocale:
let theLocale = NSLocale.autoupdatingCurrentLocale()
print(theLocale.objectForKey(NSLocaleMeasurementSystem))
print(theLocale.objectForKey(NSLocaleUsesMetricSystem))
Look at the NSLocale class reference for more options.
Swift 3
NSLocale.current
NSLocale.Key.countryCode
Yes, annoyingly renamed again, don't you have more important stuff to attend Swift team???
I have a very bizarre issue with the facebook-ios-sdk. Client is complaining about the in-app dialogs showing up in Spanish, when neither their Facebook nor phone is set for spanish. This is happening across multiple devices and multiple users/accounts.
The Facebook SDK should be pulling the user's language/locale preferences from NSLocale, correct? Is there a way to set or test this? Is there some way the language is being set in the HTTP Header Requests for the in-app dialog incorrectly?
(using SSO if it makes a difference)
Thanks,
This is apparently a Facebook-level issue. You can track the status here:
http://developers.facebook.com/bugs/407246299295529?browse=search_4fa410ea79db26337556383
"On initial login using Facebook, the dialog asking the user to authorize this application is displayed in seemingly random languages.. Only Happens when user is connected over Wifi."
You can check the country code:
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
NSString *language;
if ([[NSLocale preferredLanguages] count] > 0)
{
language = [[NSLocale preferredLanguages] objectAtIndex:0];
}
else
{
language = [locale objectForKey:NSLocaleLanguageCode];
}