1

I'm a designer with little coding knowledge outside of html/css. Reading on the 'net I find many references to some php using http_accept_language but cannot find a step-by-step "do this, do that" explanation and consequently I don't know what to do with / where to put the code that people present for the job.

I'm developing a site with an English main home page. There, users are presented with a choice of two branches. One branch will be only in English and the other in English plus 3 Scandinavian languages. The non-English pages are pre-translated, not created on the fly by Google.

I want that after arriving at the main home, a user who opts to go into the 4-language section will be initially served a page in the language of their browser (on all pages there will be always be a menu to manually select another language, if preferred).

Is this a job for php? If so I'd be grateful for some code and help in how to use it.

peterh
  • 37
  • 1
  • 7
  • @NickR, the question asks for a PHP way. Besides, you *cannot* do it client-side, as the accepted answer to the cited question says. – Jukka K. Korpela Jan 17 '14 at 15:12
  • “Browser language” is a vague concept and need not match the language that is most preferred according to the `Accept-Language` header. You may need to consider what you wish to use as the basis of selecting the language of the initial page. – Jukka K. Korpela Jan 17 '14 at 15:15
  • Thanks Jukka. If there's a better way that isn't covered in my original question I'd be really pleased to hear of it. – peterh Jan 17 '14 at 15:40
  • @Jukka I have been reading further and it seems like a reasonable strategy to use accept_language as returned by the browser. I take that to mean the browser's default language if it has never been changed (probably the most likely in the vast majority of cases), and then the user's chosen preference if they have selected it in their browser prefs. – peterh Jan 17 '14 at 20:19

2 Answers2

3

You can use HTTP_ACCEPT_LANGUAGE header. Save supported languages to array and then loop the header using foreach.

$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
    if(in_array($lang, $languages)) {
        header("Location: $lang.php"); // i.e. fi.php or se.php
            break;
    }
}

HTTP_ACCEPT_LANGUAGE contains something like this:

Accept-Language: en-gb,en;q=0.5

As you can see, there is multiple languages, en-gb and en. That's why it's clever to loop the header. If you prefer functions, here's one:

function get_user_lang() {
    $languages = array('en', 'fi', 'sv', 'no');
    $header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    foreach($header as $lang) {
        if(in_array($lang, $languages)) {
            return $lang; // i.e. fi.php or se.php
                break;
        }
    }
}
echo 'Your language is ' . get_user_lang();

Step-by-step guide:

  1. Create new files for each language. Name them like this: "fi.php" or "se.php".

  2. Place the first code part to very top of your home page. That file must include .php ending, so it must be php file. If you don't understand, that should be where to place it:

    <?php 
     $languages = array('en', 'fi', 'sv', 'no');
     $header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
     foreach($header as $lang) {
        if(in_array($lang, $languages)) {
        header("Location: $lang.php"); // i.e. fi.php or se.php
            break;
        }
     }
    ?>
    <!DOCTYPE html>
    <html>
      <head>
        <title>title</title>
      </head>
      <body>
        contents
      </body>
    </html> 
    
  3. Navigate to your home page in your browser. If your browser's language is english, it'll redirect to "en.php", if swedish; "se.php" and so on.

  4. You can see all language codes from this link, swedish is "sv".

aksu
  • 5,221
  • 5
  • 24
  • 39
  • Thanks for the code. I'd like to try it but, as I mention above, don't know how/where to use it. – peterh Jan 17 '14 at 15:42
  • @peterh put the code on top your home page inside `` tags. It'll redirect, if it regocnizes one of the supported languages. – aksu Jan 17 '14 at 15:57
  • Thanks @aksu but I really do not know how to apply the php. Your code above is in several parts - how do they relate? And how should I name the language pages so that they relate to what php returns? And by the way, should swedish be 'se' or 'sv'? – peterh Jan 17 '14 at 20:11
  • I added step-by-step guide to my post, follow it. Php returns one of the language codes, you can see [this link](http://msdn.microsoft.com/en-us/library/ms533052%28vs.85%29.aspx) for language codes. You can name the files by that list. – aksu Jan 18 '14 at 07:33
  • 1000 thank-you's. Works exactly how I need. Previously I was putting your php in the Head instead of before Doctype – peterh Jan 18 '14 at 17:55
  • 1
    I hope your step-by-step will help others - I couldn't find anything so plainly described out there in the web. – peterh Jan 18 '14 at 18:04
0

If you have the INTL extension you can use Locale::acceptFromHttp().

Here's the example from the PHP documentation.

$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale;

$locale will be something like "en_US". If you only want the language and not the country, use something like this:

$locale = explode('_', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
echo $locale[0];
mcrumley
  • 5,682
  • 3
  • 25
  • 33
  • Thanks @mcrumley, but I fear that installing the INTL extension in my mamp environment, then on the staging server and on the final server will be beyond me – peterh Jan 17 '14 at 20:13