【问题标题】:Detect Browser Language in PHP and set $locale accordingly检测 PHP 中的浏览器语言并相应地设置 $locale
【发布时间】:2015-08-14 18:15:46
【问题描述】:

我正在努力实现,当有人访问我的 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:

终于成功了。

【问题讨论】:

  • 你为什么在你的变量前面放一个" "?如果您删除它,它应该可以工作吗?
  • 使用 str_replacede-DE 更改为 de_DEstr_replace('-','_',$browserlang);
  • 谢谢你们两位的cmets。 @GabrielMoretti 是的,那会起作用。但我找到了另一种方法:我将在大约 1 分钟内编辑我的帖子。
  • 这个文件是用来配置语言的吗?
  • 您问题的代码。如果您的应用程序可能会长大,您应该寻找更通用的解决方案。否则,您将需要不断为每种新的可用语言添加条件(在我看来,这违反了开放-封闭原则)

标签: php html wordpress


【解决方案1】:

编辑 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' 只是出于调试原因。

【讨论】:

    【解决方案2】:

    显示正确的语言,即“de_DE”,

    不应该。您向我们展示的代码在标题值之前插入了一个空格。

    此外,您的代码不处理还可以包含首选项的 multi-valued accept-language 标头(例如,see here 用于解析器)

    如何规范化值( {"de", "de_DE", "de_CH","de_AT"} -> "de_DE" )取决于您。

    【讨论】:

    • 所以也许我应该将我的解决方案与此线程上发布的解决方案结合起来:stackoverflow.com/questions/3770513/…?其中一些负责多种语言和 q 值。
    • 你可以 - 但我不熟悉 Wordpress 中的 API 以及它们如何表达语言的可用性。
    • 这个问题不是 wordpress 特有的。返回给 wordpress 的唯一参数是 $browserlang,它必须包含所选语言。
    猜你喜欢
    • 2011-04-15
    • 1970-01-01
    • 2010-12-18
    • 2011-10-30
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2012-02-10
    • 2019-02-17
    相关资源
    最近更新 更多