Convert country name to country code


#1

Ive got the TZInfo library installed, and I am able to convert a country
code into country name, but I want to do it the other way around. Is it
possible?

Thanks,
Petr


#2

On 8 Mrz., 04:39, Petr J. removed_email_address@domain.invalid wrote:

Ive got the TZInfo library installed, and I am able to convert a country
code into country name, but I want to do it the other way around. Is it
possible?

Thanks,
Petr


Posted viahttp://www.ruby-forum.com/.

I just took a small glimpse on the API doc and think it’s not possible
with tzlib, cause they have a countries hash in the Form: {code =>
name}

add the following method to country.rb:

def self.get_code(identifier)
  load_index
  Indexes::Countries.countries.keys.find{|code| a[code] ==

identifier}
end

Then you should be able to do Country.get_code(“Germany”).
Good luck :slight_smile:


#3

I’ve written a library called NormalizeCountry that can do this on a
wide array of formats: https://github.com/sshaw/normalize_country


#4

On 8 Mrz., 09:38, “rretzbach” removed_email_address@domain.invalid wrote:

  Indexes::Countries.countries.keys.find{|code| a[code] ==

identifier}

Sry, too fast too furious!
a should be the same hash:

Indexes::Countries.countries.keys.find{|code|
Indexes::Countries.countries[code] == identifier}

Feel free to improve it :slight_smile:


#5

Working for me, i am building on country codes information website. This helped me and solved my problem for my drop down select. I am now able to convert country two digit iso to country dialing code, example, US = 1 and UK = 44