1

I'm trying to make a multilingual page, my intention is to redirect the user to the specific page depend on his language (in case the cookie is not setup if the cookie exists because the user selected previously the language I'll ignore this block) doing a dummy file to capture the language through $_SERVER['HTTP_ACCEPT_LANGUAGE']:

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

In theory this should return the actual accept language header for the request. But doing tests in different computers with different languages EN, IT, ES...(I've checked they are sending they're languages as accept_language)

I've found that if I first access with EN no matter any other request I made in other languages that will keep EN. Same if the first request is with IT or ES. This doesn't refresh after a while or after I've modify the dummy file and upload it again.

As far as I can see is behaving as an static variable but according with PHP doc says that it should return the "actual" and all example I'm seeing to do this multilingual suggest to do it in this way.

Actually after hours testing I've decided to create a rule in the .htaccess to redirect to the proper site depends on this value and it works:

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteRule .* /en/index.php [L]
RewriteCond %{HTTP:Accept-Language} (it) [NC]
RewriteRule .* /it/index.php [L]
RewriteCond %{HTTP:Accept-Language} (es) [NC]
RewriteRule .* /es/index.php [L]

so something is not making it work in PHP.

  • Do you have any idea of what is going on?
  • Would you recommend to keep the .htaccess redirection or instead to detect this first language by php?

Kind regards,

Pablo

  • The problem most likely is somewhere outside PHP. `$_SERVER['HTTP_*']` are populated on each request using headers which PHP gets from proxy (Apache/nginx/any other server). The way how you do i18n on your site strongly depends on tools you use. To my knowledge, all major frameworks (Symfony, Zend, Laravel) have that kind of functionality. – kozlice Aug 22 '16 at 10:21
  • You're a genius! I found the nginx was activated! once I've deactivated everything started to work! thanks!! @kozlice – Pablo Aldana Aug 22 '16 at 22:03

0 Answers0