Forum: Ruby Getting browser country parameter in Ruby on Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
CiriusMex (Guest)
on 2008-11-25 18:51
(Received via mailing list)
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) 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.
Joshua B. (Guest)
on 2008-11-25 22:39
(Received via mailing list)
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
Florian G. (Guest)
on 2008-11-25 23:34
(Received via mailing list)
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
CiriusMex (Guest)
on 2008-11-26 18:35
(Received via mailing list)
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!
This topic is locked and can not be replied to.