Getting browser country parameter in Ruby on Rails

Hi folks ^^

Here is my problem. I have a page on my RoR site which display prices.
The idea is to display those prices in the currency corresponding to
the user country. I already have a table in my database with the
currency in function of the country and I implemented the convertion
module (using Yahoo Finance plugin, works really great!).
My problem is how do I do to access user’s browser parameters in order
to get the country parameter?
I know it can be done in the most part of the languages (such as Java
and so I hope it can be done with RoR too…Checked on google
and didn’t find anything interesting on the subject.

Thanks in advance for the help.

Have a good day,

Best bet is to put the user agent string through a RegExp. The
following has worked for me and generates a two-letter country code:




If you want to go down that road:

request.env[‘Accept-Language’] #=> en-us,en;q=0.5

Gives you a list of all the languages the client prefers in order of
preference. q stands for quality… look at the HTTP-Specs for this.

But this as well as the user agent is widely inaccurate. For example,
my browser sends en-us because i prefer my browser in english and
have thus downloaded firefox with default setting for english. In
reality, my location is either France or Germany. The same holds for
networks that have a english standard installation worldwide and so
on, so forth…

If you really want to find out the users country, have a look into a
geo-ip library. User agents lie.


Thanks for the help, that’s great, I’m gonna check it right now ^^

Florian thanks for the tip, actually your right the browser language
isn’t a good source for the user language, I knew that, but finally
it’s not that important for my purpose.

Thanks again!