Auto-detect language from browser accept language

Author Message

Sébastien Hordeaux

Wednesday 22 August 2007 6:40:18 am

Hello,

How can eZ publish display localized information based on user's AcceptLanguage settings in the browser?

If it is not possible, is there a way to redirect user to a different site access based on this?

Thanks you by advance for any hint :)

Lazaro Ferreira

Wednesday 22 August 2007 9:36:24 am

Hi Sebastien

You can create a template operator to detect the HTTP_ACCEPT_LANGUAGE, sent by the browser, the following php code get the information :

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

 

Then you can program your pagelayout.tpl to redirect based on the operator output, you can create your own redirect operator, or use the one at the contributions

Regards

Lazaro
http://www.mzbusiness.com

Andrey Astakhov

Wednesday 22 August 2007 12:14:21 pm

Hello, Sébastien

I implemented it so:

1. Update your httpd.conf (or .htaccess)

        RewriteCond %{HTTP:Accept-Language} ^ru.* [NC]
        RewriteRule (.*) - [E=SITEACCESS:siteaccess_ru]

        RewriteCond %{HTTP:Accept-Language} ^en.* [NC]
        RewriteRule (.*) - [E=SITEACCESS:siteaccess_en]

"ru" and "en" are languages of my site.
It adds appropriate server variable depending of user Accept-Language.

2. Update your settings/override/site.ini.append

MatchOrder=servervar;host
ServerVariableName=SITEACCESS
HostMatchMapItems[]=www.yoursite.com;siteaccess_default

Now users with Accept-Language = ru* see siteaccess_ru siteaccess, users with Accept-Language = en* see siteaccess_en siteaccess, any other users see siteaccess_default.

You could also change rewriterules to minimize set of rules and with site.ini settings to create other siteaccess matching.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.