3

In my rails app I want to use country-code, currency-code, ISO locale code to fetch some data from API. How can I get this information dynamically when user visit my site from anywhere?

I have used geocoder gem so by request.location I will get location's information and using this gem I can get country-code. Now I am not getting how can I get remaining information such as currency-code & ISO locale code?? Can anyone please help me or guide me??

I have seen this money gem but not sure it will provide me all these information.

Thanks in advance :)

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55

3 Answers3

7

I have tried @Prakash Murthy's answer. But there are many issue in this http://www.currency-iso.org/dam/downloads/table_a1.xml I found there is not proper name of all countries and some country has multiple currency_code which made me confused. But finally I found the solution by this single countries gem without creating any database.

Here is how I achieved the solution:

country_name = request.location.data['country_name'] # got country name
c = Country.find_country_by_name(country_name) # got currency details
currency_code = c.currency['code'] # got currency code

Sorry to answer my own question but I have posted here so in future if anyone stuck like me for the same issue then his/her time not wasted.

Community
  • 1
  • 1
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
  • 1
    Answering your own question is the right thing to do when you have the answer and it hasn't been posted. Thanks. – Richard_G Jun 06 '15 at 20:18
  • I don't recommend using the countries gem for this because it uses the currencies gem which has a lot of missing currency information. https://github.com/hexorx/currencies/pulls – Kris Khaira Feb 22 '16 at 10:52
2

I found a way that makes it really easy:

Add countries gem to Gemfile, then bundle install

def currency_for_country(currency_iso_code)

  ISO3166::Country.new(currency_iso_code).currency_code

end

Then:

currency_for_country('US')
=> "USD"

currency_for_country('AU')
=> "AUD"

This info is based off the countries gem readme

stevec
  • 41,291
  • 27
  • 223
  • 311
1

currency-code & ISO locale code are static data which change very rarely - if at all, and are best handled as static information within the system by storing them within the database tables. Might even be a good idea to provide a CRUD interface for managing these data.

One possible source for Currency code : http://www.currency-iso.org/en/home/tables/table-a1.html

List of All Locales and Their Short Codes? has details about getting the list of all locale codes.

Community
  • 1
  • 1
Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • While I agree that it changes rarely, I don't think it is a good idea to consider it *"static data"*. If your project is going to live 5+ years, it will be the case that you will get lots of inconsistencies. You need to periodically update the list. – ndnenkov Jan 17 '17 at 14:02
  • Thanks. That is indeed a valid point; I had not considered it. – Prakash Murthy Jan 17 '17 at 16:20