簡體   English   中英

在PHP中檢測瀏覽器語言並相應地設置$ locale

[英]Detect Browser Language in PHP and set $locale accordingly

我正在努力實現,當有人訪問我的wordpress頁面時,加載了他的語言的.po(語言)包。 目前,可以通過向URL添加?lang =參數來更改語言。 但我希望根據瀏覽器語言選擇正確的語言。

我的代碼:

<?php
// start the session 
session_start();
$browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];

// if there's a "lang" parameter in the URL...  
if( isset( $_GET[ 'lang' ] ) ) { 

    // ...set a session variable named WPLANG based on the URL parameter...     
    $_SESSION[ 'WPLANG' ] = $_GET[ 'lang' ]; 

    // ...and define the WPLANG constant with the WPLANG session variable 
    $locale = $_SESSION[ 'WPLANG' ];
    echo 'Based on URL parameter';

// if there isn't a "lang" parameter in the URL...  
} else {

    // if the WPLANG session variable is already set...
    if( isset( $_SESSION[ 'WPLANG' ] ) ) {

        // ...define the WPLANG constant with the WPLANG session variable 
        $locale = $_SESSION[ 'WPLANG' ];
        echo 'Based on session variable';

   // if the WPLANG session variable isn't set...
   } else { 

        // set the WPLANG constant to your default language code is (or empty, if you don't need it)        
        $locale = $browserlang;
        echo 'Should be based on browser language. It is:' . $browserlang;

    } 
};
?>

$ locale用於設置語言並選擇正確的.po文件。

現在我想要$ locale

$ locale ='en_US'

默認情況下,當有人進入具有默認語言“de”,“de_DE”,“de_CH”或“de_AT”的頁面時,它應該是。

$ locale ='de_DE'

我目前正在使用的代碼不起作用。

$browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
echo $browserlang;

向我展示了正確的語言,即“de_DE”,但$locale = $browserlang不做任何事情。 另一方面,當我設置$locale = 'de_DE'它的工作原理......

提前謝謝你們。

編輯:

當我使用echo $locale會說de-DE。 多數民眾贊成,因為它不起作用......

編輯2:

那是因為它必須是de_DE(下划線)而不是de-DE(減號)......如何解決這個問題?

編輯3:

最后它有效。

向我展示正確的語言,即“de_DE”,

它不應該。 您向我們展示的代碼會在標頭值之前插入一個空格。

此外,您的代碼不處理多值的accept-language標頭,它也可以包含首選項(例如, 請參閱此處了解解析器)

如何規范化值({“de”,“de_DE”,“de_CH”,“de_AT”} - >“de_DE”)取決於您。

編輯3:

搞定了:

<?php
// start the session 
session_start();

// if there's a "lang" parameter in the URL...  
if( isset( $_GET[ 'lang' ] ) ) { 

    // ...set a session variable named WPLANG based on the URL parameter...     
    $_SESSION[ 'WPLANG' ] = $_GET[ 'lang' ]; 

    // ...and define the WPLANG constant with the WPLANG session variable 
    $locale = $_SESSION[ 'WPLANG' ];
    $languagemsg = 'based on url parameter';

// if there isn't a "lang" parameter in the URL...  
} else {

    // if the WPLANG session variable is already set...
    if( isset( $_SESSION[ 'WPLANG' ] ) ) {

        // ...define the WPLANG constant with the WPLANG session variable 
        $locale = $_SESSION[ 'WPLANG' ];
        $languagemsg = 'based on session variable';

    // if the WPLANG session variable isn't set...
    } else { 

        // set the WPLANG constant to your default language code is (or empty, if you don't need it) 
        $browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
        if ($browserlang == 'de') {$browserlang = 'de_DE';}
        else {$browserlang = 'en_US';}
        $_SESSION[ 'WPLANG' ] = $browserlang;
        $locale = $browserlang;
        $languagemsg = 'based on browser language';

    } 
};
?>

現代瀏覽器總是將首選語言列在其他語言之前。 這就是為什么我只挑選刺的前兩個字母:

$browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);

現在我為所有以“de”開頭的語言代碼設置$browserlang = de_DE 對於所有其他語言,我設置$browserlang = en_US

'$ languagemsg'僅用於調試原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM