Getting browser country parameter in Ruby on Rails

C

CiriusMex

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

Joshua Ballanco

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
 
F

Florian Gilcher

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

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

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

CiriusMex

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top