Go Back   SitePoint Forums > Forum Index > Program Your Site > General Application Design and Theory
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jan 31, 2008, 12:56   #1
Pixelateur
SitePoint Evangelist
 
Pixelateur's Avatar
 
Join Date: Feb 2006
Location: Hamburg, Germany
Posts: 526
Question Best way to detect browser language?

Hi,

I have a website in two languages, English and German:

www.website.com/en/
www.website.com/de/

How can I make sure that each visitor is sent to the correct language version? I've been doing quite a bit of searching but can't decide between a javascript or php based solution.

I need a script that detects the user's browser language and then immediately renders the website in his/her language. However, the user should also be given the opportunity to switch to the other language should s/he wish to.

Any ideas on how to implement this?
Pixelateur is offline   Reply With Quote
Old Jan 31, 2008, 16:08   #2
Mittineague
Google search user
 
Mittineague's Avatar
 
Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 9,127
language

When a browser makes an HTTP request it sends headers. You could try using
$_SERVER["HTTP_ACCEPT_LANGUAGE"] and $_SERVER["HTTP_ACCEPT_CHARSET"])
Mittineague is offline   Reply With Quote
Old Jan 31, 2008, 17:22   #3
wwb_99
Community Advisor
SitePoint Award Recipient
 
wwb_99's Avatar
 
Join Date: May 2003
Location: Washington, DC
Posts: 9,134
Honestly, the best way is to:

1) pick a language which the majority of your users use and make this the default
2) put a clear indication of how to switch languages in the site navigation

Trying to auto-detect can get very annoying for people who are, say, visiting a foreign country and keep getting served pages in arabic they can't even begin to comprehend.
wwb_99 is offline   Reply With Quote
Old Feb 1, 2008, 04:44   #4
Pixelateur
SitePoint Evangelist
 
Pixelateur's Avatar
 
Join Date: Feb 2006
Location: Hamburg, Germany
Posts: 526
Hi wwb_99,

You are right of course. On every page there will be a switch language button in the top right corner, so people can easily switch to their preferred language at any time. However, the very first time someone visits my site, it should be served in his/her system language. I think this makes total sense, not least because the chances of getting it right are pretty high actually. So what would be the best way to do it?
Pixelateur is offline   Reply With Quote
Old Feb 1, 2008, 05:25   #5
kyberfabrikken
SitePoint Mentor
 
kyberfabrikken's Avatar
 
Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,819
As Mittineague said, the HTTP-request may contain a header "Accept-Language", which specifies a list of languages, in order of preference. Set a cookie, when you have decided, so that the next time the user comes back, you use the cookie instead. That way, people have to switch language once.
kyberfabrikken is offline   Reply With Quote
Old Feb 1, 2008, 05:45   #6
AutisticCuckoo
SitePoint Wizard
 
Join Date: Nov 2004
Location: Åsnorrbodarna
Posts: 11,581
Quote:
Originally Posted by kyberfabrikken View Post
As Mittineague said, the HTTP-request may contain a header "Accept-Language", which specifies a list of languages, in order of preference.
Just to clarify: the languages do not necessarily occur in the preferred order. Rather, each language may have an associated quality value ('q' value) between 0.000 and 1.000. An omitted q value is the same as q=1.000.

A sample header can look like this,
Code:
Accept-Language: en-GB, sv;q=0.7, en;q=0.9
The preferred order is,
  1. en-GB (q=1.0)
  2. en (q=0.9)
  3. sv (q=0.7)
AutisticCuckoo is offline   Reply With Quote
Old Feb 21, 2008, 09:51   #7
mr.vain
SitePoint Enthusiast
 
Join Date: May 2007
Posts: 25
I've implemented this in the following way:


1) Detect the BrowserLanguages
2) Check that this Languages exists in your system
3a) Set this Language to the user session
3b) Set a Language via URL (forced)
4) Include the translated file

Source:
http://svn.gna.org/viewcvs/clansuite...49&view=markup

Feedback appreciated. Regards, v
mr.vain is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 18:28.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved