Pin*_*lus 5 html php wordpress
我正在努力实现,当有人访问我的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;
    } 
};
?>
Run Code Online (Sandbox Code Playgroud)
$ locale用于设置语言并选择正确的.po文件.
现在我想要$ locale
$ locale ='en_US'
默认情况下,当有人进入具有默认语言"de","de_DE","de_CH"或"de_AT"的页面时,它应该是.
$ locale ='de_DE'
我目前正在使用的代码不起作用.
$browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
echo $browserlang;
Run Code Online (Sandbox Code Playgroud)
向我展示正确的语言,即"de_DE",但$locale = $browserlang不做任何事情.另一方面,当我设置$locale = 'de_DE'它工作...
提前谢谢你们.
编辑:
当我使用的echo $locale是de-DE.多数民众赞成,因为它不起作用......
编辑2:
那是因为它必须是de_DE(下划线)而不是de-DE(减号)......如何解决这个问题?
编辑3:
最后它有效.
编辑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';
    } 
};
?>
Run Code Online (Sandbox Code Playgroud)
现代浏览器总是将首选语言列在其他语言的前面。这就是为什么我只选择刺痛的前两个字母:
$browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
Run Code Online (Sandbox Code Playgroud)
现在我设置$browserlang = de_DE所有以“de”开头的语言代码。对于我设置的所有其他语言$browserlang = en_US。
'$languagemsg' 只是出于调试原因。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           3529 次  |  
        
|   最近记录:  |