Stefano Giacone
Stefano Giacone

Reputation: 2153

How to translate city names in different languages

I have a mobile app (both iOS and Android) and I need to translate cities name in the language of the user. I can do the translation on mobile device or on my server (running php).

As of now I managed to translate country names, here the java code that translate all possible countries in all possible languages:

import java.util.Locale;

public class ListCountry {

public static void main(String[] args) {

ListCountry obj = new ListCountry();

obj.getListOfCountries();

}

public void getListOfCountries() {

String[] locales = Locale.getISOCountries();

for (String countryCode : locales) {

    Locale obj = new Locale("", countryCode);
    String[] lingue = Locale.getISOLanguages();

    for (String languageCode : lingue) {

        System.out.println("Country Code = " + obj.getCountry() 
        + ", Country Name = " + obj.getDisplayCountry(new Locale(languageCode)) + ", language = " + (new Locale(languageCode)).getDisplayLanguage());
    }
 }

}

}

How can I do a similar thing but with city names? I know CLDR and ICU but I really can't figure out how to do it (or if it's even possible). If there is a nice object oriented library out there it'll be better than parsing CLDR XMLs or other source.

I prefer to do it locally (on my server or even on mobile app) instead of calling Google API, example:

http://maps.googleapis.com/maps/api/geocode/json?address=turin&language=ES
http://maps.googleapis.com/maps/api/geocode/json?address=turin&language=IT
http://maps.googleapis.com/maps/api/geocode/json?address=turin&language=EN

(question is: I guess google DB of cities name is public, where is it? is it nicely wrapped in some user friendly cross-platform library?)

Thanks for your help

Upvotes: 3

Views: 10975

Answers (1)

itinance
itinance

Reputation: 12398

I guess you're looking for a file containing all cities and its translations instead of fetch them once per city?

If so, www.geonames.org has geo-data of different types (countries, adminzones, cities) in multiple languages. Next to their API call you can also download their files directly and parse it by yourself:

At the following URL, you'll find 3 Zip-Files prefixed by "alternativeNames" http://download.geonames.org/export/dump/

They contain - hopefully - the necessary data.

Upvotes: 7

Related Questions