Localized “Current Location” string for iPhone apps

I’m writing an app that has a “get directions from here to the selected address” function at some point.

When the user invokes that function, the app creates a “Google Map” URL and opens the official Maps app with the user current location as source and the selected address as destination.

In situations like this, you have two options to determine the current location of the user:

  1. use the CoreLocation framework;
  2. use the string “Current Location” as the source address.

The first option requires you to code all the CoreLocation related stuff and requires the user to accept being geolocated by the app; the second option is much simpler to implement, because it’s Maps that takes care of everything concerning the geolocation.
Besides code writing considerations, remember also that continuous use of GPS in your app consumes a lot of battery power.

Why would anybody use the first option, then? Unfortunately, the “Current Location” trick works only if the user’s iPhone is set to use English or British English (see this question on Stack Overflow).
Every language that can be set on the iPhone has its own localized version of this string (in Italian is “Posizione attuale”).

Please note also that simply translating “Current Location” doesn’t necessarily do: you must match the exact string that Apple chose for the localization in use.
So even if you speak all the 34 currently supported languages :-) , you can’t be sure that your translations will properly work everywhere…

With some patience and some sniffing into the iPhone firmware, I managed to discover the exact translation of “Current Location” in all the languages supported by the iPhone (and Maps thus) and I wrote the helper class LocalizedCurrentLocation that can provide your app with the correct localized version of “Current Location”.

The class is very simple and has only one class method:

+ (NSString *)currentLocationStringForCurrentLanguage;

To get the localized string you simply call:

[LocalizedCurrentLocation currentLocationStringForCurrentLanguage]

Usage example:

NSString *destinationAddress = [NSString stringWithFormat:@"%@+%@+%@",
    [address street],
    [address city],
    [address country]];

NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];

NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
    [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
    [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

Of course this trick is not guaranteed to work forever; it doesn’t use any particular “forbidden” piece of code, but it relies on the assumption that everything is as is now.
I tested the class with a couple of languages and everything seems to work seamlessly; I don’t have time/patience to test all the 34 languages so let me know if you find any issues.

You can download the class here: LocalizedCurrentLocation.zip

EDIT: You can now download the class from GitHub: https://github.com/Sjors/currentLocationStringForCurrentLanguage (Thank you Sjors!)

EDIT: Please read the comment by Alexey below, if you plan to use the Russian language!

EDIT: Thanks to Brad, the class is available for MonoTouch as well: https://github.com/ytn3rd/CurrentLocationStringForCurrentLanguage.

This entry was posted in iOS and tagged , , . Bookmark the permalink.

21 Responses to Localized “Current Location” string for iPhone apps

  1. Thanks for the list! Note that using ‘Current Location’ doesn’t work on the iPad so you may need to fall back to option #1. Works great as-is on iPhone 4.3 though!

  2. martip says:

    @William: oh… you’re right :-(

  3. Matthew Frederick says:

    Excellent work!

    Strange that it won’t work on the iPad, though, since it’s a standard Google Maps reference, not something specific to the iPhone. I guess the iPad Maps app must eat it.

  4. martip says:

    Thank you @Matthew. The iPad handles Google Maps in a different way; I couldn’t manage to find a way to make this trick work on the iPad… If you find anything interesting, please let us know.

  5. Carolina says:

    Hi, we developed a little app that works with a qr code, when it reads the code it opens up to a mobile site on your smartphone and it has a image and a title of a location (restaurant). We added a “get directions” button that will automatically open your maps app or browser on your phone. We are having issues with some phones including iphones that wont populate or have a “current location” and then will only show where the location is. You need to then add your address (if your driving you don’t know exactly where you are) and click on get directions.

    Is there a way to add your “current location” to the script so you dont have to waist time, and that it works for all smartphones?

    Appreciate your input.

    Carol

  6. martip says:

    Hi Carol.
    My class works only on the iPhone, it’s written in Objective-C… I can’t help you with different phones/languages, sorry.

  7. Eric says:

    To whoever that can help,

    I’ve been trying to work with the ‘LocalizedCurrentLocation.zip’ file but I’m kinda lost. I’m not sure how to ‘call’ the localized string and use the ‘example usage’. I apologize for asking such a newbie question.

    Please help me out here as I’m scratching my head over this matter. Anyone? Please~
    Thanks!^^

  8. martip says:

    @Eric, I think that the usage example is quite simple… If you can’t get it, I’m afraid that probably your knowledge of Objective-C/iOS programming needs some improvement.
    May I suggest you a link with useful resources for beginners? http://ios-blog.co.uk/resources/recommended-reading-ios/.

  9. Eric says:

    @martip, first of all I apologize for any inconvenience caused. I still very new in this matter and am self-studying currently. But I’d like to learn more about using ‘LocalizedCurrentLocation.zip’ as soon as possible. It’s OK if it’s too troublesome, I’ll keep on trying my luck. I appreciate your time and link for supporting me.
    Thanks!^^

  10. Thanks, this is very useful. I needed to make it Automated Reference Counting compatible. That turned about to be just a matter of deleting one line of code, but I decided to put it on Github while I was at it:

    https://github.com/Sjors/currentLocationStringForCurrentLanguage

  11. martip says:

    @Sjors Thank you, a public repository is definitely a better place to store this code!

  12. Alexey says:

    Thanks for your category.
    Unfortunately at least Russian version doesnt work properly on iOS 5.0.1.
    Apple has changed the phrase from “Текущее размещение” to “Текущая геопозиция” (Current location => Current geoposition)

  13. martip says:

    @Alexey Thank you for reporting. I’m not changing the code because I don’t know exactly when the changed happened (and I don’t have time to investigate further), but your comment is certainly a good warning for interested coders.

  14. Evan GIfford says:

    Exactly what I needed, thanks for this!

  15. Nino says:

    Awesome, thanks a lot!!!
    Lost one hour looking why Maps App doesn’t start navigation from “Current Location” on my german iPhone! *sigh*

  16. martip says:

    Thank you @Brad! I added the GitHub URL to the original post.

  17. Maurizio says:

    String with accented characters are wrong, for example “Ubicación actual” should be “Ubicaci%C3%B3 actual”

  18. martip says:

    Hi Maurizio, I’m not sure that I get your point. I tried to switch my iPhone to Spanish and everything works as expected…

  19. Maurizio says:

    It was my fault, I did not use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding.

    Thanks

  20. rexshi says:

    In my iOS 5.1, “zh-CN” is no longer supported. It is replaced with ‘zh-Hans’.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>