BalestraPatrick
BalestraPatrick

Reputation: 10144

How to get the user country always in English?

In my app I have a system with geolocation that compares the user's country with a string in my code. Here is something similar:

if (UserLocation.country == @"Switzerland") {
      //country is Switzerland
}

This system works fine if the user language of the device is English. Some users have reported that it isn't working and that they have the device in German or Italian.

How can I force the device to give me the string of the country always in English?

Upvotes: 0

Views: 332

Answers (2)

Manuel BM
Manuel BM

Reputation: 888

I used Google Places and made a request to this url

"https://maps.googleapis.com/maps/api/geocode/json?latlng=\(userLocation.latitude),\(userLocation.longitude)&key=\(self.googlePlacesApiKey)&language=en&result_type=country"

so it always gives me the results in english independently from the user's language or locale settings

you can change change the information you receive with result_type parameters. Full guide here:

https://developers.google.com/maps/documentation/geocoding/intro#Viewports

Upvotes: 0

Monolo
Monolo

Reputation: 18253

It is generally not ideal to use natural language (i.e., the language we speak and write as humans) to store information that is inherently symbolic.

You found a good example yourself that arises when your software travels, as it does with modern devices used world-wide.

Another reason could be that even within the same language there may be several widely-used ways to refer to the same "object" or topic. Just think about America, which can be referred to as America, US, USA and probably a couple of other names, too. Or the UK, which is referred to as both the United Kingdom (UK) and Great Britain (GB). Or even for some simple object properties: Is it color or colour? I am sure that more examples can be found for other languages - especially if they are spoken in more than one country.

On top of that, natural language is also prone to misspellings, typing mistakes, use of abbreviations and changes to spelling standards over time.

Hence, a better solution would be to use a language-independent method.

The commonly accepted practice for country names is to use ISO country codes ("CH" for Switzerland, "DE" for Germany, etc., in case you use the 2-letter versions.)

In your database (which can be as simple as a plist file) you would create a field called for instance countrycode of length 2 characters which should take a value of "US", "CH", "DE", "FR", etc. following the ISO 3166 standard. You can also use 3-letter ISO country codes, but it seems that the most commonly used is the 2-letter one.

Then, for UI purposes you would keep another table, which could be a .strings file for localization with the natural language names of the countries:

US = United States;
DE = Germany;
CH = Switzerland;
/* etc. */

On the screen you can translate to natural language:

NSString *isoCountryCode = @"CH"; // Or however you want to set it
NSString *countryName = NSLocalizedString(isoCountryCode, @"Default string if country name not found.");

// Now you can set the country name in human-readable form in the UI
myCountryNameLabel.text = countryName;

In the rest of your code, i.e., the code that handles the data before showing it to the user in the UI, you only use the ISO codes to distinguish countries. Some people refer to this part of the code as the business logic, and as a simple example it could look something like this:

if ( [UserLocation.countryCode isEqualToString: @"CH"] ) {
     // Do things needed for Switzerland
}

Internationalization is an extensive topic, and you can find Apple's documentation on it here for iOS. The OS X version is here - but they will be largely identical.

Upvotes: 5

Related Questions