Detect Browser Language in PHP

后端 未结 13 1374
孤城傲影
孤城傲影 2020-11-22 09:16

I use the following PHP script as index for my website.

This script should include a specific page depending on the browser\'s language (automatically detected).

13条回答
  •  日久生厌
    2020-11-22 09:54

    Quick and simple:

    $language = trim(substr( strtok(strtok($_SERVER['HTTP_ACCEPT_LANGUAGE'], ','), ';'), 0, 5));
    

    NOTE: The first language code is what is being used by the browser, the rest are other languages the user has setup in the browser.

    Some languages have a region code, eg. en-GB, others just have the language code, eg. sk.

    If you just want the language and not the region (eg. en, fr, es, etc.), you can use:

    $language =substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    

提交回复
热议问题