PHP language detection

前端 未结 9 2257
陌清茗
陌清茗 2020-12-14 16:32

I\'m trying to build multilangual site.

I use this piece of code to detect users language. If you havent chosen a language, it will include your language file based

9条回答
  •  渐次进展
    2020-12-14 17:30

    Here's the script I used for a bi-lingual site. It is to be used as index.php of mysite.com. Based on the user's browser's language preference, it would redirect to desired language version of the site or the default language site if the site in user's preferred langauge was not available.

     'url' map
    $sites = array(
        "en" => "http://en.mysite.com/",
        "bn" => "http://bn.mysite.com/",
    );
    
    // Get 2 char lang code
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    
    // Set default language if a `$lang` version of site is not available
    if (!in_array($lang, array_keys($sites)))
        $lang = 'en';
    
    // Finally redirect to desired location
    header('Location: ' . $sites[$lang]);
    ?>
    

提交回复
热议问题