Michael Giovanni Pumo
Michael Giovanni Pumo

Reputation: 14774

GeoIP2 - How can I get hold of the language code?

I'm using GeoIP2 from Maxmind and would like to know how I can get hold of the language code from the JSON response. It appears this is not present from their API and I can't locate information on this.

For example, I would like to get:

en_GB

But, GeoIP2 seems to only give me a country code: GB.

Typical response:

{
  "country": {
    "iso_code": "GB",
    "names": {
      "pt-BR": "Reino Unido",
      "es": "Reino Unido",
      "ru": "Великобритания",
      "en": "United Kingdom",
      "zh-CN": "英国",
      "fr": "Royaume-Uni",
      "de": "Vereinigtes Königreich",
      "ja": "イギリス"
    },
    "geoname_id": 2635167
  },
  "city": {
    "names": {
      "pt-BR": "Londres",
      "es": "Londres",
      "ru": "Лондон",
      "en": "London",
      "zh-CN": "伦敦",
      "fr": "Londres",
      "de": "London",
      "ja": "ロンドン"
    },
    "geoname_id": 2643743
  },
  "location": {
    "longitude": -0.0931,
    "latitude": 51.5142,
    "time_zone": "Europe/London"
  },
  "subdivisions": [
    {
      "iso_code": "ENG",
      "names": {
        "pt-BR": "Inglaterra",
        "es": "Inglaterra",
        "ru": "Англия",
        "en": "England",
        "zh-CN": "英格兰",
        "fr": "Angleterre",
        "de": "England",
        "ja": "イングランド"
      },
      "geoname_id": 6269131
    }
  ],
  "continent": {
    "names": {
      "pt-BR": "Europa",
      "es": "Europa",
      "ru": "Европа",
      "en": "Europe",
      "zh-CN": "欧洲",
      "fr": "Europe",
      "de": "Europa",
      "ja": "ヨーロッパ"
    },
    "geoname_id": 6255148,
    "code": "EU"
  },
  "traits": {
    "autonomous_system_number": 8220,
    "ip_address": "80.169.93.174",
    "organization": "COLT Technology Services Group Limited",
    "isp": "COLT Technology Services Group Limited",
    "autonomous_system_organization": "COLT Technology Services Group Limited"
  },
  "registered_country": {
    "iso_code": "GB",
    "names": {
      "pt-BR": "Reino Unido",
      "es": "Reino Unido",
      "ru": "Великобритания",
      "en": "United Kingdom",
      "zh-CN": "英国",
      "fr": "Royaume-Uni",
      "de": "Vereinigtes Königreich",
      "ja": "イギリス"
    },
    "geoname_id": 2635167
  },
  "postal": {},
  "represented_country": {
    "names": {}
  }
}

http://dev.maxmind.com/geoip/geoip2/javascript/

Now, one solution would be to manually concatenate the navigator.language code to the country code from GeoIP2, but that's a dirty hack at best I'd like to avoid.

All "iso_code" seems to be is the country code. I need the language too.

Upvotes: 0

Views: 637

Answers (1)

Dave Rolsky
Dave Rolsky

Reputation: 4532

there's no way to get this information from the GeoIP2 database or web service. It would pretty difficult to know what language is appropriate for a given IP address.

Many countries use more than one language. Given the country code, you can look up the languages given an appropriate data source, though I don't know of a good JS library for this off-hand.

Upvotes: 3

Related Questions