0

人们,

我正在开发一个基于 HTTP_ACCEPT_LANGUAGE 的函数,当我测试时,浏览器会显示一个重定向循环。我调查了其他帖子中提供的所有解决方案,但不起作用

更新:我首先尝试使用 httaccess 检测语言并使用正确的页面和语言变量重定向到正确的页面,但我发现了同样的问题。问题是我使用了两个变量:page(确定用户在哪个页面)和 lang(适当的语言,取决于用户的口语)。但是当用户第一次到达站点时:两个变量都是空的,所以这样做的目的是检测用户的浏览器语言,然后将其重定向到正确的 url,在我的情况下...... index.php? page=index&lang=en 例如...

我这样做了:

**init.php**

$supportedLangs = array (
    'en-GB',
    'en-US',
    'es-ES',
    'es-AR'
 );

 $languages = explode ( ',', $_SERVER ['HTTP_ACCEPT_LANGUAGE'] );

 detectBrowserLanguage($languages,$supportedLangs);



 **functions.php**


 function detectBrowserLanguage($languages,$supportedLangs) {

foreach ( $languages as $lang ) {
    if (in_array ( $lang, $supportedLangs )) {

            // Set the page locale to the first supported language found
         switch ($lang) {
        case 'es-ES' :
        header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
        exit ();
        break;

        case 'en-US' :
        header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
        exit ();
        break;

        case 'en-GB' :
        header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
        exit ();
        break;

        default :
        header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
        exit ();
        break;
        }
    }
    }
   }
4

2 回答 2

0

您可能正在从 index.php 页面调用 detectBrowserLanguage。确保在 URL 中传递 Lang 参数时不调用该函数。

你可以检查一下

isset($_GET['lang'])
于 2013-11-11T03:20:36.177 回答
0

如果页面已经使用检测到的语言,我添加了一种方法来打破循环。注意:请参阅我对您问题的评论。

$supportedLangs = array (
    'en-GB',
    'en-US',
    'es-ES',
    'es-AR'
 );

 $languages = explode ( ',', $_SERVER ['HTTP_ACCEPT_LANGUAGE'] );
 $currentLanguage='';
 if(isset($_GET['lang'])){
      $currentLanguage=$_GET['lang'];
 }
 detectBrowserLanguage($languages,$supportedLangs, currentLanguage);

功能

function detectBrowserLanguage($languages,$supportedLangs, $currentLanguage) {

    foreach ( $languages as $lang ) {
        if ($lang == $currentLanguage){
            return; //If the page is already redirected just return.
        }elseif (in_array ( $lang, $supportedLangs )) {

                // Set the page locale to the first supported language found
             switch ($lang) {
            case 'es-ES' :
            header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
            exit ();
            break;

            case 'en-US' :
            header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
            exit ();
            break;

            case 'en-GB' :
            header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
            exit ();
            break;

            default :
            header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
            exit ();
            break;
            }
        }
    }
}

编辑:

您可以使用http_negotiate_language轻松做到这一点。这只是有关如何执行此操作的指南。我没有测试过这个(只是在这里输入)

function detectBrowserLanguage($supportedLangs, $currentLanguage) {
    $negotiated_language = http_negotiate_language($supportedLangs);
    if ($currentLanguage != $negotiated_language){
        switch ($negotiated_language) {
            case 'es-ES' :
            header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
            exit ();
            break;

            case 'en-US' :
            header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
            exit ();
            break;

            case 'en-GB' :
            header ( "Location: index.php?pagina=inicio&lang=en", TRUE, 301 );
            exit ();
            break;

            default :
            header ( "Location: index.php?pagina=inicio&lang=es", TRUE, 301 );
            exit ();
            break;
        }
    }

}

参考:

  1. W3 接受语言 RFC
  2. 语言标签
于 2013-11-11T03:22:56.267 回答