PHP language detection

前端 未结 9 2235
陌清茗
陌清茗 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.

    <?php
    // List of available localized versions as 'lang code' => '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]);
    ?>
    
    0 讨论(0)
  • 2020-12-14 17:31

    Try This

    function getUserLanguage() {
        $langs = array();
    
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            // break up string into pieces (languages and q factors)
            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);
            }
        }
        //extract most important (first)
        reset($langs);
        $lang = key($langs);
    
        //if complex language simplify it
        if (stristr($lang, '-')) {
            list($lang) = explode('-', $lang);
        }
    
        return $lang;
    }
    
    0 讨论(0)
  • 2020-12-14 17:36

    The browser generally sends a HTTP header, name Accept-Language, that indicates which languages the user is willing to get.

    For instance, this header can be :

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

    There is notion of priority in it, btw ;-)

    In PHP, you can get this in the $_SERVER super global :

    var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);
    

    will get me :

    string 'en-us,en;q=0.5' (length=14)
    

    Now, you have to parse that ;-)


    If I edit my preferences in the browser's option to say "I want french, and if you don't can't serve me french, get me english from the US ; and if you can't get me that either, just get me english), the header will be :

    Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3
    

    And, from PHP :

    string 'fr-fr,en-us;q=0.7,en;q=0.3' (length=26)
    


    For more informations, you can take a look at section 14.4 of the HTTP RFC.

    And you probably can find lots of code example in PHP to parse that header ; for instance : Parse Accept-Language to detect a user's language

    Have fun !

    0 讨论(0)
提交回复
热议问题