SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist Pixelateur's Avatar
    Join Date
    Feb 2006
    Location
    Hamburg, Germany
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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.

  3. #3
    SitePoint Evangelist Pixelateur's Avatar
    Join Date
    Feb 2006
    Location
    Hamburg, Germany
    Posts
    593
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    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?

  4. #4
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  6. #6
    SitePoint Evangelist Pixelateur's Avatar
    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.

  7. #7
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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"][/[^,;]+/]

  8. #8
    SitePoint Evangelist Pixelateur's Avatar
    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...

    Quote Originally Posted by 33degrees
    request.env["HTTP_ACCEPT_LANGUAGE"][/[^,;]+/]
    But where exactly do I need to put this bit of code? I assume it will yield a variable like "en-US" or something similar, that I can use in my controller?

  9. #9
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Evangelist Pixelateur's Avatar
    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
    I'm am pretty sure this is not going to work though, because this HTTP_ACCEPT_LANGUAGE thing will contain more than just "de_DE", right? And how about those other variations of German such as de_AT and de_CH (for Austria and Switzerland respectively). Can anybody please help me out here. Cheers!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •