# Getting browser country parameter in Ruby on Rails

**URL:** <https://www.ruby-forum.com/t/getting-browser-country-parameter-in-ruby-on-rails/154130>\
**Category:** Ruby\
**Created:** [November 25, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/getting-browser-country-parameter-in-ruby-on-rails/154130 "2008-11-25T17:51:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [November 25, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/getting-browser-country-parameter-in-ruby-on-rails/154130/1 "2008-11-25T17:51:05Z")

</div>

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 [ASP.net](http://ASP.net)) 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,  
Olivier.

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [November 25, 2008, 9:39pm UTC](https://www.ruby-forum.com/t/getting-browser-country-parameter-in-ruby-on-rails/154130/2 "2008-11-25T21:39:40Z")

</div>

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:

request.env[‘HTTP\_USER\_AGENT’][/((._;)+._(\S\*?))/,2][0…1]

Cheers,

Josh

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [November 25, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/getting-browser-country-parameter-in-ruby-on-rails/154130/3 "2008-11-25T22:34:32Z")

</div>

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  
company  
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.

Regards,  
Skade

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [November 26, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/getting-browser-country-parameter-in-ruby-on-rails/154130/4 "2008-11-26T17:35:47Z")

</div>

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!
