Forum Moderators: phranque

Message Too Old, No Replies

Best practices for determining a visitor's language?

         

JAB Creations

11:10 am on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is or are the best method(s) for detecting or determining a visitor's language?

- John

encyclo

11:32 am on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The
Accept-Language
header sent by the browser. It should offer a choice of preferred languages with a quotient of preference. For example, mine reads:

en-ca,en;q=0.8,fr-ca;q=0.5,fr;q=0.3

Other options include checking the language in the user-agent header, but that's less reliable. I may user a browser with the interface in one language, but have a preference for another. Geo-targetting is possible too, but that doesn't indicate the preferred language, just the language of the region where the person is connecting from.

You should always offer the possibility of switching languages manually - in that case, set a cookie for the chosen language and serve that up as a default for that user.

JAB Creations

5:40 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, I'm going to rely on this primarily and cross-check via the user agent. Simply put all I'm going to do is provide flags of detected/determined languages and it should give visitors an easy visual queue that a translation is available. Here is the method to determine the visitor's language via their browser header in PHP...

- John

<?php echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];?>

[edited by: JAB_Creations at 5:41 pm (utc) on Nov. 16, 2007]

phranque

2:05 am on Nov 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i concur with the above.
we typically go with the following priorities:
- user profile/session/cookie/whatever (last user-selected language preference)
- satisfy high-q language/locale specifications in Accept-Language header
- default or "native" language for the site.

in most cases we allow for a url or form parameter to override all.
(e.g., "...&User:LANGUAGE=sa&...")

if you were using perl the HTTP::Negotiate module [search.cpan.org] would be helpful - perhaps php has something similar...

anewstone

2:07 am on Nov 20, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Why not use Google Analytics?

JAB Creations

2:12 am on Nov 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've updated my homepage to determine the supported languages via the browser's headers. When a language is supported I have PHP echo a highlight class so the flag that corresponds with the language stands out more.

- John

phranque

2:28 am on Nov 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], anewstone!

Why not use Google Analytics?

it's a little late to use google analytics if you are trying to serve content based on client-side settings.