0

I have a PHP Language autodetect feature that loads a different page according to the browser's language.

The code works fine when I try to bypass the browser's language (using a browser extension), but it won't work properly when I try to append a country.

This is my code, where I want to load a version for Canada (CA) and a different one for France (FR):

<?php

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "en":
    include("translations/en.php");
    break;
case "fr_CA":
    include("translations/fr-CA.php");
    break;
case "fr_FR":
    include("translations/fr-FR.php");
    break;
default:
    include("translations/en.php");
    break;
}

?>

Tried also adding and if within case "fr": introducing the locales, but it won't work either.

nicozica
  • 403
  • 6
  • 16
  • 3
    You're defining `$lang` to be a substring of only the first two characters. If you want to accept country codes as well, you'll need more characters than that. – rickdenhaan Apr 27 '18 at 18:24
  • 1
    A better approach in my opinion would be to grab the language from the header `Accept-Language`. You can retrieve it like this: `$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);` Then you can easily set conditions. – MrJack Mcfreder Apr 27 '18 at 18:45
  • Both solutions work great locally. But when I try to host them on Heroku, it will return a blank page. This wasn't happening when I only used the language. Any thoughts? – nicozica Apr 27 '18 at 19:29
  • If you [turn on error reporting](https://stackoverflow.com/a/21429652/1941241), do you get any helpful error messages? – rickdenhaan Apr 28 '18 at 14:16
  • I believe this is an issue only experienced in Heroku. There are no issues at all using a different hosting. – nicozica Apr 30 '18 at 18:36

0 Answers0