Language browser detection

104 views
Skip to first unread message

gmcms

unread,
Jun 16, 2018, 11:26:39 PM6/16/18
to Fat-Free Framework
Hi all.
When a browser is sending something like 'fr-fr', F3 3.6 does not understand that it's 'fr' and translate it as 'en'. It looks like it was working in some previous F3 releases...
Any best practices to know?
Thanks
Regards

ikkez

unread,
Jun 17, 2018, 3:28:08 AM6/17/18
to Fat-Free Framework
The code that's handling this wasn't touched for a while, so it shouldn't have changed in behaviour.
What's sent in the header? "Accept-Language: fr,fr-FR;q=0.7,en;q=0.3" ??

gmcms

unread,
Jun 17, 2018, 3:44:39 AM6/17/18
to f3-fra...@googlegroups.com
It’s 'fr-fr' only on Safari MAC, but this is the same issue with 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3' on Firefox Windows.

Setup file include:
LOCALES=app/dict/
FALLBACK=fr

Dictionaries are:
en.ini
fr.ini

F3 LANGUAGE always return 'en, fr', so en.ini is used rather than fr.ini

xfra35

unread,
Jun 19, 2018, 4:47:42 PM6/19/18
to Fat-Free Framework
Can you add the following code at the top of your index.php, just after instantiating the framework:

echo $_SERVER['HTTP_ACCEPT_LANGUAGE'],' >> ',$f3->language($_SERVER['HTTP_ACCEPT_LANGUAGE']);die();

and share the output with us?

If the browser is sending fr-fr, the expected output should be:
fr-fr >> fr-FR,fr,en

gmcms

unread,
Jun 19, 2018, 5:21:42 PM6/19/18
to f3-fra...@googlegroups.com

The test with a Windows 7 / Firefox browser: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3 >> fr,fr-FR,en-US,en

The test with MAC Safari: fr-fr >> fr-FR,fr

But one more information: Displaying LANGUAGE variable, it contains 'en,fr' and without FALLBACK defined it contains only 'en'

Thanks
Regards

xfra35

unread,
Jun 20, 2018, 3:02:07 AM6/20/18
to Fat-Free Framework
The core $f3->language() method works properly, but it looks as if the Accept-Language header is not found.

Which 3.6 patch version are you running exactly? 3.6.0 is nearly 2 years-old and I can remember there were a few optimizations made at that time to the part extracting the client headers.

gmcms

unread,
Jun 20, 2018, 3:03:23 AM6/20/18
to Fat-Free Framework

I'm using the last 3.6.4

xfra35

unread,
Jun 20, 2018, 5:25:17 AM6/20/18
to Fat-Free Framework
Can you dump the value of $headers at that line: https://github.com/bcosca/fatfree-core/blob/master/base.php#L2277 ?

It should contain 'Accept-Language' = 'fr-fr' on your Mac Safari, but I suspect it doesn't.

gmcms

unread,
Jun 20, 2018, 3:17:04 PM6/20/18
to f3-fra...@googlegroups.com
It does not. The test has been done with Firefox on Windows.

array(3) { ["User-Agent"]=> &string(72) "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" ["Host"]=> &string(13) "domain.ext" ["Remote-Ip"]=> &string(13) "v.x.y.z" }

xfra35

unread,
Jun 20, 2018, 4:09:53 PM6/20/18
to Fat-Free Framework
Which HTTP server are you using? Can you dump the result of getallheaders() ?

gmcms

unread,
Jun 20, 2018, 9:22:01 PM6/20/18
to f3-fra...@googlegroups.com
The server is provided by a hoster. It's a Linux VM with Apache and PHP 7.0.

getallheaders() done on MAC with PHP 7.0 on server:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Upgrade-Insecure-Requests: 1 
Cookie: clusterD=R1663458276; clusterBAK=R1564895428; PHPSESSID=00c9266fd3ef2484b42024783d9d7946 
Host: domain.ext 
Remote-Ip: v.x.y.z


For your information, getallheaders() done on MAC with PHP 7.2 on server (F3 is working):
X-Cdn-Any-Ip: v'.x'.y'.z'
Remote-Port: 55997 
Forwarded: for=v.x.y.z; proto=http; host=domain.ext 
X-Remote-Proto: http 
X-Remote-Ip: v.x.y.z
X-Remote-Port: 55997 
X-Forwarded-Proto: http 
X-Forwarded-Port: 80 
X-Forwarded-For: v.x.y.z 
Accept-Encoding: gzip, deflate 
Accept-Language: fr-fr 
Referer: http://domain.ext/login 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Upgrade-Insecure-Requests: 1 
Cookie: clusterD=R1663458276; clusterBAK=R1564895428; PHPSESSID=00c9266fd3ef2484b42024783d9d7946 
Host: domain.ext 
Remote-Ip: v.x.y.z

xfra35

unread,
Jun 21, 2018, 6:03:26 AM6/21/18
to f3-fra...@googlegroups.com
So, for some reason, getallheaders() doesn't return all the client headers in the first case (PHP 7.0), although the $_SERVER variable contains all of them, especially $_SERVER['HTTP_ACCEPT_LANGUAGE'], am I right?

gmcms

unread,
Jun 21, 2018, 3:51:01 PM6/21/18
to Fat-Free Framework
Yes it's right.
In PHP 5.6, 7.0 and 7.2 $_SERVER['HTTP_ACCEPT_LANGUAGE']='fr_fr'.

ikkez

unread,
Jun 21, 2018, 6:23:18 PM6/21/18
to Fat-Free Framework
I'm not sure but could it be that getallheaders() is not always giving us all the headers that are present in $_SERVER ?

gmcms

unread,
Jun 22, 2018, 1:23:35 AM6/22/18
to Fat-Free Framework
I don't know, but I found that in a forum:

For some reason, $_SERVER does not always contain all the headers.I found this while testing the reverse proxy stuff, I did not bother to start looking into why.
The code simply adds to $_SERVER any from the "get all headers" that it finds are missing.

ved

unread,
Jun 22, 2018, 6:06:42 AM6/22/18
to Fat-Free Framework
It is possible (although strange imho) that your hosting provider has some kind of load balancing/reverse proxy setup that strips some headers before passing the request to the app server.

F3 has a fallback mechanism to get the headers if getallheaders() doesn't exist (for example, if using php-fpm instead of mod_php or php-cgi) but if, for some reason, getallheaders exists but doesn't return all the regular headers, I guess that could probably be causing these issues.

Also, are you absolutely sure that your host is using Apache? Can you find out from your host's support if they employ any sort of system that may be stripping the accept-language header?

Could you create a simple test.php on your host with:

<pre>
<?php
if(function_exists('getallheaders')) {
        echo
'getallheaders:<br>';
        var_dump
(getallheaders());
}
echo
'server:<br>';
var_dump
($_SERVER);
echo
'</pre>';
phpinfo
();

And then share the link to that file with us? (or at least let us know what hosting company you're using)

gmcms

unread,
Jun 22, 2018, 5:20:02 PM6/22/18
to Fat-Free Framework
The hosting company is OVH and this is the "Performance" LAMP.
You can download a PDF file including the result of your script here: http://dl.free.fr/m70A9lFp7
Thanks

xfra35

unread,
Jun 24, 2018, 12:33:33 PM6/24/18
to Fat-Free Framework
The file you've provided doesn't reflect what you've written before:
  • getallheaders() does return Accept-Language
  • the Accept-Language header is not set to fr-fr

gmcms

unread,
Jun 24, 2018, 12:38:05 PM6/24/18
to Fat-Free Framework
To work fine, PHP has been migrated to 7.2 and the test has been made from Windows Firefox. As you can see in the file: ["Accept-Language"]=> string(35) "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"

With 7.2 F3 is working as expected.
Thanks
Reply all
Reply to author
Forward
0 new messages