繁体   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