简体   繁体   English

在PHP中检测浏览器语言并相应地设置$ locale

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

I'm trying to achieve, that when someone visits my wordpress page the .po (language-) packages of his pefered language are loaded. 我正在努力实现,当有人访问我的wordpress页面时,加载了他的语言的.po(语言)包。 At the moment it is possible to change the language by adding a ?lang= parameter to the URL. 目前,可以通过向URL添加?lang =参数来更改语言。 But i want the right language to be selected based on the browser language. 但我希望根据浏览器语言选择正确的语言。

My code: 我的代码:

<?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 is used to set the language and select the right .po files. $ locale用于设置语言并选择正确的.po文件。

Now i want $locale to be 现在我想要$ locale

$locale = 'en_US' $ locale ='en_US'

by default, but when someone enters the page that has default language "de", "de_DE", "de_CH" or "de_AT" it should be. 默认情况下,当有人进入具有默认语言“de”,“de_DE”,“de_CH”或“de_AT”的页面时,它应该是。

$locale = 'de_DE' $ locale ='de_DE'

The Code im currently using isnt working. 我目前正在使用的代码不起作用。

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

Shows me the right language, which is "de_DE", but $locale = $browserlang doesnt do anything. 向我展示了正确的语言,即“de_DE”,但$locale = $browserlang不做任何事情。 On the other hand when i set $locale = 'de_DE' it works... 另一方面,当我设置$locale = 'de_DE'它的工作原理......

Thank you guys in advance. 提前谢谢你们。

Edit: 编辑:

When i use echo $locale is says de-DE. 当我使用echo $locale会说de-DE。 Thats very stange, because it doesnt work... 多数民众赞成,因为它不起作用......

Edit 2: 编辑2:

Thats because it must be de_DE (underline) not de-DE (minus)... how to fix that? 那是因为它必须是de_DE(下划线)而不是de-DE(减号)......如何解决这个问题?

Edit 3: 编辑3:

Finally it works. 最后它有效。

Shows me the right language, which is "de_DE", 向我展示正确的语言,即“de_DE”,

It shouldn't. 它不应该。 The code you've shown us inserts a space before the header value. 您向我们展示的代码会在标头值之前插入一个空格。

Also, your code does not handle a multi-valued accept-language header which can also include preferences (eg see here for a parser) 此外,您的代码不处理多值的accept-language标头,它也可以包含首选项(例如, 请参阅此处了解解析器)

How you normalize the value ( {"de", "de_DE", "de_CH","de_AT"} -> "de_DE" ) is up to you. 如何规范化值({“de”,“de_DE”,“de_CH”,“de_AT”} - >“de_DE”)取决于您。

Edit 3: 编辑3:

Got it working: 搞定了:

<?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';

    } 
};
?>

Modern browsers always list the prefered language in front of the others. 现代浏览器总是将首选语言列在其他语言之前。 Thats why i only pick the first two letters of the sting: 这就是为什么我只挑选刺的前两个字母:

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

now i set $browserlang = de_DE for all language codes that begin with "de". 现在我为所有以“de”开头的语言代码设置$browserlang = de_DE For all other languages i set $browserlang = en_US . 对于所有其他语言,我设置$browserlang = en_US

'$languagemsg' is just for debugging reasons. '$ languagemsg'仅用于调试原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM