0

I have some pages of my site that I translate to other languages to make things easier for the end user and I have been doing it based on the country they are in. I found out recently that I can do it based on their browsers set language which would be better for me, so I was wondering how I can detect the browser language, and define content based on the the detected browser language. Here is what I'm currently using:

$eng = (isset($_GET['country']) && (
    $_GET['country'] == 'AU' ||
    $_GET['country'] == 'CA' ||
    $_GET['country'] == 'UK' ||
    $_GET['country'] == 'US'));

if ($eng)
 {
$text1 = "Text 1 content goes here";
$text2 = "Text 2 content goes here";
$text3 = "Text 3 content goes here";
 }

Instead of using countries, how do I use browser languages? For example in Canada they speak english in some places and french in another so I would like to display the correct language which I think is done with ISO Language codes but im not completely sure

Joe Bobby
  • 2,803
  • 10
  • 40
  • 59
  • possible duplicate of [Detect Browser Language in PHP](http://stackoverflow.com/questions/3770513/detect-browser-language-in-php) – NoLiver92 Jan 23 '15 at 18:32

1 Answers1

0

This will detect the languages, you will then need to change the case statement for the languages you want to use. You can then change the include to show whatever content you want in the relevant language, the code below shows the example of different home pages.

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  switch ($lang){
    case "fr":
      //echo "PAGE FR";
      include("index_fr.php");
      break;
    case "it":
      //echo "PAGE IT";
      include("index_it.php");
      break;
    case "en":
      //echo "PAGE EN";
      include("index_en.php");
      break;        
    default:
      //echo "PAGE EN - Setting Default";
      include("index_en.php");
      break;
  }
?>

Source for browser language codes: Language Codes

NoLiver92
  • 882
  • 3
  • 15
  • 39
  • what do I do for languages when there is multiple types like for spanish? https://cloudup.com/cgD6Jc_JVjr the spanish in latin america and spain can be very different, so how do i deal with that? – Joe Bobby Jan 23 '15 at 18:47
  • these are the codes to look for: http://www.metamodpro.com/browser-language-codes – NoLiver92 Jan 23 '15 at 18:52
  • instead of multiple php files, is there a way to define this in 1 file? – Joe Bobby Jan 23 '15 at 18:55
  • Yes just replace the include line with the required code – NoLiver92 Jan 23 '15 at 18:58
  • and my last question, if say there is no index_it.php file to include, is there any way to default it to index_en.php? – Joe Bobby Jan 23 '15 at 19:01
  • If you dont include the case "it" (delete that section) then it will automatically fo the the default section where you can make it en. Have a look at switch statements. For example in my code if the browser returned "es" the default section would make it en as es doest exist – NoLiver92 Jan 23 '15 at 19:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69473/discussion-between-joe-bobby-and-noliver92). – Joe Bobby Jan 23 '15 at 19:13