0

I have been searching for a while, but can't seem to find the correct way to achieve this:

For creating a multilanguage(2) website, I was thinking of using the following folder structure:

DOMAIN
    www.domain.com/index.php
    NL-folder
        www.domain.com/nl/index.php (alternative home.php?)
        www.domain.com/nl/pagina2.php
        ...
    EN-folder
        www.domain.com/en/index.php (alternative home.php?)
        www.domain.com/nl/page2.php
        ...

I have read that some people find the www.domain.com/index.php being a language select page annoying.

Therefore I was thinking this page should show the same content as the index/home page in the preferred language. I have heard this can be done by using the headerfield Accept-Language. Alternativly, I should show the content of www.domain.com/nl/index.php or redirect there(To be able to do language-dependent queries I feel like I have to redirect www.domain.com/index.php to a language specific page, so that the language can be selected?). Ofcourse the user would be able to change language later...

I am completely stuck on how to correctly achieve this.

The only thing I might come up with is redirecting. With the code found on http://www.thefutureoftheweb.com/blog/use-accept-language-header this would become:

$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);

    if (count($lang_parse[1])) {
        // create a list like "en" => 0.8
        $langs = array_combine($lang_parse[1], $lang_parse[4]);

        // set default to 1 for any without q factor
        foreach ($langs as $lang => $val) {
            if ($val === '') $langs[$lang] = 1;
        }

        // sort list based on value 
        arsort($langs, SORT_NUMERIC);
    }
}

// look through sorted list and use first one that matches our languages
foreach ($langs as $lang => $val) {
    if (strpos($lang, 'nl') === 0) {
        header("Location: http://domain.com/nl/index.php");
    } else if (strpos($lang, 'en') === 0) {
        header("Location: http://domain.com/en/index.php");
    } 
}

Is my example ok, or should I do it differently?

PSZ_Code
  • 1,015
  • 10
  • 29
  • 1
    This doesn't answer the question, but I myself prefer a language choice page. Being in Belgium and using an english OS and browser, I'm always redirected to Dutch part of any "auto-detect-language" website while I speak french. Of course I would then expect my choice to be saved in a cookie so that I don't have to choose each time... – Laurent S. Jul 08 '14 at 12:16

2 Answers2

0

I don't think there is one answer for this question. I have written a few blogs about multilanguage support in a Zend Framework (v1) application, and wrote (still writing actually) a library that includes this.

Your approach of separating the different versions in the first directory path is good, but you'll need some rewrite rules, because I suppose you don't want to be replicating code in every language directory! :)

You'll also need to pre-define a set of supported languages, and have a fallback language if the browser's language is not in your list of supported languages.

Also, as Bartdude gave an example of, do not rely on the country of your visitor to decide the language.

Wouter Thielen
  • 1,016
  • 9
  • 21
0

Algorihtm

  • Detect language in index.php
  • Select correct language for accept language
  • route language pages

Detect Accept Language

function GetAcceptLanguageList(){
  $langs = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  $list = [];
  {
    $ls = explode(",",$langs);
    foreach ($ls as $key => $value) {
      $lx=explode(";",$value);
      array_push($list,$lx[0]);
    }
  }
  return $list;
}

This function will return a array. Example :

Array
(
    [0] => tr-TR
    [1] => tr
    [2] => en-US
    [3] => en
    [4] => la
)

After select accept language or default language. Route to language page example. /tr/index.php

Hasan Delibaş
  • 470
  • 3
  • 14