SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
-
Oct 28, 2006, 15:25 #1
- Join Date
- Feb 2006
- Location
- Hamburg, Germany
- Posts
- 593
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Detect user location / language in browser?
Hi,
I'm implementing a bilingual website in Rails and I wonder if there's any way to detect where exactly on this planet a user is actually looking at my site. I would like to provide the user with a version in his own language, based on where he's based. Is this feasible?
-
Oct 29, 2006, 05:19 #2
You can get their general area (i.e. country) by IP, but I wouldn't do automatic redirection. You don't know if someone in, say Austria, wants the English or German version of your site.
-
Oct 29, 2006, 16:32 #3
- Join Date
- Feb 2006
- Location
- Hamburg, Germany
- Posts
- 593
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm... I would assume that 99% of my site visitors in Austria want the German language version. The same for Germany and Switzerland.
If anybody from those countries enters my site, it should appear in German. Outside those countries, it should default to English. How can I detect their location/IP address and then serve the correct version?
-
Oct 29, 2006, 21:01 #4
To get the location from an IP address, you need to use a geolocation service, some of which are free, but most of which require some sort of subscription. MaxMind offers both, and they have a Ruby API. Personally, I wouldn't use that approach, however; for all my bilingual sites, I use a splash page that stores a cookie.
-
Oct 30, 2006, 05:45 #5
Note that the free geo IP services are usually pretty badly out of date.
You can also get a general idea of the user's language from the HTTP Accept-Language request header. At least in IE, this defaults to the operating system's settings if the user hasn't changed it in the browser (which I'm sure many people do not). I'm not sure if Firefox default to the current OS locale settings or not.
-
Oct 30, 2006, 09:05 #6
- Join Date
- Feb 2006
- Location
- Hamburg, Germany
- Posts
- 593
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hm, the spash page idea doesn't convince me because it may constitute just another barrier to site entry, don't you think? I would rather go for a solution where the user location is detected automatically, even if it's just to save the user one mouse click.
So how exactly can I detect the user language from the HTTP Accept-Language request header? I think detecting the user's system settings would be a pretty reasonable way to go...Last edited by PixelLover; Nov 21, 2006 at 07:19.
-
Oct 30, 2006, 16:48 #7
Splash pages for language selection are quite common here in Canada, but I agree they have their drawbacks.
As for the Accept-Language, you can retrieve it by using something like:
Code:request.env["HTTP_ACCEPT_LANGUAGE"][/[^,;]+/]
-
Oct 31, 2006, 04:17 #8
- Join Date
- Feb 2006
- Location
- Hamburg, Germany
- Posts
- 593
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I agree, splash pages are of course very useful in countries with more than one official language, like Canada or Switzerland...
Originally Posted by 33degrees
-
Oct 31, 2006, 10:43 #9
Generally, you'd implement the locale detection code as a before_filter in your application controller. What you do with the locale data afterwards depends on how you're implementing your multiple languages; I believe someone mentioned the Globalize plugin in another thread, you might want to check it out.
-
Nov 21, 2006, 17:34 #10
- Join Date
- Feb 2006
- Location
- Hamburg, Germany
- Posts
- 593
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
What I've got so far is this. Controller:
Code:def localize user_lang = request.env['HTTP_ACCEPT_LANGUAGE'] if user_lang == "de_DE" session[:lang] = "deutsch" else session[:lang] = "english" end end
Bookmarks